Question
I need help with making the code for this. OverviewThe purpose of this assignment is to give you some practice with the process of implementing
I need help with making the code for this.
OverviewThe purpose of this assignment is to give you some practice with the process of implementing a class from a specification and testing whether your implementation conforms to the specification.You'll also get a lot of practice with modular arithmetic.For this assignment you will implement one class, called TV, that models some aspects of the behavior of simple television. A TVhas a current channeland 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 startand numChannels. In general there will always be numChannelsconsecutive channels, the lowest of which is start; that is, the range of values isfrom startup to start + numChannels 1,inclusive.The volume is adjusted with methods volumeUpand volumeDown, but nevergoes 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 channelUpand channelDown, but never goes below startor above start + numChannels 1.The channel can also be set directly with thesetChannelmethod. The method goToPreviousChannelsets the channel to the most recent previous channel number. It is also possible to "reprogram" the TV to set a differentstart 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 ofplaces where you need to choose the larger or smallerof two numbers, which can be done with the methodsMath.min()or Math.min().Forthewrapping 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.)SpecificationThe specification for this asssignment includesthis pdf along with any "official" clarifications announcedon Canvas.There is one public constant:public static final double VOLUME_INCREMENT = 0.07;There is one public constructor:publicTV(intgivenStart, intgivenNumChannels)
Constructs a new TVwith channels givenStartthrough givenStart + givenNumChannels1.Initially the volume is 0.5and the channel is equal to givenStart.There are the following public methods:publicvoid channelDown()Changes the channel down by 1, wrapping around to start + numChannels-1if the current channel is start.publicvoid channelUp()Changes the channel up by 1, wrapping around to startif the current channel is start + numChannels1.publicStringdisplay()Returns a string representing the current channel and volume in the form "ChannelxVolumey%", where xis the current channel and yis the volume, multiplied by 100 and rounded to the nearest integer. For example, if the channel is 8and the volume is .765, this method returns the exact string "Channel 8Volume 77%".publicint getChannel()Returns the current channel.publicdouble getVolume()Returns the current volume.publicvoid goToPreviousChannel()Sets the current channel to the most recent previous channel.If no channel has ever been set for this TVusing one of the methods channelDown, channelUp, or setChannel, this method sets thechannel to start.publicvoid resetStart(int givenStart)Resets this TVso that its available channels are now fromgivenStartthrough givenStart+ numChannels1.Ifthe current channel orprevious channel isoutside the new range of channelnumbers, their values are adjusted to be the nearest valuewithin the new range.publicvoid resetNumChannels(int givenNumChannels)Resets this TVso that its available channels are now from startthrough start+ givenNumChannels1.Ifthe current channel orprevious channel isoutside the new range of channelnumbers, their values are adjusted to be the nearest valuewithin the new range.publicvoid 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 valueis less than start, it is set to start.publicvoid volumeDown()Lowers the volume by VOLUME_INCREMENT, but not below 0.0.publicvoid volumeUp()Raises the volume by VOLUME_INCREMENT, but not above 1.0.Where's the main() method??There isn't one! Like most Java classes, this isn't a complete program and you can't "run" it by itself. It's just a single class, that is, the definition for a type of object that might be part of a larger system. To try out your class, you can write a test class with a main method such the example below.There is also a specchecker (see below) that will perform a lot of functional tests, but when you are developing and debugging your code at first you'll always want to have some simple test cases of your own,as in the getting started section.Suggestions for getting startedSmart developers don't try to write all the code and then try to find dozens of errors allat once; they work incrementallyand test every new feature as it's written. Since this is our first assignment, here is an example ofsome incremental steps you could take in writing thisclass.0. Be sure you have done and understood Lab 2.1. Create a new, empty project and then add a package called hw1.Be sure to choose "Don't Create" at the dialog that asks whether you want to create module-info.java.2. Create the TVclass in the hw1package 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, constructorand methods. This is a required part of the assignmentanyway, 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 COPYAND PASTE from the pdf or online Javadoc!This leads to insidiousbugscaused 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 operationsgetVolume, volumeUp,and volumeDown. Start with some sample usage, that is,a very simpletest 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.5tv.volumeUp();tv.volumeUp();System.out.println(tv.getVolume()); // expected 0.64tv.volumeUp();tv.volumeUp();tv.volumeUp();tv.volumeUp();tv.volumeUp();tv.volumeUp();System.out.println(tv.getVolume()); // expected 1.0}}The presence of an accessormethodgetVolumesuggests 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 fromgoing over 1.0, you can just use the method Math.min, something like this:myVolume= Math.min(myVolume, 1.0);Write a similar testcasefor volumeDown.6. Nextyou might try getting and setting channel numbers. Start with a simple usage example:tv = new TV(2, 10);
System.out.println(tv.getChannel()); // expected 2tv.setChannel(8);System.out.println(tv.getChannel()); // expected 8tv.setChannel(42);System.out.println(tv.getChannel()); // expected 11tv.setChannel(1);System.out.println(tv.getChannel()); // expected 2Havinganaccessor method getChannelsuggests you need an instance variable storing your current channel number.ForsetChannel, notice that you can use Math.maxand Math.minto keep the channelfrom beingout of range. But that means the methodneedsto know the range, which implies that the constructor parameters for startand numChannelsneed 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 9tv.channelUp();System.out.println(tv.getChannel()); // expected 10tv.channelUp();System.out.println(tv.getChannel()); // expected 11tv.channelUp();System.out.println(tv.getChannel()); // expected 2Once the channel is incrementedpast 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. Supposeyou have ten values, 0 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 zeroSystem.out.println(x);
}}The only difference between the example above, and the range of channels,is that your channels begin at the startvalue, not necessarily at zero. So just subtract startfrom the channel, do the increment, and add start. (Or,you couldjust store the channel number internally as channel start, and add startin the method getChannel.)8.For channelDownthere is one tiny wrinkle, due to the fact that the Java % operator always gives a negative answer if one of theoperands is negative. That is, -1 % 10gives us -1, not 9, whichis what we want.This iseasy 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 9System.out.println(x); Write a short testcase 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= newTV(2, 10);tv.setChannel(8);tv.resetNumChannels(5);System.out.println(tv.getChannel()); // should now be 6Since the new range of channel values isonly2 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 lab1 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 firstin the output and will usually say "Class does not conform to specification." Always fix these first.The SpecCheckerYou can find the SpecChecker on Canvas. Import and run the SpecCheckerjust as you practiced in Lab1. It will run a number of functional tests and then bring up a dialog offeringto create a zip file to submit. Remember that error messages will appear in the consoleoutput. There are many test cases so there may be an overwhelming number of error messages. Always start reading the errors at the top and make incremental corrections in the code to fix them.When you are happy with yourresults, click "Yes" at the dialog tocreate the zip file. See the document SpecCheckerHOWTO, link #10 on our Canvas front page,if you are not sure what to do.More about gradingThis is a "regular" assignment so we are going to read your code. Your score will be based partly (about a third) on the specchecker's functional tests and partly on the TA'sassessment of the quality of your code. This means you can get partial credit even if you have errors, and it also means that even if you pass all the specchecker tests you can still lose points. Are you doing things in a simple and direct way that makes sense? Are you defining redundant instance variables? Are you using a loop for something that can be done with integer division?Some specific criteria that are important for this assignment are:Use instance variables only for the permanent state of the object, use local variables for temporary calculations within methods. oYou will lose points for having unnecessary instance variablesoAll instance variables should be private.Accessor methods should not modify instance variables.See the "Style and documentation" section below for additional guidelines.Style and documentationRoughly 15% of the points will be for documentation and code style. Hereare some general requirements and guidelines:
Each class, method, constructor and instance variable, whether public or private, must have a meaningful javadoc comment. The javadoc for the classitself can be very brief, but must include the @authortag. The javadoc for methodsmust include @paramand @returntags as appropriate. oTry 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 fromthis pdfor the online Javadoc! This leads to all kinds of weird bugs due to the potential forsophisticated document formatslike Wordand pdfto contain invisible characters.oRun 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 (i.e., named for the value they store).Your code should notbe producing console output. You may add printlnstatements when debugging, but you need to remove them before submitting the code.Internal (//-style) comments are normally used inside of method bodies to explain howsomething works, while the Javadoc comments explain whata method does. (A good rule of thumb is: if you had to think for a few minutesto figure out howsomething works, you should probably include an internalcomment explaining how it works.) oInternal comments always precedethe code they describe and are indented to the same level. In a simple homeworklike this one, as long as your code is straightforward and you use meaningful variable names, yourcode will probably not need any internal comments.Use a consistent style for indentation and formatting. oNote 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.
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