Question: Java 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
Java
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 known as testing the class methods on an object).
LISTING 9.4 TestTV.java 1 public class TestTV { 2 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);
}
}
tv1's channel is 30 and volume level is 3 tv2's channel is 3 and volume level is 2
The program creates two objects in lines 3 and 8 and invokes the methods on the objects to perform actions for setting channels and volume levels and for increasing channels and vol- umes. The program displays the state of the objects in lines 1417. The methods are invoked using syntax such as tv1.turnOn() (line 4). The data fields are accessed using syntax such as tv1.channel (line 14).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
