CS46A Spring 18

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.

5A draft:
5A final:

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.

5B draft:
5B final:

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

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:

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

5C draft:
5C final: