Question: Working on a TV code in Java, where I created a TV class file. I have trouble trying to output my power and mute function
Working on a TV code in Java, where I created a TV class file. I have trouble trying to output my power and mute function as on and off in my main function in the print statement. Please help me with trying to get my output correctly - need my power to be on or off and mute to be on or off in the print statment in the output instead of true/false. Output is seen below. Only included parts of my code.
OUTPUT:
run: Check out the TV = [Brand = Samsung, Model Number: 1x2d3e, Volume: 5, Channel 3, Power is true, Mute: false] //NEED MY OUTPUT TO BE "POWER IS ON" and "MUTE IS ON" Check out the TV = [Brand = LGTV, Model Number: 1dfb7349, Volume: 5, Channel 3, Power is true, Mute: false]//NEED MY OUTPUT TO BE "POWER IS false" and "MUTE IS false" The brand is: vizio The channel is: 9
TV CLASS FILE
/* package modeltv;
*/ public class Tv { //private instance variables private String brand; private String model; private boolean power; private int channel = 2; private int volume = 4 ; //volume set to approximately 20% of the max allowed value private boolean mute; private int mutedVolume = muteOn(); // returns volume to previous volume.
//Properties: brand, model, channel, volume, power //overloaded constructors public Tv(){ //1st constructor this.brand = ""; this.model = ""; this.power = false; this.channel = 2; this.volume = 0; this.mute = true; } public Tv(String brand){ //2nd constructor this.brand = ""; this.model = ""; this.power = false; this.channel = 2; this.volume = 0; this.mute = true;
} public Tv(String brand, String model){ //3rd constructor this.brand = ""; this.model = ""; this.power = false; this.channel = 2; this.volume = 0; this.mute = true;
} public Tv(String brand, String model, boolean power){ //4th constructor this.brand = ""; this.model = ""; this.power = false; this.channel = 2; this.volume = 0; this.mute = true;
} public Tv(String brand, String model, boolean power, int channel){ //5th constructor this.brand = ""; this.model = ""; this.power = false; this.channel = 2; this.volume = 0; this.mute = true;
} public Tv(String brand, String model, boolean power, int channel, int volume){ //6th constructor this.brand = ""; this.model = ""; this.power = false; this.channel = 2; this.volume = 0; this.mute = true;
} public Tv(String brand, String model, boolean power, int channel, int volume, boolean mute){ //7th constructor this.brand = ""; this.model = ""; this.power = false; this.channel = 2; this.volume = 0; this.mute = true; } public Tv(String brand, String model,int volume, int channel,boolean power,boolean mute){ //8th constructor) this.brand = brand; this.model = model; this.power = power; this.channel = channel; this.volume = volume; this.mute = mute; } //Methods: Power On/Off, Channel Up/Down, Set channel, volume up/down, mute on/off public void powerOn(){ //turn on TV power = true; System.out.print("ON"); } public void powerOff(){ //turn off TV power = false; System.out.print("OFF");
} public void volumeUp(){ //increase volume if (power && volume < 20 && mute == false) volume++; } public void volumeDown(){ //decrease volume if (power && volume > 1 && mute == false) volume--; } public void setNewVolume(int newVolume) {
if (power && newVolume >= 1 && newVolume <= 20) { volume = newVolume; } else { System.out.println("Please select volume between 1 and 20."); } } public int muteOn (){ //mute on mutedVolume = volume; if (power && volume > 1){ mute = true; volume = 0; System.out.print("ON");
} return mutedVolume; } public void muteOff (){ ////mute off if (power && mute == true) //tried to turn mute off if mute was on mute = false; volume = mutedVolume; System.out.print("OFF");
} @Override public String toString() { return "Check out the TV = [" + "Brand = " + brand + ", Model Number: " + model + ", Volume: " + volume + ", Channel " + channel + ", Power is " + power + ", Mute: " + mute + "]"; } }
(MAIN)
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package modeltv;
public class ModelTv {
public static void main(String[] args) { //Test constructor and toString() Tv t1 = new Tv ("Samsung", "1x2d3e", 5, 3, true, false); System.out.println(t1.toString()); Tv t2 = new Tv("LGTV", "1dfb7349", 5, 3, true, false); System.out.println(t2.toString()); //Test setters and getters t1.setBrand("vizio"); t1.setChannel(9); System.out.println("The brand is: " + t1.getBrand()); System.out.println("The channel is: " + t1.getChannel());
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
