Activity_04A - COMP 1210 Reading: Chapter 4.1 – 4.5...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 1210 Reading: Chapter 4.1 – 4.5 Activity 4A: Creating Classes I Page 1 of 5 pages 160-181 Terminology: attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Due: Activity: Monday, Feburary 6, 2012. At the end of the lab Goals: By the end of this activity you should be able to do the following: Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class Know how to generate a UML diagram and documentation in jGRASP Program Description: In this activity you will create a class called ProfileInformation, which will hold basic information about a person. In-lab Directions: Part 1: Create Project and Generate UML Class Diagram • Create your ProfileInformation class and add the following comments (there will be no main method in this class): ÏÕÖ×public class ProfileInformation { Ïϧ // declare instance variables here Ïϧ // constructor Ïϧ // methods Ïϧ // toString method (for String output) ÏÏ©} • Instance variables represent all of the information that an object of the class needs to store. Declare the following variables: o firstName: String that will later reference the person’s first name o lastName: String that will later reference the person’s last name o location: String that will later reference the person’s location o age: int that will contain the person’s age o status: int representing whether the person is online or offline Hint: Use the private access modifier so that values cannot be changed from outside of the object: private String firstName; COMP 1210 | Spring 2012 Page 1 of 5 COMP 1210 Activity 4A: Creating Classes I Page 2 of 5 • Constants store values that cannot be changed. Use constants to represent the two possible statuses of the user. private static final int OFFLINE = 0, ONLINE = 1; • The toString method returns a String representation of the object. Create and return a local variable called output with all of the profile information: ÏϧÏÞßàpublic String toString() { ÏϧÏϨ¹íÏString output = "Name: " + firstName + " " ÏϧÏϧÏÏÏÏÏÏ+ lastName + "\n"; ÏϧÏϨ¹¹Ïoutput += "Location: " + location + "\n"; ÏϧÏϨ¹¹Ïoutput += "Age: " + age + "\n"; ÏϧÏϨ¹¹Ïoutput += "Status: " + status; Ïϧ¹Ĺ¹Ïreturn output; ÏϧÏÏ©} After you have successfully compiled your class, open the interactions pane in jGRASP. Create a new ProfileInformation object using the new operator and print it out: ϼÏÏProfileInformation p = new ProfileInformation(); ϼÏÏSystem.out.println(p); ÏÏÏÏName: null null ÏÏÏÏLocation: null ÏÏÏÏAge: 0 ÏÏÏÏStatus: 0 Notice that because the String values haven’t been initialized they have a value of null. In order to avoid this, you will add a constructor to initialize your instance variables. • The constructor includes code that will be run when the object is first created. It should be placed before the methods in the class. The constructor does not have a return type, and always has the same name as the object. When a profile is created, the constructor will take two parameters as input: first name and last name. ÏÞßàpublic ProfileInformation(String first, String last){ Ïϧ ÏÏ©} In the constructor, store the first and last name in the appropriate instance variables: ÏϧÏϨ¹¹ÏfirstName = first; ÏϧÏϨ¹¹ÏlastName = last; Because you don’t have inputs for age, location, and status, you’ll have to set those to default values: ÏϧÏϨ¹¹Ïlocation = "Not specified"; ÏϧÏϨ¹¹Ïage = 0; ÏϧÏϨ¹¹Ïstatus = OFFLINE; COMP 1210 | Spring 2012 Page 2 of 5 COMP 1210 Activity 4A: Creating Classes I Page 3 of 5 Compile your program, open up the interactions pane, and create a new profile with the following input. Now that your constructor has stored your values, you should get the following output: ϼÏÏProfileInformation p = new ProfileInformation("Jane", "Lane"); ϼÏÏSystem.out.println(p); ÏÏÏÏName: Jane Lane ÏÏÏÏLocation: Not specified ÏÏÏÏAge: 0 ÏÏÏÏStatus: 0 • The methods of your class describe what your object can do (the behaviors of an object). For a profile, you would want to be able to set the location and age off the user. You’d also want to allow the user to change their status. First, create a set method for location: ÏϧÏÞßàpublic void setLocation(String locationName){ ÏϧÏϨ¹¹Ïlocation = locationName; ÏϧÏÏ©} Test your program in the interactions pane: ϼÏÏProfileInformation p = new ProfileInformation("Jane", "Lane"); ϼÏÏp.setLocation("Auburn"); ϼÏÏSystem.out.println(p); ÏÏÏÏName: Jane Lane ÏÏÏÏLocation: Auburn ÏÏÏÏAge: 0 ÏÏÏÏStatus: 0 Add a method to change age. This will only change the age if the age is over 0 years. It will return a boolean representing whether the age was set: ÏϧÏÞßàpublic boolean setAge(int ageInYears) { ÏϧÏϨ¹íÏboolean isSet = false; ÏϧÏϨ¹³´if(ageInYears > 0) { ÏϧÏϧÏ6¨¹¹Ïage = ageInYears; ÏϧÏϧÏ6¾¹¹ÏisSet = true; ÏϧÏϧ϶Ï} Ïϧ¹Ĺ¹Ïreturn isSet; ÏϧÏÏ©} Add a method to return the age. Notice this method takes no parameters, but returns an int value (instead of void). Represents what the method returns ÏϧÏÞßàpublic int getAge() { (an int value). Ïϧ¹Ĺ¹Ïreturn ________; ÏϧÏÏ©} COMP 1210 | Spring 2012 Page 3 of 5 COMP 1210 Activity 4A: Creating Classes I Page 4 of 5 Finally, add a method to return the location of the user. ÏϧÏÞßàpublic ________ getLocation() { Ïϧ¹Ĺ¹Ïreturn ________; ÏϧÏÏ©} Add the following two methods to allow the user to log off and on: ÏϧÏÞßàpublic void logOff() { ÏϧÏϨ¹¹Ïstatus = OFFLINE; ÏϧÏÏ©} Ïϧ ÏϧÏÞßàpublic void logOn() { ÏϧÏϨ¹¹Ïstatus = ONLINE; ÏϧÏÏ©} The actual values (0 and 1) of offline status and online status are not used in the code because they are declared as constants. This makes code easier to read and modify later. You should also hide the values when printing the class information. Modify the toString method as follows: ÏϧÏÞßàpublic String toString(){ ÏϧÏϨ¹íÏString output = "Name: " + firstName + " " ÏϧÏϧÏÏÏÏÏÏ+ lastName + "\n"; ÏϧÏϨ¹¹Ïoutput += "Location: " + location + "\n"; ÏϧÏϨ¹¹Ïoutput += "Age: " + age + "\n"; ÏϧÏϨ¹¹Ïoutput += "Status: "; Prints Offline or Online rather ÏϧÏϨ¹³´if(status == OFFLINE) { than 0 or 1 for status. ÏϧÏϧÏ6¾¹¹Ïoutput += "Offline"; ÏϧÏϧÏ6Ï} ÏϧÏϧÏö´else{ ÏϧÏϧϸ¾¹¹Ïoutput += "Online"; ÏϧÏϧÏÈÏ} ÏϧÏϧ Ïϧ¹Ĺ¹Ïreturn output; ÏϧÏÏ©} Test each of your methods in the interactions pane. Your output should be as follows: ϼÏÏProfileInformation p = new ProfileInformation("Jane", "Lane"); ϼÏÏp ÏÏ«ÏName: Jane Lane ÏϧÏLocation: Not specified ÏϧÏAge: 0 ÏÏ©ÏStatus: Offline ϼÏÏp.setAge(23); ϼÏÏp.setLocation("Auburn"); ϼÏÏp.logOn(); ϼÏÏp COMP 1210 | Spring 2012 Page 4 of 5 COMP 1210 Activity 4A: Creating Classes I Page 5 of 5 ÏÏ«ÏName: Jane Lane ÏϧÏLocation: Auburn ÏϧÏAge: 23 ÏÏ©ÏStatus: Online Part 2: Create Project and Generate UML Class Diagram • In addition to writing the class above, you are to create another class as your driver program (Activity4A.java). The main method in Activity4A.java should create several instances of ProfileInformation and exercise the methods to test your code. Then you are to create a jGRASP project file that includes the ProfileInformation class and the class with your main program – Activity4A. After you have created the project file, you should generate (and layout) the UML class diagram for the project. Since this is your first "project" assignment, it may be easier for you to do it after you have completed the program. However, in the future you should be comfortable adding classes to the project file as you implement them and generating/updating the UML class diagram with each new addition. Below is the “quick” version for creating a jGRASP project. 1. With Activity4A in the edit window, click the UML button on the toolbar. 2. In the Add File to Project dialog, click the “Create New Project” button. 3. In the New Project dialog, click the “Create” button to create a project named Activity4A_Project. 4. In the In the Add File to Project dialog, click the “Add” button to add Activity4A.java to the project. This should open the UML window, and you should see “Activity4A” in a green rectangle. 5. Now you need to add the ProfileInformation class to the project. You can do this by dragging ProfileInformation.java from the Browse tab to the UML window. 6. Your UML class diagram should now have two classes. You can rearrange these by dragging them around on the screen. 7. Right-click on one of the classes, and select “Show Class Info” to see the class member information in the UML Info tab. 8. Right-click a red dashed dependency and you see the dependencies in the UML Info tab. COMP 1210 | Spring 2012 Page 5 of 5 ...
View Full Document

This note was uploaded on 03/02/2012 for the course COMP 1210 taught by Professor Cross during the Winter '07 term at Auburn University.

Ask a homework question - tutors are online