Exam Rules
Problem 1. Implement a class ShoppingList that has methods
public void addItem(String item) public String getAllItems()
The getList method should return a string that numbers each
item. For example, if you call
ShoppingList myList = new ShoppingList();
myList.add("Pencils");
myList.add("Paper");
String whatToBuy = myList.getAllItems();
System.out.println(whatToBuy);
then the output should be
1. Pencils 2. Paper
Deliverable: A file ShoppingList.java. You need not write any javadoc comments.
Problem 2: Write a program that draws the following figure:

Submit a file Problem2Component.java that does the drawing.
Complete this outline:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
/**
This component draws two squares and two circles.
*/
public class Problem2Component extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
...
}
}
Here is the viewer class:
import javax.swing.JFrame;
public class Problem2Viewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Problem2Component component = new Problem2Component();
frame.add(component);
frame.setVisible(true);
}
}
Problem 3.
Consider the following DiskSensor class that aims to solve homework 9/10:
/**
A device that senses inputs from a spinning disk.
*/
public class DiskSensor
{
private int count;
private int oldValue;
/**
Constructs a sensor with count zero.
*/
public DiskSensor()
{
count = "";
oldValue = -1;
}
/**
Adds a measurement to the sensor.
@param value 0 or 1
*/
public void addValue(int value)
{
oldValue = value;
count = count + (int)Math.abs(value - oldValue);
}
int getTransitionCount()
{
return count;
}
}
This class has one syntax error, one logic error, and one style error. What
are they, and how do you fix them? Turn in a class DiskSensor.java
that fixes all three.
4. Consider the Bug and BugTester class below. Complete the following table
that shows the values of the instance variables of the lady object
of the BugTester class after the calls to the constructor and
methods labeled 1 through 6.
The Bug class has a bug, so the values that you see aren't the values that you might expect. Show what actually happens, not what should happen.
| line | position | direction |
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
/**
This class models a bug that crawls along a horizontal line.
*/
public class Bug
{
private int position;
private int direction;
/**
Constructs a bug with a given position, facing right.
@param initialPosition the initial position
*/
public Bug(int initialPosition)
{
position = initialPosition;
direction = 1;
}
/**
Moves the bug by a given number of units in the current direction.
@param distance the distance to move
*/
public void move(int distance)
{
position = position + direction * distance;
}
/**
Flips the direction of this bug.
*/
public void turn()
{
direction = direction - 1;
}
/**
Gets the current position of this bug.
@return the position
*/
public int getPosition()
{
return position;
}
}
public class BugTester
{
public static void main(String[] args)
{
Bug lady = new Bug(10); // Line 1
lady.move(2); // Line 2
lady.turn(); // Line 3
lady.move(5); // Line 4
lady.turn(); // Line 5
lady.move(4); // Line 6
System.out.println(lady.getPosition());
System.out.println("Expected: 11");
}
}