Question: Program 1: 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
Program 1:
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 known as testing the class methods on an object).
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); } }
Submit files TV.java and TestTV.java in the same Assignment Submission folder.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
