5A
Write a class called NumberCompare which will ask the user for two numbers and print out coments 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 numbers (such as 3.5 -4.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 first number is equal to the second |
first is greater than second | The first number is greater than second |
first is less than second | The first number is less than second |
they have the same sign/different sign | The numbers have the same/different sign |
The numbers are within .0001 of each other | The numbers are very close |
The numbers are more that 10000 apart | The numbers are far apart |
If the numbers are equal only print the one message. Otherwise print all that apply. 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 structure and do not just use all ifs. You will need to use nested ifs.
For the draft, get the numbers from the user and print the appropriate one of the first 3 comments.
5B
The TownBus class models a bus driving down main street. The cross streets are numbere 1, 2, 3 ... 20 ( it is a small town). A newly constructed bus starts at 1 street heading toward 20 street. It can stop at any of the streets in between. When it gets to 1 street or to 20 sreet, it turns around. It can also turn around at any point in between if so instructed.
Provide the following methods
public void drive(int numberOfStops
) causes the bus to move the specified number of stops. If the specified number of stops would take the bus beyond 20 street, the bus will still stop at 20 street. At 1 street and at 20 street, the bus will turn arround public int getCurrentStop()
gets the current street where the bus is stoppedpublic void turn()
cause the bus to face the other direction. The bus does not move. It simply turns to face the other dirction so the text drive will take it in the new direction.public int getDirection()
gets the current direction the bus is moving (1 for toward 20 street, -1 for toward 1 street)The trick here is to keep an instance variable direction
. Let 1 be toward 20 street and -1 towards 1 street. You change direction by multiplying by -1. (1 * -1 = -1 while -1 * -1 = -1). In the drive method multiply the number of stops to move times the direction before adding to the current stop.
Define and use these constants for start and stop numbers of the streets.
public static final int FIRST_STOP = 1;
public static final int LAST_STOP = 20;
Provide javadoc for the class, all methods. and constructors
For the draft, implement the constructor, instance variable, and the methods getDirection
, turn
, and getCurrentStop
. Implement drive as a stub.
Here is a table with some examples
current stop number | number of stops to drive | direction | new direction | new stop number |
---|---|---|---|---|
10 | 5 | -1 | -1 | 10 + (5 * -1) = 5 |
10 | 5 | 1 | 1 | 10 + (5 * 1) = 15 |
5 | 7 | -1 | 1 | 5 + (7 * -1) = -2 but set the new stop to 1 and change direction |
19 | 3 | 1 | -1 | 19 + (3 *1) = 22 but set the new stop to 20 and change direction |
19 | 3 | -1 | -1 | 19 + (3 * -1) = 16 |
5C
A gym awards points to its members for various amounts of exercise and then the members are given a rank according to the total points they have earned. A member earns 5 points for every minute of aerobic execise and one point for every pound lifted.
Create a class called ExerciseRecord.
It has a constructor that takes the name of the member as a parameter. The number of point is initialized to 0.
Create methods
getName()
logAerobics(int minutes)
logWeights(int weight, int repetitions)
repetitions is the number of times the exercise is done. So with a weight of 100 and 10 repetitions, the total weight lifted is 1000getPoints()
getLevel()
returns the appropriate String, Beginner ... ExpertPoints | Level |
---|---|
0 to 50,000 | Beginner |
50,001 to 200,000 | Intermediate |
200,001to 500,000 | Advanced |
above 500,000 | Expert |
Copy and paste the levels so you it is the way Codecheck expects
Define and use constants for the levels. For example
public static final int BEGINNER_LEVEL = 50000;
Also define and use constants for points per minute and points per pound, like this
public static int final POINTS_PER_MINUTE = 5;
Provide Javadoc.
For the draft: define the instance variables, implement the constructor, and implement logAerobics()
and getPoints()
methods. Implement the other methods as stubs