EXAMPLES OF CORRECT ANSWERS ON THE MIDTERM.

These are not the only possible correct answers. For some questions there are lots of other equally correct answers.

Answers to all questions except question 5 should be Java code.

Question 1 (20 points):

Use nested for loops to sum up all numbers of the form i * j, where i is any of the numbers from 1 through 10, and j is any of the numbers from 1 through 10. In other words, sum = 1*1 + 1*2 + .... + 10*10.

```   int sum = 0;
for (int i = 1 ; i <= 10 ; i++)
for (int j = 1 ; j <= 10 ; j++)
sum += i * j;
```

Question 2 (30 points, in 3 parts):

Part 1 (10 points): Implement a new object class that represents a month of the year. Your class should contain the following constructor and methods:

```   public Month(int index, String name) { /* stores the index 0..11 and the month name */ ... }
public int index() { /* returns the number 0 through 11 */ ... }
public String name() { /* returns "January", "February", etc. */ ... }
```

```public class Month
{
int index;
String name;

public Month(int index, String name) {
this.index = index;
this.name = name;
}
public int index() { return index; }
public String name() { return name; }
}
```

Part 2 (10 points): Write code that creates an array of 12 month objects, one for each month.

Extra credit (5 points): Use an array of month names in your code that creates these 12 objects.

We can create an array of 12 month objects as follows:

```   String names[] = {"January","February","March","April","May","June","July",
"August","September","October","November","December"};

Month months[] = new Month[]names.length];

for (int i = 0 ; i < months.length ; i++)
months[i] = new Month(i, names[i]);
```

Part 3 (10 points): Write code to go within a `render` method that loops through your array of months, and uses the `drawString()` method to draw a list containing both the number of each month, as a number from 1 through 12, and the name of the month.

Hint: Note that your month numbers should print as 1 to 12. That means you're going to need to add 1 somewhere.

We can display a table of these months within a Java applet as follows:

```   g.setColor(Color.black);
for (int i = 0 ; i < months.length ; i++) {
g.drawString("" + i          , 100, 100 + 30 * i);
g.drawString(months[i].name(), 200, 100 + 30 * i);
}
```

Question 3 (10 points):

Write code that stores the squares of the numbers 0 through 99 into an array `int squares[] = new int[100]`. Your code should contain a for loop.

```   int squares[] = new int[100];
for (int i = 0 ; i < squares.length ; i++)
squares[i] = i * i;
```

Question 4 (20 points):

Use some combination of methods `g.setColor()`, `g.drawRect()`, `g.fillRect()`, `g.drawOval()`, `g.fillOval()`, along with any other methods you like, to draw a simple image of a car. I'll accept lots of variations, but your car needs to have wheels and a body. It's ok if it's a side view.

```      g.setColor(Color.blue);
g.fillRect(100, 200, 300, 100);
g.fillRect(180, 110, 120,  90);

g.setColor(Color.black);
g.drawRect(100, 200, 300, 100);
g.drawRect(180, 110, 120,  90);

g.setColor(Color.gray);
g.fillOval(120, 250, 100, 100);
g.fillOval(280, 250, 100, 100);

g.setColor(Color.black);
g.drawOval(120, 250, 100, 100);
g.drawOval(280, 250, 100, 100);
```

Question 5 (10 points, in 2 parts):

Part 1 (5 points): How many integers can you store in array `int foo[] = new foo[100][200]`?

100 * 200 = 20000

Part 2 (5 points): How many integers can you store in array `int foo[][][] = new int[A][B+C][D]`, where A, B, C and D are integer constants? Express your answer in terms of A, B, C, and D.

A * (B + C) * D

Question 6 (10 points):

Write code to go in a `render()` method that draws an image of a chessboard, where even squares are light colored and odd squares are dark colored, just like in a real chessboard. As in a real chessboard, the upper left and lower right squares should be light colored.

```   int squareSize = 50;
Color lightColor = new Color(200, 200, 200);
Color darkColor  = new Color(100, 100, 100);

for (int row = 0 ; row < 8 ; row++)
for (int col = 0 ; col < 8 ; col++) {
if ((row + col) % 2 == 0)
g.setColor(lightColor); // upper left and lower right squares are this color
else
g.setColor(darkColor);
g.fillRect(squareSize * col, squareSize * row, squareSize, squareSize);
}
```