Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
CS 1301 
Lab 12 
 
Working with Classes and Objects – Chapter 9 
 
 
Use JGrasp to design and implement the following programs provided in Chapter 9 in the textbook. Understand 
what they do. 
 
 
Exercise 1:  
 
1. Type, compile, and run listing 9.1, page 324 (file TestSimpleCircle.java). Notice that this program has 
2 classes in the same file (Class TestSimpleCircle and Class SimpleCircle). Understand how 
objects circle1, circle2, and circle3 are created and manipulated. 
 
2. Now, type, compile, and run listing 9.2, page 326 (file SimpleCircle.java). Notice that the main method 
of this class is acting as the test class in listing 9.1. Next, make some changes to this class to create 2 more 
objects, say circle4 and circle5. Print out the radius and area of each circle object similar to other 
circle objects. 
 
 
Exercise 2:  
 
1. Type and compile listing 9.3, page 327 (file TV.java).  Notice that this is a class with no main method, you 
cannot run it. 
 
2. Now, type, compile, and run listing 9.4, page 328 (file TestTV.java). Next, make some changes to this 
class to create another object, say tv3. Invoke all methods of class TV on this new object in the logical 
order and print out a meaningful message after each method call indicating the change to the object. (This is 
know as testing the class methods on an object). 
 
Save files TV.java and TestTV.java in the same folder. 
 
 
Exercise 3:  
 
1. Type and compile listing 9.6, page 338 (file CircleWithStaticMembers.java).  Notice that this is a 
class with no main method, you cannot run it. 
 
2. Type, compile, and run listing 9.7, page 339 (file TestCircleWithStaticMembers.java).  Observe the 
outputs and understand the displayed values. Notice that Listing 9.6 uses the static variable 
numberOfObjects. The value of this variable is modified through different circle objects. 
 
3. Next, modify listing 9.7 to create two more circle objects, say c3 and c4. Modify the radius of object c3 to 
20 and the radius of object c4 to 100.  Then display the new objects (c3 and c4) by extending the code on 
lines 24 through 30. Again, notice the changes to the static variable value. 
 
Save all files in the same folder. 
 
 
Exercise 4:  
 
1. Type and compile listing 9.8, page 345 (file CircleWithPrivateDataFields.java). Notice that this is a 
class with no main method, you cannot run it. 
 
 
Page 1 of 2 
 
Page 2 of 2 
2. Type, compile, and run listing 9.9, page 346 (file TestCircleWithPrivateDataFields.java). Observe 
the outputs and understand the displayed values. This program uses private variables (radius and 
numberOfObjects). You have to use the get and set methods to access these private variables.  
 
3. Next, modify the code in listing 9.9 to create another circle object, call it yourCircle with radius 40, display 
its radius and area, and then increase its radius by 50% and display the radius and area once again. Notice 
the syntax difference when accessing (displaying) the value of variables radius, area, and 
numberOfObjects. 
 
Save all files in the same folder. 
 
 
 
Instructions: 
 
1.  Programs must be working correctly. 
2.  Programs must be completed and checked before working assignment #12. 
3.  Programs must be checked by the end of the designated lab session.