# Midterm 2 Review

<< Back

#### Instructions

• Complete the following handwritten on paper and make sure to put your name on your paper.
• Your work will be reviewed and graded, so make it presentable.
• The following problems are due before the test starts.
• Review of your work by another student is required for full credit. Add the following at the top of the first page of your paper:

Reviewed by: reviewing student's name

• Discuss the issues and correct any problems found during the review.
• Submit the scenario files to Canvas before the exam by the date and time posted in Canvas for Q10: Leet Arcane Studies.

### A. Numerical Types (4m)

1. Write the letter and heading for this problem on your paper.
2. Review numbers in lesson 4.4.3 and type `double` in lesson 9.1.3
3. On your paper, create three columns with these headings:
Number Type/error Description of type/error
4. For each of the following number examples in Java code, write the information about the number under the above column headings. If there is an error, write "error" and explain why and how to fix it in the description. For example: (4m)
 3 int A positive integer
1. `3`
2. `-3`
3. `3.0`
4. `3.14`
5. `3 14/100`
6. `300,000`
5. Answer the following questions and write the question number and answer on your paper.
1. The problem with the following code snippet is ________.
```int value;
value = 1.23;
```
1. The "value" variable is never initialized.
2. The data type for the "value" variable is not specified.
3. The "value" variable is never assigned a value.
4. The "value" variable is assigned a decimal value, but it can store only a whole number value.
2. In an airline reservation system, the cost of an airline ticket is required. The data type that should be used to store this value is a single ________.
1. int
2. long
3. double
4. short
6. Review your work with another student in the class and correct any problems.

^ top

### B. Loops and counting (3m + 4m)

1. Write the letter and heading for this problem on your paper.
2. Review loops and counting in lesson 7.3.3 and lesson 7.3.4
3. Complete the following problems:
1. On paper, trace the following loop under the column headings showing both the value of "i" (including the initial value) and the terminal "output" for and each iteration.
```for (int i = 2; i < 9; i = i + 2)
{
System.out.println(i);
}
```
i output

2. Answer the question: What does the loop do?
1. Prints all the numbers between 2 and 9.
2. Prints all the numbers between 2 and 8.
3. Prints the odd numbers from 1 to 9.
4. Prints the even numbers from 2 to 8.
4. Answer the following questions and write the question number and answer on your paper.
1. The values of the counter variable in the following `for`-loop are ________.
```for (i = 5; i <= 10; i++)
```
1. i = 4, 5, 6, 7, 8, 9, 10, 11
2. i = 4, 5, 6, 7, 8, 9
3. i = 5, 6, 7, 8, 9, 10, 11
4. i = 5, 6, 7, 8, 9, 10
2. The values of the counter variable in the following `for`-loop are ________.
```for (i = 10; i >= 5; i--)
```
1. i = 10, 9, 8, 7, 6, 5, 4
2. i = 10, 9, 8, 7, 6
3. i = 9, 8, 7, 6, 5
4. i = 10, 9, 8, 7, 6, 5
3. True or false: the last value of a counter variable makes the test condition evaluate to `false` and causes the loop to exit.
5. Review with another student for verification and then discuss the trace and answers.
6. Copy the `for`-loop from problem 1 above to the world of the Greenfoot scenario: Loops and Counting (4m).

After adding the loop, you will see the numbers printed in the Terminal window of Greenfoot.

7. Modify the for-loop in the world of Loops and Counting to display the numbers of the loop in the scenario.
8. Submit the "Loops and counting" scenario as described in Deliverables.

^ top

### C. Arrays and Timing (5m + 4m)

1. Write the letter and heading for this problem on your paper.
2. Review arrays in lesson 7.3.5 and lesson 8.3.5 (Colors for Every Key) and then timing in lesson 10.1.4
3. Complete the following problems:
1. Assume that "a" is an array with at least one element. On your paper, write code to complete the following:
Code Description
Set the first element of array "a" to 42.
Set the last element to 42.
Add the missing step for the loop to print all the elements.
```  for (int i = 0; i < _________ i++)
{
System.out.println(a[i]);
}
```
2. On paper, write the table shown below on your paper and then trace the following algorithm once through the array.
```Color[] colors = { Color.BLUE, Color.GREEN, Color.PINK, Color.RED, Color.MAGENTA };
set index = 0;
When Act button is pressed
set background color to colors[index]
display the index
index++
if index >= length of array then set index = 0
```
index Color of background

3. What does the algorithm do when the Act button is pressed?
1. Displays several different background colors and an index value.
2. Displays a random background color chosen from the array "colors" and an index value.
3. Displays the count of the index at random locations in the scenario's world.
4. Displays a single background color chosen from the array "colors" and an index value.
4. Answer the following questions and write the question number and answer on your paper.
1. The valid range of index values for an array of size 7 is ________.
1. `0 to 7`
2. `1 to 6`
3. `1 to 7;`
4. `0 to 6`
2. With the following array, the line that displays the value 6 is ________.
```int[] myarray = { 2, 4, 6, 8, 10 };
```
1. `System.out.println(myarray(2));`
2. `System.out.println(myarray);`
3. `System.out.println(myarray);`
4. `System.out.println(myarray);`
3. In the `Label` class of the "array_colors" scenario, of what is the following code an example? (answer)
```this("");
```
1. Calling a method named "this" that has a "String" parameter
2. Referring to "this current object".
3. Correcting a problem with shadowing.
4. Calling another constructor in the same class.
5. Review with another student for verification and then discuss the array access, code trace and question answers.
6. Implement the missing parts of the algorithm in problem 2 above by updating the `act()` method in the `ColorWorld` class of the Greenfoot scenario array_colors (4m).
7. Submit the "array_colors" scenario as described in Deliverables.

^ top

### D. Drawing Shapes (5m + 20m)

1. Write the letter and heading for this problem on your paper.
2. Review drawing shapes in lesson 8.3.2, lesson 8.3.3 and text in lesson 10.3.2.
3. Complete the following problems:
1. On paper, write the method call for the following drawing functions for the object "img" with the arguments specified in the description:
Object.Method(arguments) Description
1. Draws a line between the points (x1, y1) and (x2, y2).
2. Draws a hollow rectangle specified by (`x, y`) with `width` and `height`.
3. Draws a solid rectangle specified by (`x, y`) with `width` and `height`.
4. Draws a hollow oval bounded within the rectangle specified by (`x, y`) with `width` and `height`.
5. Draws a solid oval bounded within the rectangle specified by (`x, y`) with `width` and `height`.
6. Draws the specified `text` starting at the `x`-location and the `y`-baseline with the current font.
2. On paper, trace the following code and then sketch what it does on your paper.
```GreenfootImage img = new GreenfootImage(50, 50);
img.setColor(Color.BLACK);

img.drawRect(0, 0, 49, 49);
img.drawOval(0, 0, 49, 49);
img.drawString("Hello", 10, 30);
img.drawLine(0, 0, 49, 49);
img.drawLine(0, 49, 49, 0);

setImage(img);
```
3. On paper, answer the question: What does the above code draw?
 a b. c. d.    4. Download the Greenfoot scenario shaper and start the scenario.
5. Add the above code into the "makeImage" method of the `Mystery` class.
6. Add a Mystery object to ShapeWorld and view the result. Does it match your selection in step 3? If not, correct your answer.
7. Using the Mystery class as a model, in the shaper scenario write a public class named "Rectangle" that is a subclass of Actor and include the following class members:
1. Three private instance variables named: "width", "height" and "color".
2. A public default (no parameter) constructor that sets default values large enough to make the shape easily visible and then calls the method named "makeImage".
3. A public constructor with three parameters, one for each member variable, that assigns the appropriate parameter to each member variable and then calls the method named "makeImage".
4. A method named "makeImage" that:
1. Constructs a GreenfootImage object just large enough to draw the shape.
2. Sets the drawing color to the value of the "color" member variable.
3. Draws the outline of a Rectangle with a width and height of the member variables on the image object.
4. Sets the constructed image object as the image for this actor object.
8. In the `shaper` scenario, write a public class named "Label" that is a subclass of Actor and include the following class members:
1. Two private member variables, one of type `String` and another of type `Color`.
2. A public default (no parameter) constructor that sets default values large enough to make the shape easily visible and then calls the method named "makeImage".
3. A public constructor with two parameters, one for each member variable, that assigns the appropriate parameter to each member variable and then calls the method named "makeImage".
4. A method named "makeImage" that:
1. Constructs a GreenfootImage object just large enough to draw the shape.
2. Sets the drawing color to the value of the "color" member variable.
3. Draws the string in a black color on a transparent background with a font size of about 16.
4. Sets the constructed image object as the image for this Actor object.
5. Test the `Label` class by:
1. adding a `Label` object to the world.
2. entering text in double quotes (like "Test") inside the popup box
3. entering a color using the fully qualified Color name like `greenfoot.Color.BLUE` inside the second popup box textfield

See the Mystery class and lesson 10.3.2 for more information.

4. Review with another student for verification and then discuss the method arguments.
5. Submit the "shapers" scenario as described in Deliverables.

^ top

### E. Deliverables

1. Create a zip file of each scenario and submit the zip file(s) to the Q10: Leet Arcane Studies slot of Canvas.
2. Turn in your paper with the written portion of the problem solved before the exam starts.

^ top

<< Back