Java



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 objectscircle1circle2, 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.
  1. 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 andnumberOfObjects). You have to use the get and set methods to access these private variables.
  2. 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 radiusareaandnumberOfObjects.
Save all files in the same folder.

Exercise 5:
  1. Type and compile, and run listing 9.11, page 352 (file TotalArea.java). Notice that you are working with an array of circle objects.
  2. Now, modify the program by adding a new method that takes the array of circles and determines the smallest and largest circles in the array and prints out their information. Use the following header for this method:
Public static void minMax(CircleWithPrivateDataFields[] circleArray) The method should print out:
      The smallest circle has radius = 3
      The largest circle has radius  = 150
Save all files in the same folder.
Exercise 6: Following the instructions in the problem statement, design and implement a Java program for programming exercise 9.2, page 360 (name it Stock.java). Next, develop a test program in a separate file (call it TestStock.java) to create object ORCL as stated in the problem statement.
Now, modify the test program to create 2 more stock objects (say MS and Google) and test all class methods on these 2 objects in logical order and display meaningful information after each method call. Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.
Exercise 7: Following the instructions in the problem statement, design and implement a Java program for programming exercise 9.10, page 363 (name it QuadraticEquation.java). Next, develop a test program in a separate file (call it TestQuadraticEquation.java) as instructed in the problem statement.