Question
IN JAVA - I am encountering four problems/errors (in the picture below) which need fixing. Please revise my code, so the output is correct. I
IN JAVA - I am encountering four problems/errors (in the picture below) which need fixing. Please revise my code, so the output is correct. I will copy and paste the code as well. Thanks :)
Code:
package hw1;
/* * TV class that models some aspects of the behavior of simple television */ public class TV { // Constant for volume increment public static final double VOLUME_INCREMENT = 0.07;
// Instance variables private int currentChannel; private double currentVolume; private int start; private int numChannels; private int prevChannel;
/** * Constructs a new TV with channels givenStart through * givenStart + givenNumChannels - 1. Initially, the volume * is 0.5 and the channel is equal to givenStart. * @param givenStart * @param givenNumChannels */ public TV(int givenStart, int givenNumChannels) { this.start = givenStart; this.numChannels = givenNumChannels; this.currentChannel = givenStart; this.prevChannel = givenStart; this.currentVolume = 0.5; }
/** * Changes the channel down by 1, wrapping around to start + * numChannels - 1 if the current channel is start. */ public void channelDown(){ prevChannel = currentChannel; currentChannel = ((currentChannel - start - 1 + numChannels) % numChannels) + start; }
/** * Changes the channel up by 1, wrapping around to start * if the current channel is start + numChannels - 1. */ public void channelUp() { prevChannel = currentChannel; currentChannel = ((currentChannel - start + 1) % numChannels) + start; }
/** * Method to display a string containing * TV channel and volume. * @return TV channel and volume */ public String display() { return "Channel " + currentChannel + " Volume " + Math.round(currentVolume * 100) + "%"; }
/** * Method to return current channel. * @return currentChannel */ public int getChannel() { return this.currentChannel; }
/** * Method to return current volume. * @return currentVolume */ public double getVolume() { return this.currentVolume; }
/** * Sets the current channel to the most recent previous channel. * If no channel has ever been set for this TV using one of the * methods channelDown, channelUp, or setChannel, this method * sets the channel to start. */ public void goToPreviousChannel() { int temp = prevChannel; currentChannel = prevChannel; prevChannel = temp; }
/** * Resets this TV so that its available channels are now from * givenStart through givenStart + numChannels - 1. If the * current channel or previous channel is outside the new range * of channel numbers, their values are adjusted to be the nearest * value within the range. * @param givenStart */ public void resetStart(int givenStart) { start = givenStart; setChannel(currentChannel); }
/** * Resets this TV so that its available channels are now from start * through start + givenNumChannels - 1. If the current channel or * previous channel is outside the new range of channel numbers, their * values are adjusted to be the nearest value within the new range. * @param givenNumChannels */ public void resetNumChannels(int givenNumChannels) { numChannels = givenNumChannels; // adjust the current channel and previous channel currentChannel = Math.min(start + numChannels - 1, currentChannel); prevChannel = currentChannel; }
/** * Sets the channel to the given channel number. However, if * the given value is greater than start + numChannels - 1, * it is set to start + numChannels - 1, and if the given * value is less than start, it is set to start. * @param givenChannel */ public void setChannel(int givenChannel) { // get minimum of given channel and maximum possible channel in variable temp int temp = Math.min(start + numChannels - 1, givenChannel); // get maximum of temp and start temp = Math.max(temp, start); // set current and previous channel to temp currentChannel = temp; prevChannel = currentChannel; }
/** * Lowers the volume by VOLUME_INCREMENT, * but not below 0.0. */ public void volumeDown(){ currentVolume = Math.max(currentVolume - VOLUME_INCREMENT, 0.0); }
/** * Raises the volume by VOLUME_INCREMENT, * but not above 1.0. */ public void volumeUp(){ currentVolume = Math.min(currentVolume + VOLUME_INCREMENT, 1.0); } }
14 18 19 * 1 package hw1; 2 3- /* 4 * TV class that models some aspects of the behavior of simple television 5 */ 6 public class TV { 7 // Constant for volume increment 8 public static final double VOLUME_INCREMENT = 0.07; 9 10 // Instance variables 11 private int currentChannel; 12 private double currentVolume; 13 private int start; private int numChannels; 15 private int prevChannel; 16 170 /** * Constructs a new TV with channels givenStart through givenStart + givenNumChannels - 1. Initially, the volume * is 0.5 and the channel is equal to givenStart. @param givenStart 22 @param givenNumChannels 23 */ 240 public TV(int givenStart, int givenNumChannels) { 25 this.start = givenStart; 26 this.numChannels = givenNumChannels; 27 this.currentChannel = givenStart; 28 this.prevChannel = givenStart; 29 this.currentVolume = 0.5; 30 } 31 320 /** 33 * Changes the channel down by 1, wrapping around to start + * numChannels - 1 if the current channel is start. 35 36- public void channelDown(){ 37 prevChannel = currentChannel; 38 currentChannel ((currentChannel - start 1 + numChannels) % numChannels) + start; } 20 21 * * 34 39 40 410 42 43 44 450 46 47 48 /** * Changes the channel up by 1, wrapping around to start if the current channel is start + numChannels - 1. */ public void channelUp() { prevChannel = currentChannel; currentChannel = ((currentChannel - start + 1) % numChannels) + start; } 49 /** * Method to display a string containing * TV channel and volume. @return TV channel and volume public String display() { return "Channel " + currentChannel + } Volume 500 51 52 53 54 550 56 57 58 59 60 61 62 630 64 + Math.round(currentVolume * 100) + "%"; /** * Method to return current channel. * @return currentChannel public int getChannel() { return this.currentChannel; } 65 66 67- 68 69 70 710 72 73 74 /** * Method to return current volume. * @return currentVolume */ public double getVolume() { return this.currentVolume; } /** * Sets the current channel to the most recent previous channel. * If no channel has ever been set for this TV using one of the methods channelDown, channelUp, or setChannel, this method sets the channel to start. */ public void gotoPreviousChannel() { int temp = prevChannel; currentChannel = prevChannel; prevChannel = temp; } 750 76 77 78 79 80 810 82 83 84 85 86 87- 88 89 90 91 92 93 94 950 96 97 98 /** * Resets this TV so that its available channels are now from * givenStart through givenStart + numChannels - 1. If the * current channel or previous channel is outside the new range * of channel numbers, their values are adjusted to be the nearest * value within the range. @param given Start */ public void resetStart(int givenStart) { start = given Start; setChannel(currentChannel); } * 99 1000 101 102 103 104 105 106 107- 108 109 110 111 112 /** * Resets this TV so that its available channels are now from start * through start + givenNumChannels - 1. If the current channel or previous channel is outside the new range of channel numbers, their * values are adjusted to be the nearest value within the new range. * @param given NumChannels */ public void resetNumChannels(int givenNumChannels) { numChannels = givenNumChannels; // adjust the current channel and previous channel currentChannel = Math.min(start + numChannels - 1, currentChannel); prevChannel currentChannel; } /** * Sets the channel to the given channel number. However, if * the given value is greater than start + numChannels - 1, * it is set to start + numChannels - 1, and if the given * value is less than start, it is set to start. * @param givenChannel */ public void setChannel(int givenChannel) { // get minimum of given channel and maximum possible channel in variable temp int temp = Math.min(start + numChannels - 1, givenChannel); // get maximum of temp and start temp = Math.max(temp, start); // set current and previous channel to temp currentChannel = temp; prevChannel = currentChannel; } 1140 115 116 117 118 119 120 1210 122 123 124 125 126 127 128 129 130 1310 132 133 134 1350 136 137 138 1390 140 141 142 1430 144 145 146 } /** * Lowers the volume by VOLUME_INCREMENT, * but not below 0.0. */ public void volume Down(){ currentVolume = Math.max(currentVolume - VOLUME_INCREMENT, 0.0); } /** * Raises the volume by VOLUME_INCREMENT, * but not above 1.0. */ public void volumeUp() { currentVolume = Math.min(currentVolume + VOLUME_INCREMENT, 1.0); } PROBLEM: Message: For new TV(2, 100), after channelUp(), setChannel(42), and goToPreviousChannel(), getChannel should return 3 expected: but was: PROBLEM: Message: For new TV(2, 100), after setChannel(17), setChannel(42), and goToPreviousChannel(), getChannel should return 17 expected: but was: PROBLEM: Message: For new TV(2, 100), after setChannel(42), setChannel(17) and go ToPreviousChannel() 5 times, getChannel should return 42 expected: but was: PROBLEM: Message: For a new TV(2, 10), after channelUp(), setChannel(9), resetStart(4), and go ToPreviousChannel(), channel should be 4 expected: but was:Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started