CS 160 Final Sample Solutions
Question One
JRadioButton aButton = new JRadioButton();
JRadioButton bButton = new JRadioButton();
JRadioButton oneButton = new JRadioButton();
JRadioButton twoButton = new JRadioButton();
ButtonGroup group1 = new ButtonGroup();
group1.add( aButton );
group1.add( bButton );
ButtonGroup group2 = new ButtonGroup();
group2.add( oneButton );
group2.add( twoButton );
Question Two
0
2
4
3
1
Intermission
0
2
4
5
3
1
Question Three
numbers = new ArrayList(); // 1 point
numbers.add( new Integer(i) ); // 2 points
int total = 0; // 1 point
for (int i = 0; i < numbers.size(); i++) // 2 points
total += ( (Integer) numbers.get(i) ).intValue(); // 2 points
return total; // 1 point
private ArrayList numbers; // 1 point
Question Four
public class Employee
{
Employee (double wage)
{
this.wage = wage;
hours = 0;
}
public int getHours ()
{
return hours;
}
public void work (int hoursWorked)
{
hours += hoursWorked;
}
public double getPay ()
{
double result = wage * hours;
hours = 0;
return result;
}
private double wage;
private int hours;
}
Question Five
for (int i = 0; i < word.length(); i = i + 4)
{
System.out.print(word.charAt(i));
}
Question Six
a. unlimited
b. one
c. a method works differently depending on how it is called
d. 5
e. final
f. boolean flag;
Question Seven
1. The user presses a button
2. Assuming that an ActionListener has been implemented and installed
for that button, the ActionListener is sent the "button press" event
3. The actionPerformed method within the ActionListener is carried out
Question Eight
x = 5
y = 10
Question Nine
public class Food
{
public double getWeight()
{
return weight;
}
public void setWeight (double weight)
{
this.weight = weight;
}
private double weight;
private String Color;
}
public class Apple extends Food
{
// whatever
}
public class Banana extends Food
{
// whatever
}