Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS288 Lab Exercise 2.

Similar presentations


Presentation on theme: "CS288 Lab Exercise 2."— Presentation transcript:

1 CS288 Lab Exercise 2

2 Open PersonGui Files Go to Course web site for PersonGui source files
Make copies of these files in you home folder and then open them in some editor (e.g. Emacs): PersonGui.java PersonMaker.java Person.java

3 1: PersonMaker.java Open PersonMaker.java Find the setAge method:
public void setAge (String stringAge) { string_age = stringAge; try { age = new Double(stringAge); } catch (NumberFormatException exp1) { string_age = handleTextInput("Age"); //Why could this result in an error? // YOU HAVE TO PUT SOME CODE HERE // TO MAKE THIS WORK WHEN THE USER // DOES NOT ENTER A VALID NUMBER VALUE age = new Double(string_age); }

4 1: PersonMaker.java What happens in the PersonGui if we select Set Age from the pop up menu when no value has been set? If you click on OK for value of Age with the default value (“Fluffy”) what error messages appear,and where (see command shell)?

5 1: PersonMaker.java Change the setAge method, so that if the string_age value given by the dialog is not a suitable value for age, then string_age changes it to some sensible value (say age = new Double(0.0)). To do this you will have to add another try catch statement, this will have to be around the second occurrence of age = new Double(string_age) in the method definition. To find out about the Double class constructor follow the link on the class web site for Specification of ALL Java classes Then find the entry for Double in the left hand column called ‘All Classes’.

6 1: PersonMaker.java Finally add the following expression to the setAge method. Put it immediately after the line where you set the age value to be new Double(0.0) if the user entered an invalid number. Change the text in the string values to give a meaningful message. JOptionPane.showMessageDialog( null, " Make this message meaningful ", "Make this message meaningful", JOptionPane.PLAIN_MESSAGE);

7 2: PersonMaker.java Open the following link to the Java tutorial. This describes how while loops work. Find the setHeight method in PersonMaker.java. Define a method boolean is_ok_height (String hstring); that returns true if new Double(hstring) returns a value, but returns false if the constructor Double(hstring) throws an exception. Change the catch section of the code so that it now uses a do-while loop. The idea is to force the user to give a valid height. If it is not a valid number then the while loop will keep repeating until the user gets fed up and enters a proper value. See next page for structure of loop. Recall a do while loop looks like: do { statement(s) } while (expression);

8 2: PersonMaker.java The catch section will now be a do-while loop that: asks the user for a value for height using the handleTextInput("Height"); method use is_ok_height (String hstring); to check if the user has entered a valid number. if not repeat step 1 if it is OK the loop will terminate and set the value of height accordingly


Download ppt "CS288 Lab Exercise 2."

Similar presentations


Ads by Google