Question
JAVA - I am receiving one problem/error (listed at bottom). Could you revise my code and fix this error. Thanks! Code: package hw1; /* *
JAVA - I am receiving one problem/error (listed at bottom). Could you revise my code and fix this error. 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; prevChannel = currentChannel; currentChannel = 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);
prevChannel = currentChannel; currentChannel = temp; }
/** * 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); } }
--------------------------------------------------------------------------------------
Problem:
PROBLEM: Message: For a new TV(2, 10), after channelUp(), setChannel(9), resetStart(4), and goToPreviousChannel(), channel should be 4 expected:<4> but was:<9>
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