5A

You are going to modify the Train class from hw3b. You may use your own solution to hw3b or you can use the starter code in CodeCheck

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. Define and use these constants:

Now we want to take into account the fuel required to move along the track. It takes one power unit of fuel to move the train one stop. If there are sufficient powerUnits to move the specified number of stops, the move 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 the number of power units available.

When a Train is constructed, it has zero (0) power units.

The addFuel method will add 4 power units (define a class constant.) of fuel to the train's tank. The train tank can hold a maximum of 8 power units (define a constant). So if the tank has 7 power units and we add fuel, the tank will have 8 power units, not 11. You will need to define and use additional constants:

Add a method, getPowerUnits to return the number of power units in the train's tank.

For the draft, define and use the constants in place of literals. You will need to modify some of the methods to use the new constants. There should not be any numbers except 0 in any method, for this problem.

5A draft:
5A final:

5B

You are going to model a traffic light. Visually a TrafficLight is composed of a rectangle containing 3 circles. Two circles will be gray; one will be colored. The colored circle represents the light that is currently "lit." The TrafficLight maintains a state that tells which light is currently "lit"

TrafficLight has a constructor that takes (x, y) coordinates of the upper left hand corner of the bounding rectangle of the top light. In addition to setting x and y, the constructor will set the state of the light so that the currently lit light will be red.

TrafficLight has a method, changeLight which will change the internal state to indicate that the next light in the TrafficLight is lit. The lit light cycles in this order: red, yellow, green then red again. For example if the light is currently red, a call to changeLight will make the light yellow. If the light is currently yellow, a call to changeLight will make current light green. If the light is currently green, a call to changeLight will make the light red. Notice that the changeLight method does not draw the light.

There is a getLight method that returns the light that is currently lit as string "red", "yellow", or "green"

There is a draw method which display the TrafficLight at the (x,y) location. The currently lit light will be shown as its color and the other two as gray. Use the built-in colors Color.RED, Color.YELLOW, Color.GREEN and Color.GRAY. Use the fill() method (not the draw) The circles are 30 pixels in diameter. (Define and use a constant). The upper-left hand corner of the rectangle is the same as the upper-left hand corner of the top circle. Fill the rectangle with Color.BLACK. The lights touch the sides of the rectangle and the other circles. The draw mathod takes no parameters. You will need to import the graphics package

Only use the numbers 0, 1, 2, and 30 ONE time in your program. No magic numbers. (That means you will need to define class constants.)

Here is how to approach the problem. First define constants

public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;

Also make the diameter a constant

You will have an instance variable currentLight (you can call it anything). If the light is red, currentLight = RED (which is 0) The changeLight mehtod will add 1 to the currentLight so that if it is currently RED (0), after the method call it will be YELLOW (1). If the currentLight is YELLOW (1) the method call adds 1 making it GREEN (2). If the currentLight is GREEN (2) adding 1 will make it 3 which is too big. It should be 0. But if you take currentLight modulo (%) 3 you will get 0 which is just what you want (RED)

Here are the 3 possible states of the TrafficLight

screen shot

For the draft, define the constants (4 of them), the instance variables, the constructor, and the getLight method. Implement changeLight method as stubs. Implement the draw method so it draws the TrafficLight with all the circles in gray.

For the final you will implement the getLight and changeLight and modify the draw method to fill the appropriate circle with the appropriate color.

For the getLight method use if, else if, else since the choices are mutally exclusive

5B draft:
5B final:

5C

Ask the user for his name (this may be one name like Aruna or two names like Billy Bob). Then ask the user for his age. (If you don't get the input in this order, you may have trouble). Use System.out.print to display the prompts

Prompts:

Then print the person's name followed by one of the following statements, depending on the age entered. You will only print one statement.

Age Statement Example
16 or younger you are still quite young Mary Ann you are still quite young
older than 16 but less than 35 you are at that in between age Yilan you are at that in between age
35 or older but less than 65 you could be president Abram you could be president
65 or older you qualify for Medicare Carlos you qualify for Medicare
     

Be sure to use if, else if, else structure. Do not do unnecessary testing using && or ||

For the draft, ask for the name and age using the exact prompts given above. Then print the age is this format

Age: 16

 

I reccommend copying the prompts and replies to be sure they are what CodeCheck is looking for. That is what I did.

5C draft:
5C final: