Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS288 Lab Exercise 1. UniS Open PersonGui Files Go to Course web site for PersonGui source files

Similar presentations


Presentation on theme: "CS288 Lab Exercise 1. UniS Open PersonGui Files Go to Course web site for PersonGui source files"— Presentation transcript:

1 CS288 Lab Exercise 1

2 UniS Open PersonGui Files Go to Course web site for PersonGui source files http://www.computing.surrey.ac.uk/courses/cs288/Examples/PersonGui 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 images.zip (unzip this file in the same directory as you have put the java files) Download a copy of these slides so that you can read them directly, and not simply view them in Internet Explorer. This is handy as you can then copy and paste code examples directly from these slides.

3 UniS Read the slides on compiling Java Code Make sure you have read the notes on compiling Java Code: http://www.computing.surrey.ac.uk/courses/cs288/Labs/Compile_Java.ppt Compile PersonGui.java. In a command window type: javac PersonGui.java now run the application java PersonGui Right click the mouse in the large text area. You should see a popup menu. Enter in your name age height etc with this menu. Right click the mouse and choose the create person item. If you have installed the images correctly from the zip file you should be able to choose a suitable image at this point.

4 UniS How the PersonGui should appear Choose this option should cause this dialog to appear

5 UniS How the PersonGui should appear Once you have picked an image it should then appear as below.

6 UniS 1: Search for Make_Person method Find (could use Search in Emacs) the Make_Person ( ) method in PersonMaker.java

7 UniS 1: Change the Make_Person method public String Make_Person ( ) { Show_Attributes_InFrame (); String return_value = "This Person has these attributes: \n " + "\n\tFirst Name \t= " + getFirstName() + "\n\tLast Name \t= " + getLastName() + "\n\tHeight \t= " + getPersonHeight() + "\n\tAge \t= " + getPersonAge() + "\n"; return return_value; } Change the value of Make_Person to this: Delete any.class files you have. Recompile and rerun the code. This should not make any difference to the way the code works. Test your code to make sure that is true.

8 UniS 1: Change the Make_Person method again public String Make_Person ( ) { Show_Attributes_InFrame (); String return_value = "This Person has these FANTASTIC attributes: \n " + "\n\tFirst Wonderful Name \t= " + getFirstName() + "\n\tLast Stunning Name \t= " + getLastName() + "\n\tLook at that Height \t= " + getPersonHeight() + "\n\tLook at that Age \t= " + getPersonAge() + "\n"; return return_value; } Change the value of Make_Person to this: Recompile and rerun the code. How does this change the way the code works?

9 UniS 1: Change the Make_Person method again public String Make_Person ( ) { Show_Attributes_InFrame (); String the_empty_string = ""; //This will be handy for the next part String return_value = "This Person has these FANTASTIC attributes: \n "; return_value = return_value + "\n\tFirst Wonderful Name \t= " + getFirstName(); return_value = return_value + "\n\tLast Stunning Name \t= " + getLastName(); return_value = return_value + "\n\tLook at that Height \t= " + getPersonHeight(); return_value = return_value + "\n\tLook at that Age \t= " + getPersonAge() + "\n"; return return_value; } Change the value of Make_Person to this: Recompile and rerun the code. How does this change the way the code works?

10 UniS 1: Set Empty String as Age If we set the first name to be empty with the menu:

11 UniS 1: Empty String as Age Then we get a somewhat redundant message on the screen

12 UniS 1: Look in PersonGui.java Change the Make_Person method so that if the user enters a value for Age that is the empty string, then the Make_Person returns a String that says: You entered an empty string for the first name Instead of The Persons First Name is: "" If the user enters a non-empty string the return value is unchanged. To do this you need to use an if statement. See the Java Tutorial on the course web site to see how if statements work: http://www.computing.surrey.ac.uk/courses/cs288/Java_Documentation/Tutorial Go to the section on Learning the Java Language then to Language BasicsLearning the Java LanguageLanguage Basics then to Expressions, Statements, and BlocksExpressions, Statements, and Blocks Also you need to understand how some of the methods work for the String class. Go to the Java SDK 2, Class Specifications link on the course web site: http://www.computing.surrey.ac.uk/courses/cs288/Java_Documentation/docs/api/in dex.html

13 UniS Search for the String class in this column. Click on the String link

14 UniS Then click on the METHOD link at the top of the page. Now find the equals method for this class. This is useful if you want to know if a string is the empty string.

15 UniS 1: Change the Make_Person method again Now change the Make_Person method again so that if the User enters an empty string for the last name of someone a more sensible string is returned. Now change the method so that if a users age is greater than 150.0 then the return_string makes a suitable comment about how incredibly old that person is.

16 UniS 2: Find PersonMaker Constructor Find the PersonMaker constructor in the PersonMaker.java file Find the reset_Person ( ) method.

17 UniS 2: PersonMaker Note the contents of reset_Person ( ) and PersonMaker ( ) are the same. Define a third method, public void set_default_values ( ) which is just a copy of the current reset_Person method with new name. Now change reset_Person ( ) and PersonMaker ( ) so that they simply call the set_default_values ( ) method and do not change any variable directly themselves.

18 UniS 3: Removing Redundant Labels Find the Person constructor method in the Person.java file

19 UniS 3: Removing Redundant Labels Person ( ) creates a seperate JLabel for height, age, first and last names. This is redundant. Change the code so that Person ( ) only uses one JLabel for all the strings that are to be displayed. The purpose of this exercise is for you to learn how to track all the data that is associated with one variable, and also modify that data for a different purpose.

20 UniS 3: Removing Redundant Labels For example in this case, you will have to work out where variables first_text last_text age_text height_text are referred to in the rest of the method. You will have to figure out how to combine these variables into a single string (new String variable declaration). You will have to comment out those parts of the code that set up and use unwanted JLabels (such as label_first). You will have to create a new JLabel for the combined text. Now attach the JLabel somehow so that it replaces the four redundant labels. You will be able to do this simply by copying the way the other JLabels are defined, you dont need to understand what it all means yet.


Download ppt "CS288 Lab Exercise 1. UniS Open PersonGui Files Go to Course web site for PersonGui source files"

Similar presentations


Ads by Google