Assignment Chef icon Assignment Chef

[Solved] COMP1210 Activity4A- Writing Classes

5.0 1 customer review Digital download

Digital download

$25.00

Availability
In stock
Checkout
One item

Need a hand?

Message us on WhatsApp for payment or download support.

WhatsApp QR code
Terminology
attribute / state behaviorclass method header class header instance variable UML class diagram encapsulationclientvisibility (or access) modifieraccessor method mutator method calling method method declaration method invocation return statement parameters constructor
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 Description In this activity, you will create two classes: one called UserInfo and another called UserInfoDriver which contains a main method that uses the first class (i.e., UserInfoDriver creates instances of UserInfo and calls its methods). You will also create a project, UML class diagram, and canvas for the program. Part 1: UserInfo.java Create and test the UserInfo class. o status: int indicates whether the user is online or offline Hint: Use the private access modifier so that values cannot be directly accessed from outside of the object; for example, declare firstName as follows:

private String firstName;

private static final int OFFLINE = 0, ONLINE = 1;

In the constructor, store the first and last name in the appropriate instance variables: Because you do not have inputs for age, location, and status, you will need to set those to default values: Compile your program, click the Interactions tab, and create a new UserInfo object called u and display it by entering the following in Interactions tab. Note that the odd looking value ([email protected]), which is the objects default toString value, is not very useful. When we print or display u, we usually want to see some text that tells us something about the object. In the Workbench tab, find u and unfold it to see its fields. We can see that the fields in u do have the values that we set with constructor. The solution is to add a toString method to our UserInfo class. After you have successfully compiled your class, click the Interactions tab, and create a new UserInfo object called u and display it by entering the following in Interactions tab as you did above. This time, when u is evaluated, the toString method is implicitly invoked on u and the return value is displayed. In the example above, you can also display u by entering the print statement in interactions:

System.out.println(u);

When the u is entered without a semi-colon, it is an expression rather than a statement. You could also enter u.toString() as an expression. Remember, in the Interactions tab, expressions are evaluated and the result is immediately displayed. Since u is an object, it evaluates to the result of calling its toString method. After compiling your class, test it in the interactions pane: Add a method to set the value of age. This will only change age if the age is greater than 0, and it will return a boolean value (true or false) indicating whether the age was set: Add a method to return the age (fill in the blank). Notice this method takes no parameters, but returns an int value (instead of void). Add the following two methods to allow the user to log off and on: The actual values (0 and 1) for 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 to print Offline rather than 0 and Online rather than 1: After compiling your class, test each of your methods in the interactions pane. Your output should be as follows: Part 2: UserInfoDriver.java Create UserInfoDriver class with main method; create a project file and generate the UML class diagram for the program; create canvas for UserInfoDriver and run the program in the canvas. Web-CAT After you have created the jGRASP project in the next section, you should submit your UserInfo.java and UserInfoDriver.java files to Web-CAT via the Web-CAT button on the project toolbar.
UML Class Diagram for Project UserInfoDriver
Members for UserInfo in UML Info tab
Dependencies in UML Info tab
Right-click on UserInfo and select Show Class Info to see the class member information in the UML Info tab. See Members for UserInfo in the figure at right below the UML class diagram. Right-click the red dashed dependency arrow and select Show Dependencies Info to see the dependencies of between the classes in the UML Info tab; i.e., UserInfoDriver is using the UserInfo constructor, and methods logOn, setAge, and setLocation in the UserInfo class. See figure below. You can also create instances of the class by rightclicking and selecting Create New Instance which places an instance on the workbench. To invoke a method for an object on the workbench, you can right-click on the object (not the class) and select Invoke Method. After you successfully Compile your program, you have three ways to run your program in jGRASP: Run , Debug , and Run in Viewer Canvas . In this part of the activity, we focus on Run in Viewer Canvas , which opens a canvas window on a new or existing canvas file. When any primitive, object, or field of an object in the Debug or Workbench tabs is dragged onto the canvas, a viewer is opened using one of several viewers associated with the variable type. Below are the basic steps for creating a canvas for your program. Make sure that UserInfoDriver.java is in the edit window and that you have compiled it. Now you are ready to follow the steps below to create a viewer canvas for Activty4A. Running UserInfoDriver in the canvas and stopped at the first executable statement After user1 has been dragged onto the canvas Canvas with user1 and user2 On the canvas, click the Play button (auto step-in) on the canvas to start the visualization. Use the Pause button and Stop button as needed. To regulate the speed of the program, decrease or increase the delay between steps using the Delay slider.

of 10

When you play your program, you will be stepping into each of your methods. If it is going too fast for you to see and understand what is happening at each step, then increase the delay between steps by using the Delay slider. You can also click the Pause button . After you pause your program, you can step as you did above. Or if you want to step into a method at a statement that calls the method then you can click the Step-in button . You can also set one or more breakpoints (right-click on the line and select Toggle Breakpoint) in any of your methods and then Resume to the next breakpoint. The debugger will stop at the breakpoint, and you can examine the variables in the Debug tab or on the canvas. You can then step , step-in , or play your program as needed. You can always stop (or end) the program, and then start it again by clicking the Run in Canvas button followed by the Play button . It is important that you understand what your program is doing at each step. Although you can observe the behavior of your program using the debugger alone, the canvas works with the debugger to provide a more conceptual visualization of your program. When you are studying the example programs provided with the class notes, you are encouraged to run these in canvas mode. After you have added the variables of interest to your canvas and saved the canvas, you should be able to play or step through the program to understand the details of its behavior. Until are able to understand the example programs, it will be difficult for you to write your own programs for the project assignments. For the UserInfoDriver program above, you wrote the entire program before you created the canvas for it. However, you could have created the canvas as soon as you were able to compile and run part of the program. As you write you own programs for the project assignments, you can create a canvas as soon as there is any observable behavior. This will help you ensure that the program is correct at each increment during development. The canvas will be particularly useful when you are attempting to discover and correct errors in your program. Note that you can also use the canvas via the Debug or Workbench tabs by clicking the Open New Viewer Canvas button on the debug or workbench toolbar and then dragging one or more variables onto the canvas. Changes to these variables resulting from statements executing in the Interactions tab, from stepping the debugger, and/or from executing methods via the Invoke Method dialog will be reflected on the canvas. Usually, you will need only one canvas for your program. However, if you need more than one, you can open a new canvas window via the Debug tab drag variables onto it and save it as described above.