CS46A Spring 18

8A

Complete the application NightSky  to represent the night sky with stars and a moon. Represent the sky with a Rectangle filled with Color.DARK_GRAY. Represent the stars with a small circle filled with COLOR.WHITE. Represent the moon with a larger circle filled with Color.LIGHT_GRAY.

Declare and use constants these constants

final int MAX_X = 400;
final int MAX_Y = 300;
final int MIN_X = 0;
final int MIN_Y = 0;
final int NUMBER_OF_STARS = 50;
final int DIAMETER_OF_STAR = 3;
final int DIAMETER_OF_MOON = 20;

First create and fill the Rectangle. It is at (MIN_X, MIN_Y) and its width than height are MAX_X and MAX_Y

Use a loop to draw 50 stars at random locations. Make the x coordinate a random number between 0 (inclusive) and 400 (exclusive), Make the y coordinate a random number between 0 and 300 (exclusive). This x, y is the upper left hand corner of the bounding rectangle. Use DIAMETER_OF_STAR as the width and height of the Ellipse.

After you have drawn all the stars, draw the moon at the location of the star with the smallest y value. . If more than one star has the minimum y, use the first one with that y as the smallest. You will need to keep track of the smallest star as you go. I declared this before the loop:

Ellipse minStar = new Ellipse(0, MAX_Y, 0, 0);

In the starter file, I have created a Random object for you with a seed. Be sure to use it. Because of the seed, the Random object will produce the same sequence of numbers every time the program is run so your drawing will always look the same (and will pass Codecheck). Do not change the seed.

Generate the random values in this order: x coordinate, y coordinate. That way we will all get the same values

You must use a loop. Do not have 50 lines of code each of which draws an ellipse.

For the draft, draw the Rectangle filled with DARK_GRAY and draw one star at a random x and y

8A draft:
8A final:

8B

There is no start file this time. You create a class called ArrayListPractice with a main method. In the main method, do the following

Note: You must use an ArrayList and its methods. Do not fake it by just printing the correct strings. You will need to know how to use these methods in a situation where the output can not be faked.

Note: There is a version of remove method that will remove a specific object. Use that. Here is the code to remove target from the ArrayList called stuff

stuff.remove(target);

Note: To print an ArrayList in the [xxx, yyy, ...] format, use its toString method like this

System.out.println(stuff.toString());
For the draft,

8B draft:
8B final:

8C

Write a StockExchange class which manages Stock objects.

The Stock class is provided for you. It has a constructor that takes the symbol for the company and its price per share. It has methods to getPrice and getSymbol. Copy the Stock class into your project Do not change it in any way.

The StockExchange has an instance variable ArrayList<Stock>. Call the instance variable stocks. It has a no-argument constructor (That means it takes no parameters) which initializes an ArrayList of Stocks to an empty ArrayList.

Provide these methods

Provide Javadoc

For the draft, define the instance variable and implement the constructor. Also implement the add method.

8C draft:
8C final: