Question: 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
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.
public class TV { int channel = 1; // Default channel is 1 int volumeLevel = 1; // Default volume level is 1 boolean on = false; // TV is off
public TV() { }
public void turnOn() { on = true; }
public void turnOff() { on = false; }
public void setChannel(int newChannel) { if (on && newChannel >= 1 && newChannel <= 120) channel = newChannel; }
public void setVolume(int newVolumeLevel) { if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7) volumeLevel = newVolumeLevel; }
public void channelUp() { if (on && channel < 120) channel++; }
public void channelDown() { if (on && channel > 1) channel--; }
public void volumeUp() { if (on && volumeLevel < 7) volumeLevel++; }
public void volumeDown() { if (on && volumeLevel > 1) volumeLevel--; } }
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.
public class TestTV { public static void main(String[] args) { TV tv1 = new TV(); tv1.turnOn(); tv1.setChannel(30); tv1.setVolume(3);
TV tv2 = new TV(); tv2.turnOn(); tv2.channelUp(); tv2.channelUp(); tv2.volumeUp();
System.out.println("tv1's channel is " + tv1.channel+ " and volume level is " + tv1.volumeLevel); System.out.println("tv2's channel is " + tv2.channel+ " and volume level is " + tv2.volumeLevel); } }
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.
public class CircleWithPrivateDataFields { /** The radius of the circle */ private double radius = 1;
/** The number of objects created */ private static int numberOfObjects = 0;
/** Construct a circle with radius 1 */ public CircleWithPrivateDataFields() { numberOfObjects++; }
/** Construct a circle with a specified radius */ public CircleWithPrivateDataFields(double newRadius) { radius = newRadius; numberOfObjects++; }
/** Return radius */ public double getRadius() { return radius; }
/** Set a new radius */ public void setRadius(double newRadius) { radius = (newRadius >= 0) ? newRadius : 0; }
/** Return numberOfObjects */ public static int getNumberOfObjects() { return numberOfObjects; }
/** Return the area of this circle */ public double getArea() { return radius * radius * Math.PI; } }
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.
public class TestCircleWithPrivateDataFields { /** Main method */ public static void main(String[] args) { // Create a circle with radius 5.0 CircleWithPrivateDataFields myCircle = new CircleWithPrivateDataFields(5.0); System.out.println("The area of the circle of radius "+ myCircle.getRadius() + " is " + myCircle.getArea());
// Increase myCircle's radius by 10% myCircle.setRadius(myCircle.getRadius() * 1.1); System.out.println("The area of the circle of radius " + myCircle.getRadius() + " is " + myCircle.getArea());
System.out.println("The number of objects created is " + CircleWithPrivateDataFields.getNumberOfObjects()); } }
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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
