Answered step by step
Verified Expert Solution
Question
1 Approved Answer
SimpleTests.java TryOutModularArithmetic.java For this assignment you will implement one class, called TV, that models some aspects of the behavior of simple television. A TV has
SimpleTests.java
TryOutModularArithmetic.java
For this assignment you will implement one class, called TV, that models some aspects of the behavior of simple television. A TV has a current channel and volume. The volume level ranges from 0.0 to 1.0. The possible range of channels is determined by two parameters that we will refer to as start and numChannels. In general there will always be numChannels consecutive channels, the lowest of which is start; that is, the range of values is from start up to start + numChannels - 1, inclusive. The volume is adjusted with methods volume Up and volume Down, but never goes above 1.0 or below 0.0. The volume is incremented or decremented by the value of the constant VOLUME_INCREMENT. The channel is adjusted with methods channelUp and channelDown, but never goes below start or above start + numChannels - 1. The channel can also be set directly with the setChannel method. The method gotoPreviousChannel sets the channel to the most recent previous channel number. It is also possible to "reprogram" the TV to set a different start channel or number of channels. Please note that you do not need any conditional statements (i.e. "if" statements) or anything else we haven't covered, for this assignment. There will be a couple of places where you need to choose the larger or smaller of two numbers, which can be done with the methods Math.min() or Math.min(). For the wrapping behavior of the channel numbers, you can use the mod (%) operator. (You will not be directly penalized for using conditional statements, but your code will become longer and more complicated, and if it becomes overly complicated and hard to read, the TA will start taking off points.) Specification The specification for this asssignment includes this pdf along with any "official" clarifications announced on Canvas. There is one public constant: public static final double VOLUME_INCREMENT = 0.07; There is one public constructor: public TV (int givenStart, int givenNumChannels) Constructs a new TV with channels givenstart through givenstart + givenNumChannels - 1. Initially the volume is 0.5 and the channel is equal to givenstart. There are the following public methods: public void channelDown(). Changes the channel down by 1, wrapping around to start + numChannels - 1 if the current channel is start. public void channelUp () Changes the channel up by 1, wrapping around to start if the current channel is start + numChannels - 1. public String display ( ) Returns a string representing the current channel and volume in the form "Channel x Volume y", where x is the current channel and y is the volume, multiplied by 100 and rounded to the nearest integer. For example, if the channel is 8 and the volume is .765, this method returns the exact string "Channel 8 volume 778". public int getChannel() Returns the current channel. public double getVolume () Returns the current volume. public void goto PreviousChannel() 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 resetstart(int givenstart) 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 new range. public void resetNumChannels (int givenNumChannels) 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. public void setChannel (int channelNumber) 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. public void volume Down () Lowers the volume by VOLUME_INCREMENT, but not below 0.0. public void volumeUp () Raises the volume by VOLUME_INCREMENT, but not above 1.0. 1. Create a new, empty project and then add a package called hwl. Be sure to choose "Don't Create" at the dialog that asks whether you want to create module-info.java. 2. Create the tv class in the hwl package and put in stubs for all the required methods, the constructor, and the required constant. Remember that everything listed is declared public. For methods that are required to return a value, just put in a "dummy" return statement that returns zero. There should be no compile errors. 3. Briefly javadoc the class, constructor and methods. This is a required part of the assignment anyway, and doing it now will help clarify for you what each method is supposed to do before you begin the actual implementation Copying from the method descriptions here or in the Javadoc is perfectly acceptable; however. DO NOT COPY AND PASTE from the pdf or online Javadoc! This leads to insidious bugs caused by invisible characters that are sometimes generated by sophisticated document formats.) 4. Look at each method Mentally classify it as either an accessor (returns some information without modifying the object) or a mutator (modifies the object, usually returning void). The accessors will give you a lot of hints about what instance variables you need. 5. You might start with the volume operations getVolume, volumeUp, and volume Down. Start with some sample usage that is a very simple test case, such as this one: public class SimpleTests { public static void main(String[] args) { TV tv = new TV(0, 5); System.out.println(tv.getVolume()); // expected 0.5 tv.volumeUp(); tv.volumeUp(); System.out.println(tv.getVolume()); // expected 0.64 tv.volumeUp(); tv.volumeUp(); tv.volumeUp(); tv.volumeUp(); tv.volumeUp(); tv.volume Up(); System.out.println(tv.getVolume()); // expected 1.0 } } The presence of an accessor method getVolume suggests that you might need an instance variable to store the current volume. Remember that every time you define an instance variable. you should initialize it in the constructor. (Tip: you can find these sample test cases in the file SimpleTests.java which is posted along with the homework spec.) Notice that to keep the volume from going over 1.0, you can just use the method Math.min, something like this: myVolume = Math.min(myVolume, 1.0); Write a similar test case for volumeDown. 6. Next you might try getting and setting channel numbers. Start with a simple usage example: tv = new TV (2, 10); System.out.println(tv.getChannel()); // expected 2 tv.setChannel (8); System.out.println(tv.getChannel()); // expected 8 tv.setChannel (42); System.out.println(tv.getChannel()); // expected 11 tv.setChannel (1); System.out.println(tv.getChannel(); // expected 2 Having an accessor method getChannel suggests you need an instance variable storing your current channel number. For setChannel, notice that you can use Math.max and Math.min to keep the channel from being out of range. But that means the method needs to know the range, which implies that the constructor parameters for start and numChannels need to be stored in instance variables too. 7. Next you might think about channelUp. What is it supposed to do? tv = new TV (2, 10); tv.setChannel (8); tv.channelUp(); System.out.println(tv.getChannel()); // expected 9 tv.channelUp(); System.out.println(tv.getChannel()); // expected 10 tv.channelUp(); System.out.println(tv.getChannel()); // expected 11 tv.channelUp(); System.out.println(tv.getChannel()); 11 expected 2 Once the channel is incremented past the maximum channel number it's supposed to "wrap" back around to the start. If you are familiar with conditional statements, you may be tempted to implement this using conditionals, but that's really not necessary. It can be done simply using modular arithmetic. Suppose you have ten values. O through 9. Try this: public class TryoutModularArithmetic { public static void main(String[] args) { int numValues - 10; int x = 7; x = (x + 1) & numValues ; System.out.println(x); x = (x + 1) numValues ; System.out.println(x); x = (x + 1) numValues; // wraps around to zero System.out.println(x); } } The only difference between the example above and the range of channels, is that your channels begin at the start value, not necessarily at zero. So just subtract start from the channel, do the increment, and add start. Or, you could just store the channel number internally as channel - start, and add start in the method getChannel.) 8. For channelDown there is one tiny wrinkle, due to the fact that the Java % operator always gives a negative answer if one of the operands is negative. That is. -1 * 10 gives us -1, not 9, which is what we want. This is easy to work around by adding the number of channels each time you decrement, for example: x = 2; x = (x - 1 + numValues) numValues ; System.out.println(x); x = (x - 1 + numValues) numValues; System.out.println(x); x = (x - 1 + numvalues) numValues; // wraps around to 9 System.out.println(x); Write a short test case using a different range of channels and make sure you're getting the right answers. 9. For resetNumChannels, the interesting part is that you may have to update the current channel, as well as the previous channel, to make sure they are still in range. Start with a concrete test case to make this behavior clear: tv = new TV (2, 10); tv.setChannel (8); tv.resetNumChannels (5); System.out.println(tv.getChannel()); // should now be 6 Since the new range of channel values is only 2 through 6, the current channel value 8 is supposed to be reset to the nearest value that is within range in this case 6. (This is easy using Math.min.) 10. The behavior is similar for resetstart. Here, the current channel value 8 is reset to the nearest value within the new range 10 through 19. tv = new TV(2, 10); tv.setChannel (8); tv.resetStart (10) ; System.out.println(tv.getChannel()); // should now be 10 14. At some point download the SpecChecker, import it into your project as you did in lab 1 and run it. Always start reading error messages from the top. If you have a missing or extra public method, if the method names or declarations are incorrect, or if something is really wrong like the class having the incorrect name or package, any such errors will appear first in the output and will usually say "Class does not conform to specification" Always fix these first. Each class, method, constructor and instance variable, whether public or private, must have a meaningful javadoc comment. The javadoc for the class itself can be very brief, but must include the author tag. The javadoc for methods must include @param and @return tags as appropriate o Try to briefly state what each method does in your own words. However, there is no rule against copying the descriptions from the online documentation. However: do not literally copy and paste from this pdf or the online Javadoc! This leads to all kinds of weird bugs due to the potential for sophisticated document formats like Word and pdf to contain invisible characters. o Run the javadoc tool and see what your documentation looks like! (You do not have to turn in the generated html, but at least it provides some satisfaction :) All variable names must be meaningful (1.e. named for the value they store). Your code should not be producing console output. You may add printin statements when debugging, but you need to remove them before submitting the code. Internal (1/-style) comments are normally used inside of method bodies to explain how something works, while the Javadoc comments explain what a method does. (A good rule of thumb is: if you had to think for a few minutes to figure out how something works, you should probably include an internal comment explaining how it works.) o Internal comments always precede the code they describe and are indented to the same level In a simple homework like this one, as long as your code is straightforward and you use meaningful variable names, your code will probably not need any internal comments. Use a consistent style for indentation and formatting Note that you can set up Eclipse with the formatting style you prefer and then use Ctrl- Shift-F to format your code. To play with the formatting preferences, go to Window- >Preferences->Java->Code Style->Formatter and click the New button to create your own "profile for formatting Package hw1 Class TV java.lang. Object hw1.TV public class TV extends java.lang.Object Model of the channel and volume controls for a television. A TV has a current channel and volume level. The volume level ranges from 0.0 to 1.0. The possible range of channels is determined by two parameters that we will refer to as start and numChannels. In general there will always be numChannels consecutive channels, the lowest of which is start; that is, the range of values is from start through start + numChannels - 1, inclusive. The volume is adjusted with methods volumeUp and volume Down, but never goes above 1.0 or below o.o. The volume is incremented or decremented by the value of the constant VOLUME_INCREMENT. The channel is adjusted with methods channelDown and channelUp, but never goes below start or above start + numChannels - 1. The channel can also be set directly with the setChannel method. The method gotoPreviousChannel sets the channel to most recent previous channel number. Field Summary Fields Modifier and Type Field Description static double VOLUME_INCREMENT Incremental increase or decrease in volume when the volume Up or volume Down methods are called. Constructor Summary Constructors Constructor Description TV(int givenStart, int givenNumChannels) Constructs a new TV with givenNumChannels available channels, numbered consecutively beginning with givenStart. Method Summary All Methods Instance Methods Concrete Methods Modifier and Type Method Description void channelDown() Changes the channel down by 1, wrapping around to start + numChannels - 1 if the current channel is equal to start. Changes the channel up by 1, wrapping around to start if the current channel can't go any higher. void channelUp() java.lang.String display Returns a string representing the current channel and volume, where the volume is expressed as a percentage rounded to the nearest integer. int getChannel() Returns the current channel for this TV. double getVolume) Returns the current volume for this TV. void goToPreviousChannel() Sets the current channel to the most recent previous channel. void resetNumChannels(int givenNumChannels) Resets the number of channels. void resetStart(int givenStart) Resets the value of the start channel. void setChannel(int number) Sets the channel to the given channel number. void volumeDown() Lowers the volume by VOLUME_INCREMENT, but not below o.o. void volumeUp Raises the volume by VOLUME_INCREMENT, but not above 1.0. Methods inherited from class java.lang. Object equals, getClass, hashcode, notify, notifyAll, toString, wait, wait, wait Field Detail VOLUME_INCREMENT public static final double VOLUME_INCREMENT Incremental increase or decrease in volume when the volume Up or volume Down methods are called. See Also: Constant Field Values Constructor Detail TV public TV(int givenstart, int givenNumChannels) Constructs a new TV with givenNumChannels available channels, numbered consecutively beginning with givenStart. Initially the channel is givenStart and the volume is 0.5. Parameters: givenStart - minimum channel number givenNumChannels - number of channels Method Detail channelUp public void channelUp Changes the channel up by 1, wrapping around to start if the current channel can't go any higher. channelDown public void channelDown() Changes the channel down by 1, wrapping around to start + numChannels - 1 if the current channel is equal to start. volumeUp public void volumeUp() Raises the volume by VOLUME_INCREMENT, but not above 1.0. volume Down public void volume Down() Lowers the volume by VOLUME_INCREMENT, but not below o.o. setChannel public void setChannel(int number) Sets the channel to the given channel number. If the given value is greater than start + numChannels - 1, the channel is set to start + mumChannels -1; if the given value is less than start, the channel is set to start. Parameters: number-channel number to be set goToPreviousChannel public void gotoPreviousChannel) Sets the current channel to the most recent previous channel. If no channel has ever been set for this TV using channelUp, channelDown or setChannel, this method sets the current channel to start. getVolume public double getVolume) Returns the current volume for this TV. Returns: current volume getChannel public int getChannel() Returns the current channel for this TV. Returns: current channel display public java.lang.String display Returns a string representing the current channel and volume, where the volume is expressed as a percentage rounded to the nearest integer. For example, if the channel is 8 and the actual volume is 71.9, the method returns the exact string Channel 8 Volume 72% Returns: a string of the exact form "Channel x Volume y96", where x is the channel and y is the volume times 100, rounded to the nearest integer reset Start public void resetStart(int givenStart) Resets the value of the start channel. If necessary, the current channel and previous channel are adjusted to be the nearest value within the new range from start up through start + rumChannels - 1. Parameters: givenStart - the new starting channel resetNumChannels public void resetNumChannels(int givenNumChannels) Resets the number of channels. If necessary, the current channel and previous channel are adjusted to be the nearest value within the new range from start up through start + numChannels - 1. Parameters: givenNumChannels - the new number of channels Sportvi. try volume some basic usage examples for the TV Class / public class simpleTests public static void main(String[] ars) 12 TV tv - TV(@, 5). tv.vol: System.out.println(tv.getVolume()). // expected 0.5 two (v.gut Volun()). // expected 8.64 tv.volucoup(): tv.com tv.volume) tv.volume) tv.volup(). System.out.println(tv.gutVolume()). // expected 1.0 System.out.println(). 1/ try setting and setting channel tv - TV(2, 18). System.out.println(tv.getC10). // expected 2 tv.setChannel(s). System.out.println(tv.getC10). // expected 8 tv.setChanel(42) System.out.println(tv.gethe10). // expected 11 tv.setChanel(). System.out.printintv.getChanel()). // expected 2 System.out.println(). tv TV(2, 10) tv.setChanel(s). tv.channel(). System.out.println(tv.getChan10). // expected System.out.println(tv.get()). // expected 10 tv.chalO. System.out.println(tv.getChanel(). // expected 11 tv.chalup(). System.out.println(tv.getChanel()// expected 2 System.out.println(). tv.setChanel (4) System.out.printintv.getChanel()// expected tv.choo(). System.out.printinct.getChanel()// expected 2 tv.choo(. System.out.println(tv.get()). // expected 11 System.out.println(). tv - TV(2, 18). tv.setChanel(). tv. Macha(s). System.out.println(tv.getChan10). I should now be tv - TV(2, 10). tv.setChanel(a). tv.resetstart (18) System.out.println(tv.getChannel()It should now be 10 tv - TV(2, 18). tv.setChanel(). tv.netstart(.5). System.out.println(tv.getChal()). // should now be 4 /** * Experiment in using modular arithmetic to get the wrapping * behavior for the channelUp and channelDown methods. */ public class TryoutModularArithmetic { X = public static void main(String[] args) { int numValues = 10; int x = 7; x = (x + 1) % numValues; System.out.println(x); (x + 1) % numValues; System.out.println(x); x = (x + 1) % numValues; // wraps around to zero System.out.println(x); // for decrementing, we have to add 'numValues' each time to // ensure that we don't get a negative result x = 2; (x - 1 + numValues) % numValues; System.out.println(x); x = (x - 1 + numValues) % numValues; System.out.println(x); x = (x - 1 + numValues) % numValues; // wraps around to 9 System.out.println(x); } }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