Question
I am encountering four problems/errors (listed below the code) which need fixing. Could you identify and solve the issue? Thanks! Code: package hw1; /* *
I am encountering four problems/errors (listed below the code) which need fixing. Could you identify and solve the issue? 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);
}
}
--------------------------------------------------------------------------------
PROBLEM:
Message: For new TV(2, 100), after channelUp(), setChannel(42),
and goToPreviousChannel(), getChannel should return
3 expected:<3> but was:<42>
PROBLEM:
Message: For new TV(2, 100), after setChannel(17), setChannel(42),
and goToPreviousChannel(), getChannel should return
17 expected:<17> but was:<42>
PROBLEM:
Message: For new TV(2, 100), after setChannel(42), setChannel(17)
and goToPreviousChannel() 5 times, getChannel should
return 42 expected:<42> but was:<17>
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