Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed GUI Java Program to...




Objectives



This assessment item relates to the course learning outcomes as stated in the Unit Profile.




Details



For this assignment, you are required to develop a
Windowed GUI Java Program
to demonstrate you can use Java constructs including input/output via a GUI interface, Java primitive and built-in types, Java defined objects, arrays, selection and looping statements and various other Java commands. Your program must produce the correct results.


The code for the GUI interface is supplied and is available on the course website, you must write the underlying code to implement the program. The command buttons are linked to appropriate methods in the given code. Please spend a bit of time looking at the given code to familiarise yourself with it and where you have to complete the code. You will need to write comments in the supplied code as well as your own additions.



What to submit for this assignment


The Java source code:


You will need to submit two source files:
BodyMassIndex.java
and
BodyMassIndexGUI.java, please submit these files as a single zip file, do not include your report in the zip file you must submit it as a separate file.


o
Ass2.zip



If you submit the source code with an incorrect name you will lose marks.


A report including an
UML diagram of your BodyMassIndex class
(see text p 493), how long it took to create the whole program, any problems encountered and screen shots of the output produced. (Use Alt-PrtScrn to capture just the application window and you can paste it into your Word document) You should test every possibility in the program.




Assignment Specification



In assignment one we read in multiple person names, heights and weights using both Scanner and GUI dialogs. In this assignment we are going to read in the data and output information via a GUI interface, the code for the GUI interface
BodyMassIndexGUI.java
is supplied (via the Moodle web site) which supplies the basic functionality of the interface. We are also going to store the information in an array of BodyMassIndex objects.


Look at the code supplied and trace the execution and you will see the buttons are linked to blank methods (stubs) which you will implement the various choices via the buttons.


The GUI interface contains four JLabels for the heading and the prompts.


There are three JTextFields in which the person, height and weight are entered read.


There is also a JTextArea for displaying the output.


Four JButtons are at the bottom which link to blank methods for implementing the functionality of the application.




BodyMassIndex class



First step is to create a class called BodyMassIndex (BodyMassIndex.java) which will not contain a main.


The BodyMassIndex class will be very simple which will contain three
private
instance variables:


o
studentName
as a String


o
height
as a double


o
weight
as a double



The following public methods will have to be implemented:


o A default constructor


o A parameterised constructor


o Three set methods (mutators)


o Three get methods (accessors)


o deriveBodyMassIndex value returning method*


o deriveRatingvalue returning method**



*You will create a public value returning method deriveBodyMassIndex(), the body mass index can be derived from the instance variables height and weight. Typically we do not store calculated values in a database so the BMI will be derived via your deriveBodyMassIndex() method.
Do not store the body mass index as an instance variable.


**You will also create a public value returning method deriveRating() to return the BMI rating “Underweight”, “Normal”, “Overweight” etc (see assignment one). The rating can be derived from the BMI and as with the BMI above typically we do not store calculated values in a database and the rating will be retrieved via your deriveRating() method. Use constants in the if statements for the grade boundaries. Hint: use deriveBodyMassIndex() method above in your deriveRating() method to calculate the BMI (we do not want to repeat any code in our program).




BodyMassIndexGUI class



Once the BodyMassIndex class is implemented and fully tested we can now start to implement the functionality of the GUI interface.



Data structures


For this assignment we are going to store the person names, heights and weights in an
array
of BodyMassIndex objects.
Do not use the ArrayList data structure.


Declare an array of BodyMassIndex objects as an
instance variable of BodyMassIndexGUI class, the array should hold
ten
entries. Use a constant for the maximum entries allowed.


private BodyMassIndex [] bmiArray = new BodyMassIndex[MAX_PERSONS];


We need another instance variable (integer) to keep track of the number of persons being entered and use this for the index into the array of BodyMassIndex objects.


private int currentPerson = 0;




Button options




1.

Enter button: enterPersonNameHeightAndWeight()



For assignment two we are going to use the JTextFields for our input.


When the enter button is pushed the program will transfer to the method: enterPersonNameHeightAndWeight() this is where we read the person name, height and weight and add them to the BodyMassIndex array.


The text in the JTextFields is retrieved using the getText() method:


String personName = nameField.getText();


When we read the height input we are reading a string from the GUI, we will need to convert this to a double using the Double wrapper class as per assignment one.


double height = Double.parseDouble(heightField.getText());


Repeat this process to read in the weight value.


We need to add these values name, height and weight to the array of BodyMassIndex objects, when we declared our array using the new keyword, only an array of references were created and we need to create an instance of each of the BodyMassIndex objects. When we create the BodyMassIndex object we can pass the name, height and weight to the object via the parameterised constructor.


bmiArray[currentPerson] =


new BodyMassIndex(personName, height, weight);


Remember to increment currentPerson at the end of the enter method.


Next we will output the entry including the person’s BMI and rating in the JTextArea


The supplied code contains the methods for printing the heading and the line underneath.


The font in the text area is “fixed width” so can be aligned using column widths.


displayTextArea.setText(String.format("%-21s%-9s%-9s%-4s%12s\n", "Person Name", "Height", "Weight", "BMI", "Rating"));


Just like the JTextFields the JTextArea has a method setText() to set the text in the text area, note this overwrites the contents of the text area.


You can append text to the text area by using the append() method.


displayTextArea.append("----------- … ----------\n");


Hint: use the above format string to display all of the values for the person, Java will convert the numerical values to strings for you and the values will align with the headings. To retrieve the values from the array use the get and derive methods in your BodyMassIndex class. Create a separate method to display one line of data and pass the index to this method.


e.g


bmiArray[index].getPersonName();



When the data has been entered and displayed, the name, height and weight JTextFields should be cleared. We can clear the contents by using: nameField.setText("");


The focus should then return to the
person name
JTextField.


nameField.requestFocus();



Data validation
(you can implement this after you have got the basic functionality implemented)


When the maximum number of persons is reached, do not attempt to add any more persons and give the following error message:


Use an if statement at the beginning of the method and after displaying the error dialog use the return statement to exit the method.


if (nameField.getText().compareTo("") == 0) // true when blank


Use the above code to check the name field for text and if there is no text then display the following error dialog and use the return statement to exit the method, the focus should return to the name field.


The height field should also be checked for text, do the same for the weight.



We will not worry about checking data types or numeric ranges in this assignment.




2. Display all persons’ names, heights, weights, BMIs and ratings
: displayAll()


When this option is selected, display all of the person names, heights, weights, BMIs and ratings which have been entered so far. At the end of the list display the average BMI.


.


Use a loop structure to iterate through the array, you should be sharing the printing functionality with the enter button, hint: use the method to display a line of data from the enter method.


Only print the entries which have been entered so far and not the whole array, use your instance variable currentPerson as the termination value in your loop.


Sum the BMIs in the loop for the average calculation at the end.


If no entries have been added then clear the text area and display the following error dialog,
repeat this for your search.



2.

Search for a person name: search()



You can just use a simple linear search which will be
case insensitive. Use the JOptionPane.showInputDialog() method to input the person’s name.


If the search is successful display the details about the person.


If the search is unsuccessful display an appropriate message and clear the text area.




4. Exit the application: exit()


The exit method is called when the user pushes the exit button or when they select the system close (X at top right hand corner), try and find the code which does this.


During a typical exiting process we may want to ask the user if they want to save any files they have opened or if they really want to exit, in this assignment we will just print an exit message.


Jan 30, 2020
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here