PDA

View Full Version : Ask java questions free


SioneFangaiuiha
Oct 1, 2014, 07:04 AM
I have a lab exercise that I have no idea how to do so please help me. So my professor has given the class two class source codes and we have to write another class which will corporate with one another.

Here are the instructions;

The ExerciseWrite a Java class that is named RemoteControl, which represents a television remote control. Such an object keeps track of both the current channel being viewed and the channel immediately previously viewed. This class contains only the following public methods (you may add as many private methods and/or attributes as you see fit to).

• A constructor that takes one integer argument. The argument represents the number of (contiguous) channels, starting at 0. Both the current channel and the previous channel should initially be 0 as a result of using this constructor.

• A method named clickUp that should take no arguments and should increase the current channel number by 1, wrapping around when it reaches the largest channel number (which would be one less than the number of channels). Note that this should also likely update the previous channel.

• A method named clickDown that should take no arguments and should decrease the current channel number by 1, wrapping around when it reaches the smallest channel number (which would be 0). Note that this should also likely update the previous channel.

• A method named setChannel that should take one integer argument and sets the current channel to that integer argument. Note that this should also likely update the previous channel.

• A method named getChannel that should take no arguments and returns an integer representing the current channel number.

• A method named getPreviousChannel that should take no arguments and returns an integer representing the previous channel number.This exercise is about how you get it done, not just “getting it done by any means possible”.Failing to follow the notes below will mean that you will fail this lab exercise.NOTES:

• You may not use any form of selective execution (if, switch, etc... ) to complete this problem.• You may not use any form of iterative execution (while, for, do-while, etc... ) to complete this problem.

• You may not use any form of formatted output (printf, DecimalFormat, etc... ) to complete this problem.

• DO NOT PROVIDE A main METHOD as part of your RemoteControl class. Several potential main methods (each in their own class) will be provided to you on Blackboard. All of them should work if you implement the RemoteControl class properly (i.e. everything will work if you follow the above directions exactly).

here are the two codes the professor gave us;

public class RemoteControlDriver1 { public static void main(String[] args) { // build two remote controls RemoteControl myRemote=new RemoteControl(250); RemoteControl otherRemote=new RemoteControl(100); // set the channel to 35 myRemote.setChannel(35); System.out.println("set to 35 EXPECTED:"+35); System.out.println("set to 35 ACTUALLY:" +myRemote.getChannel()); System.out.println(); // set the other remote's channel to 56... otherRemote.setChannel(56); System.out.println("set to 56 EXPECTED:"+56); System.out.println("set to 56 ACTUALLY:" +otherRemote.getChannel()); System.out.println(); //... and make sure that the right one changed (and // the wrong one did not change) System.out.println("original EXPECTED:"+35); System.out.println("original ACTUALLY:" +myRemote.getChannel()); System.out.println(); // click up a few times. myRemote.clickUp(); myRemote.clickUp(); System.out.println("two ups EXPECTED:"+37); System.out.println("two ups ACTUALLY:" +myRemote.getChannel()); System.out.println(); // click down a few times. otherRemote.clickDown(); otherRemote.clickDown(); otherRemote.clickDown(); System.out.println("three downs EXPECTED:"+53); System.out.println("three downs ACTUALLY:" +otherRemote.getChannel()); System.out.println(); // jump to a chaneel, and check what previous channel holds myRemote.setChannel(212); System.out.println("current EXPECTED:"+212); System.out.println("current ACTUALLY:" +myRemote.getChannel()); System.out.println("previous EXPECTED:"+37); System.out.println("previous ACTUALLY:" +myRemote.getPreviousChannel()); System.out.println(); // click down below 0, and see what happens. myRemote.setChannel(1); myRemote.clickDown(); myRemote.clickDown(); myRemote.clickDown(); System.out.println("current EXPECTED:"+248); System.out.println("current ACTUALLY:" +myRemote.getChannel()); } }

and the second is

import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * * @author Stephen Blythe * @date 9/2014 * * No, you're not supposed to understand this code. * If you code the RemoteControl class properly, * this will work properly. If you think you * need to understand this code to get it to work, * then YOU HAVE MISSED THE POINT OF THE LAB.If * this is the case, you need to re-read the ENTIRE * lab exercise. * */public class GUIRC extends JFrame{ static final long serialVersionUID=666; //Dr. Blythe is Pure Evil! Private RemoteControl controller; private JPanel top; private JPanel bottom; JLabel chNumber; public GUIRC() { super("TV Remote Control"); controller = new RemoteControl(100); // create our up/down buttons JButton downButton = new JButton(""); // add the listener to the jbutton to handle the "down" event downButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { controller.clickDown(); setLabel(); } }); // add the listener to the jbutton to handle the "up" event upButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { controller.clickUp(); setLabel(); } }); // add up/down, curr channel. getContentPane().setLayout(new BorderLayout() ); top=new JPanel(); top.add(downButton, BorderLayout.WEST); chNumber =new JLabel(); setLabel(); top.add(chNumber, BorderLayout.CENTER); top.add(upButton, BorderLayout.EAST); add(top,BorderLayout.NORTH); JTextField goBox = new JTextField(2); goBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JTextField source = (JTextField) e.getSource(); String contents=source.getText(); int requestedChannel = Integer.parseInt(contents)%100; controller.setChannel(requestedChannel); source.setText(""+requestedChannel); setLabel(); } }); bottom=new JPanel(); bottom.add(goBox, BorderLayout.EAST); add(bottom,BorderLayout.SOUTH); // set up the main JFrame, then display it setDefaultCloseOperation(WindowConstants.EXIT_ON_C LOSE); //setPreferredSize(new Dimension(170, 50)); pack(); setLocationRelativeTo(null); setVisible(true); } public void setLabel() { String base=""; if (controller.getChannel()

CravenMorhead
Oct 1, 2014, 08:09 AM
You need a class with three variables: (optimal solution)
int currentChannel;
int previousChannel;
int maxNumberChannels;

Six methods
constructor: RemoteControl(int maxChannels) // initialize maxNumberChannels to maxChannels, the other two to zero.
ClickUp() // Set previousChannel to currentChannel; add 1 to current channel. Use the modulo function to make sure you're still in range. (ie, currentChannel = currentChannel%maxNumberChannel;)
ClickDown() // Set previousChannel to currentChannel;This one is trickier because you don't want to go negative, so you have to add the maxNumberChannel to currentChannel and subtract 1, then take the modulo of the result like you do above. (currentChannel = (currentChannel+maxnumberChannels -1)%maxNumberChannels.) so if you have 100 channels, and you're at channel zero, you'll roll over to 99.
--The rest are easy --
GetChannel(){return currentChannel;}
GetPreviousChannel(){return previousChannel;}
SetChannel(int newChannel) // Set previousChannel to currentChannel; set currentChannel to new channel.
-------------------

Plug this into the code provided and it should work easily. A note about these types of assignments, they're REALLY relevant to what is being taught. I am guess math operators and how you streamline code using math operators. Look at what is going on and is going to be going on in class and you'll probably figure out the assignments. I could have tapped out this class in about 10min. I haven't wrote java in 14 years.