Note: in this homework, we will start decucting points if your code is not formatted properly. In Bluej when the workbench has the focus, click Edit / Auto Layout to format your code

3A

Precious metals are a type of commodity that can be bought and sold by the ounce. Its price can go up and down. Write a class PreciousMetal that models this commodity

PreciousMetal has a constructor that takes the current price and type of metal as parameters. The class will need some "long term memory" (instance variables) to remember the price and type. Instance variables should always be private.

The class has constructors and methods:

The methods and constructor are provided as stubs in the starter file. A stub has a method header and a body with no implementation. The stub for an accessor method returns 0 for numbers or null for objects like strings. A class with stubs for methods will compile, but you will need to supply implementation and the correct return values. The idea is that you can implement one method at a time and test it since the class will compile. This technique is frequently used in development of applications.

For the draft, provide the instance variable, complete the constructor and the getPrice method.


3A draft:
3A final:

3B

A train moves along a track from the start to the end . There are stops every 5 miles. The track is 50 miles long. The train moves one stop at a time. It can either move toward the end (+ direction) or towards the start (- direction). Write a class Train that models this behavior.

Train class has a default constructor that sets the train at the start moving toward the end (+ direction). You can think of this as a number line with only positive numbers and zero.

Train has these methods

Do not use if statements. You will get no credit if you do

Think about what information needs to be remembered and define instance variables to remember that information.

For the draft, implement the constructor and all the methods as stubs. Include javadoc for the class, constructor and each method. Your class should compile and run with the tester even though the return values will not be accurate. You will lose points if you do not include javadoc.

HINT: You can represent the direction with either 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.

HINT: Remember that each stop is 5 miles from the previous stop. So if a train has moved 3 stops along the track, it distance from the start is 15. It will probably be easiest to call the start location stop number 0.

Javadoc for the class might look like this:

/**
 *   Models a Train runing on a straight track going either direction
 */

3B draft:
3B final:

3C

In this exercise, we are going to revisit the flower from homework2c. In that assignment you wrote a FlowerViewer with a main method and did all the drawing in it. But there is a problem with that design: If you wanted to draw a second flower, you would have to recalculate all the coordinates and repeat all the lines of code. Not good. The solution to this problem is to create a Flower class that models a flower at a given (x,y) coordinate and that knows how to draw itself on the canvas (It has a draw method)

Flower will have a constructor that takes the x and y coordinates of the upper left hand corner of the flower as parameters. That point is the same as the upper left-hand corner of the surrounding rectangle for the top petal of the flower. The radius of each circle is 15. Think about what the object needs to remember. That will be the instance variables.

No starter this time. You provide all the code.

Flower has a draw method that can draw the Flower at its x, y coordinates. The code in Flower's draw method should be similar to the code in the main method of FlowerDrawer except you will use variables x, y rather than hard coding he coordinates. (NOTE: the draw method should only draw one flower)

Make the center yellow using Color.YELLOW. Create a custom color for the petals with red = 200, green = 0, blue = 200. Be sure to use the fill() method. Do not use draw()

For the draft, draw the circle for the center of the petal. Make it yellow.

In the final, draw the center first and then the petals to get all the pixels in the right location for Codecheck.

Hint: You are given the radii of the circles but the Ellipse constructor requires width and height.

I will provide a FlowerViewer class that instantiates several of your Flower objects and calls their draw method.

3C draft:
3C final: