5A
Write a class called TwoNumbers which will ask the user for two doubles and print out comments based on their values. (no starter file. You will write all the code)
Use this exact prompt to get input:
System.out.print("Enter two doubles (like this: 41.7 -22.5): ");
The user will enter both numbers on the same line at the same time. This is possible because the first call to nextDouble will skip any leading whitespaces and then read up to the next whitespace (the space), leaving that whitespace in the input stream. The second call to nextDouble will ignore leading whitespaces and then read up to the next whitespace (the newline) leaving the newline in the input stream. This is a handy way to request multiple inputs.
Here are the conditions and the comments to print.
| Condition | Comment |
|---|---|
| first is equal to second | The numbers are equal |
| first is greater than second | The first number is larger |
| second number is greater than the first | The second number is larger |
| the first number is equal to an integer | The first number is an integer/The first number is an not integer There is a typo in codecheck. You need to make the same typo to pass. (2/27) (47.0 would be an integer 47.3 would not) You will need to cast |
| the second number is negative | The second number is negative |
Copy and paste the prompt and the replies. That way you will have exactly what Codecheck expects. Print the comments in the order given above
Use the appropriate if structures
Note for advanced students: Do not use System.exit().Codecheck will not be happy.
For the draft, get the numbers from the user and print the appropriate one of the first 3 comments.
5B
We do not want users of our classes to be able to set invalid values for instance variables. Now that you know how to use if statements, you are going to add error checking to a few classes from earlier homeworks. You can copy correct solutions from Canvas Modules if you need to..
You should not print error messages from constructors or methods of object. In the real world, you might throw an exception when the parameter is invalid, but we have not covered exceptions yet. We will just be sure the data in our objects are valid. Follow the directions below to add error checking to each class.
Use proper if structure. Do not do unnecessary tests . Do not have empty if or else blocks
Hal9000 (hw3)
SodaCan (hw4)
ElecticCar (hw3)
You can use your version of the solutions if they are correct or you can use the versions in Codecheck
For the draft, just make the changes to Hal9000.
5C
A train moves along a track from the start to the end . There are 10 stops. (Define a constant) The train can either move toward the end (+1 direction) or towards the start (-1 direction). The train moves by using power units of fuel. It takes one power unit of fuel to move the train one stop. The train tank can hold a maximum of 10 power units (Define a constant).
Complete the Train class that models this behavior.
Train class has a no-argument constructor that sets the train at the start (FIRST_STOP_NUMBER) moving toward the end (+1 direction FORWARD). You can think of this as a number line with only positive numbers and zero. The Train has two (2) power units when constructed. (Another constant.)
Train has these methods
public void move(int numberOfStops) - moves the Train the specified number of stops in the current direction. If there are sufficient power units to move the specified number of stops, the method will change the train's position by the specified number of stops and reduce the number of power units. If there are not enough power units to move the specified number of stops, the train moves as many as it can using all the power units available. In any case, the train can not move beyond the end or the start of the track. If the train is at stop 8 and receives a command to move 5, it will go to stop 10 not 13. But it still uses fuel in trying to move, so it will use 5 power units.public void turn() - reverses the direction of the train. If the train was moving from start to end (+1 direction), after the method executes, the train will be moving toward the start (-1 direction) Remember to use the REVERSE constant and not magic numbers.public void addFuel() adds 6 power units of fuel to the train's tank (define a class constant.) up to a maximum of 10 (another constant). So if the tank has 7 power units and we add fuel, the tank will have 10 power units, not 13. public String getDirection()returns the direction the train is moving as a String - either "Forward" or "Reverse"public int getPowerUnits() the number of power units in the train's tank.public int getStop() gets the current stop (1 through 10).HINT: You can represent the direction with as an integer: +1 if the train is pointed toward the end. -1 if it is pointed towards the start. Multiplying direction by -1 will change the direction.
You need to define and use class constants instead of magic numbers. Make them public class constants by defining them as public, static, and final. There should not be any numbers except 0 in any method, for this problem. Define and use these constants:
FIRST_STOP_NUMBER (1)LAST_STOP_NUMBER (10)FORWARD ( 1: you will initialize direction with this constant)REVERSE ( -1: use this instead of the literal -1 to reverse direction)POWER_UNITS_PER_ADD_FUEL (6)MAX_POWER_UNITS (10)INITIAL_POWER_UNITS (2)Include Javadoc for the class, constructor and each method.
For the draft, implement the constructor and all the methods but do not do error checking yet. No need for ifs. But do define and use the constants