In: Computer Science
JAVA, this is practice to better understand java.
//Please note throughout if possible looking to better understand
the process.
Write the following class: TV.
Define Class TV
Write the class header
Class Variables
Class TV is to have the following instance variables:
1.channel of type int
2. volumeLevel of type int
3. isOn of type boolean
Each data member is to have an access specifier of private.
Constructor
Class TV is to have a single, no-arg constructor that initializes
the instance variables to zero or false.
Get and Get Methods
Class TV has neither get( ) nor set( ) methods
Auxiliary Methods
In place of standard get/set methods, class TV will have the
following public auxiliary methods:
1. a void method turnOn( ), that takes no parameters and sets isOn
to true;
2. a void method turnoff( ), that takes no parameters and sets isOn
to false;
3. a void method setChannel( ) that takes an integer parameter
named newChannel and assigns the instance variable channel to the
parameter value;
4. a void method setVolume( ) that takes an integer parameter named
newVolumeLevel and assigns the instance variable volumeLevel to the
parameter value;
5. a void method channelUp( ) that takes no parameters and
increments the instance variable channel by one;
6. a void method channelDown( ) that takes no parameters and
decrements the instance variable channel by one;
7. a void method volumeUp( ) that takes no parameters and
increments the instance variable volume by one;
8. a void method volumeDown( ) that takes no parameters and
decrements the instance variable volume by one.
Method Overrides
Class TV is to override the Object equals( ) method to do the
following:
1. test to see if the parameter represents an object;
2. test to see if the parameter object is of the same class type as
the calling object;
3. determine if the calling object and the parameter object store
identical data values for the corresponding data members.
Class TV is to override the Object toString( ) method that does
the following:
1. displays the following information in the format
presented:
Current Channel: display channel data
Volume Level: display volumeLevel data
Power: display isOn data
TV.java
/*
* Class Name: TV
* Description: class to define a TV
* Author: Your name
*
*/
public class TV {
//data members
private int channel;
private int volumeLevel;
private boolean isOn;
//constructor
public TV() {
channel = 0;
volumeLevel = 0;
isOn = false;
}
//methods to turn on and off
public void turnOn() {
isOn = true;
}
public void turnOff() {
isOn = false;
}
//methods to set channel and volume
public void setChannel(int newChannel) {
channel = newChannel;
}
public void setVolume(int newVolumeLevel) {
volumeLevel = newVolumeLevel;
}
//methods to turn up and down the channel and
volume
public void channelUp() {
channel++;
}
public void channelDown() {
channel--;
}
public void volumeUp() {
volumeLevel++;
}
public void volumeDown() {
volumeLevel--;
}
//overriding equals method
@Override
public boolean equals(Object arg0) {
if(arg0 instanceof Object
&& arg0 instanceof TV && ((TV)arg0).channel ==
this.channel && ((TV)arg0).volumeLevel == this.volumeLevel
&& ((TV)arg0).isOn == this.isOn) {
return
true;
}
return false;
}
//overriding toString method
@Override
public String toString() {
String outString = "";
outString += "\nCurrent Channel: "
+ this.channel;
outString += "\nVolume Level: " +
this.volumeLevel;
outString += "\nPower: " +
this.isOn;
return outString;
}
}
Main.java
public class Main {
public static void main(String[] args) {
//initializing two TVs
TV tv1 = new TV();
TV tv2 = new TV();
//printing
System.out.println("\nTV1 : " +
tv1.toString());
System.out.println("\nTV2 : " +
tv2.toString());
//calling the method
tv1.turnOn();
tv1.setChannel(5);
tv1.channelUp();
tv1.setVolume(50);
tv1.volumeDown();
//printing data
System.out.println("\nTV1 : " +
tv1.toString());
tv2.turnOn();
tv2.setChannel(5);
tv2.channelUp();
tv2.setVolume(50);
tv2.volumeDown();
System.out.println("\nTV2 : " +
tv2.toString());
//checking if two TVs are
equal
System.out.println("\nTV1 equals
TV2?: " + tv1.equals(tv2));
//turning off the 2nd TV
tv2.turnOff();
System.out.println("\nTV2 : " +
tv2.toString());
//checking if two TVs are equal
now
System.out.println("\nTV1 equals
TV2?: " + tv1.equals(tv2));
}
}
Sample Output:
TV1 :
Current Channel: 0
Volume Level: 0
Power: false
TV2 :
Current Channel: 0
Volume Level: 0
Power: false
TV1 :
Current Channel: 6
Volume Level: 49
Power: true
TV2 :
Current Channel: 6
Volume Level: 49
Power: true
TV1 equals TV2?: true
TV2 :
Current Channel: 6
Volume Level: 49
Power: false
TV1 equals TV2?: false