Anda di halaman 1dari 10

CSE 133

FINAL EXAM
Spring 2008
May 8, 2008







Part A __________/ 60 pts.
Name _______________________________________
Part B __________/ 11 pts.
Section (circle one)
01L Tu 9:30-10:45 Part C __________/ 34 pts.
02L Tu 11-12:15
05L Tu 12:30-1:45 Part D __________/ 20 pts.
06L Tu 2-3:15
Total __________/ 125 pts.
Part A. (60 pts) In this part of the exam you will complete a program which will place boxes on a
panel, at regular intervals. The program will randomly choose where the box is to be placed on
the panel. The questions in this part will tell you the parts of the code you are to supply for this
program, and will also give you the parts of the code that have been supplied to you. The
program will use Java 6, awt and swing.

1) (15 pts)Supply the missing statements for the main application, where some of the statements
have already been supplied. The class MyApp will be responsible for having a timer and
responding to the timer by placing a new Box on the BoxPanel, and displaying the new box. You
may need to look at the other questions in this part to understand how the program will work.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

4pts public class MyApp extends JFrame implements ActionListener__________ {
private BoxPanel _aBoxPanel;
1pt private Timer____ _aTimer;
private static final int INTERVAL=100;

public MyApp (String title) {
super (title);
setSize (800,600);
setDefaultCloseOperation(EXIT _ON_ CLOSE);
_aBoxPanel = new BoxPanel();

// place the BoxPanel into MyApp
2pts this.add(_aBoxPanel);_______________________________

// instantiate and set up the timer, including setting its listener
4pts _aTimer = new Timer(INTERVAL, this);______ _______
_ _____________________________________________

_aTimer.start();
this.setVisible(true);
}
// handle timer ticks
2pts public void actionPerformed(ActionEvent e)___ {
aBoxPanel.addBox();
}
public static void main(String [] args) {
2pts MyApp myApp = new MyApp("This is a test"); _______
}
}
2) (20 pts) This part of the program is the BoxPanel class. It is responsible for maintaining a list
of the Boxes and for displaying the Boxes in the Panel. The background of the panel will be
WHITE and the color and size of the boxes is the same and defined in the class Box.
import java.awt.*;
import java.util.*;
import javax.swing.*;

1 pt public class BoxPanel extends JPanel {
public ArrayList <Box> _aBoxList;

public BoxPanel( ) {
super();
this.setBackground (Color.WHITE);
// instantiate an empty ArrayList of Box
2 pt _aBoxList = new ArrayList<Box>(); _
}
1 pt public void paintComponent (Graphics pen) {
// display the background of the panel
2 pts super.paintComponent(pen);____________________________
// change the pen to a Graphics2D object
2 pts Graphics2D betterPen = (Graphics2D) pen; _______________

4 pts // display all of the Boxes in _aBoxList
_for(int i=0; i<_aBoxList.size(); i++)_________________________
_______aBoxList.get(i).fill(betterPen);_______________________
________________________________________________________
______________________________________________________
}
public ArrayList<Box> getList() {
return _aBoxList;
}
public void addBox() {
int x, y;
1 pts // declare and instantiate a box
_Box aBox = new Box();__________________________________
2 pts // get two random numbers within the size of the panel for x and y
x = int(Math.random()*this.getWidth()); _
y = int(Math.random()*this.getHeight()); _
2 pts // set the location of the box bases on x and y
aBox.setLocation(x, y);___________________________ _______
// place the box in the ArrayList
2 pts _aBoxList.add(aBox);____________________________________
// update the display of the panel
1pt repaint();______________________________________________
}
}
3) (11 pts) This is the Box class it is responsible for creating boxes which are always 20x20 in
size and colored BLUE. It can assume that the Box is always created at location 0,0 and let its
setLocation method take care of placing it where it is needed. It will be responsible for
displaying the Box object as well as maintaining those things which are not maintained in class
Rectangle2D.Double of which it is a subclass.
import java.awt.*;
import java.awt,geom.*;
import java.io.*;
1 pt public class Box _extends Rectangle2D.Double___ {
Color _myColor;
public Box() {
4 pts super(0,0,20,20);_______________________________________
_myColor = Color.BLUE;_______________________________
______________________________________________________
}
4 pts public void fill (Graphics2D aBetterPen) {
_aBetterPen.setColor(_myColor);__________________________
_aBetterPen.fill(this);____________________________________
_______________________________________________________
}
2 pts public void setLocation(int x, int y) {
_setFrame(x, y, getWidth(), getHeight());__________________________
}
}

4) (2 pts) What changes would need to be made to the Box class if you now want to be able to
store the ArrayList of boxes into a file?

The Box class would need to implement Serializable.
5) (12 pts) Given that the user would like the ability to save the ArrayList in the BoxPanel, a
ButtonPanel containing the button is added to the frame with the statement
this.add(new ButtonPanel(aBoxPanel), BorderLayout.SOUTH);
Fill in the code each statement in the ButtonPanel, to add the button and make it work.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class ButtonPanel extends JPanel {
private Box Panel _theBoxPanel;
public ButtonPanel(BoxPanel aBoxPanel) {
super();
_theBoxPanel = aBoxPanel;
2 pts // Declare and instantiate the button
JButton saveButton = new JButton("Save");________________
2 pts // Make the button listener the internal listener class defined below
saveButton.addActionListener(new SaveListener();___________
// add the button to the panel
2 pts _this.add(saveButton); __________________________________
}
// Define the listener class.
private class SaveListener implements ActionListener {
// declare method to handle this button
2 pts _public void actionPerformed(ActionEvent e)_______________ {
String filename = "Exam.dat";
ArrayList<Box> theList = theBoxPanel.getList();
try {
// open the file
ObjectOutputStream ostream =
new ObjectOutputStream(new
FileOutputStream(filename));
4 pts // write the ArrayList to the file
for (int i=0; i<theList.size(); i++) _________________
________ostream.writeObject(theList.get(i));_______
______________________________________________
_theBoxPanel.repaint();
}
catch (Exception except) {
System.out.println("Output file did not open correctly");
}
}
}
}

Part B. (11 pts) Given the following linked list and the code for Node, what is the result of the
recursive function testMe, and what does the function do.
AHead





BHead



The function testMe given below is invoked BHead = testMe(AHead);

public Node<elemType> testMe(Node<elemType> a)
{ if (a == null)
return null;
Node<elemType> temp, aNode;
aNode = new Node<elemType> (a.getData());
if (a.getNext() == null)
return aNode;
temp =testMe(a.getNext());
aNode.setNext(temp);
return aNode;
}
public class Node <elemType> {
private elemType _data;
private Node<elemType> _next;
public Node<elemType>(elemType elem) {
_data = elem;
_next = null;
}
public Node<elemType> getNext() {
return _next;
}
public elemType getData() {
return _data;
}
public void setNext(Node<elemType> aNode) {
_next = aNode;
}
}
The program returns null when the list is empty. (1 pt)
The program cycles down the list until it gets to the last node and returns a node with the data D (2 pts)
The program stores a node with data value A into BHead (2 pts)
The program creates a new node, which is a copy, for each node in the original list (2 pts)
Hooking the nodes up correctly (4 pts)
A B C D
a is A->B->C->D
aNode =

temp is aNode from
next activation record
A
a is B->C->D
aNode =

temp is aNode from
next activation record

a is C->D
aNode =

temp is aNode from
next activation record

a is D
aNode =

temp is aNode from
next activation record

A B C D
D
C
B
Part C ( 34 pts) Circle the correct answer to the left of the question in the answer area provided.
Each question will be worth 2 pts.
Answer Area
A B C D 1. The default layout for a JPanel is
A. Absolute Layout
B. Border Layout
C. Flow Layout
D. Grid Layout
A B C D 2. Which GridLayout creates a grid with an unspecified number of rows and one
column?
A. new java.awt.GridLayout(0,0);
B. new java.awt.GridLayout(0,1);
C. new java.awt.GridLayout(1,0);
D. new java.awt.GridLayout(1,1);
A B C D 3. Which of the following will create a RadioButton which displays with a red box
around the button to show its color?
A. _redButton = new JButton(java.awt.Color.red);
B. _redButton = new JRadioButton("red");
C. _redButton = new JRadioButton(java.awt.Color.red);
D. _redButton = new JRadioButton();
_redButton.setBackground(java.awt.Color.red);
A B C D 4. Which pattern do you use when you want to store a changing value allowing two
or more objects to send it messages to perform some action?
A. Composite Pattern
B. Factory Pattern
C. Holder Pattern
D. Proxy Pattern
A B C D 5. The function Math.random() returns a value which is between
A. 0 and the maximum integer
B. 0 and the maximum double
C. 0 and 1, not including 1
D. 1 and the maximum integer
A B C D 6. When an instance variable is declared to be static the term static means:
A. The instance variable is a constant, i.e. it can not change its value.
B. It is of a primitive type
C. It can not be used in methods of that class
D. Only one instance of that variable exists and it is used for all
instances of the class.
A B C D 7. The type of event generated by a slider is
A. a SliderEvent
B. an ActionEvent
C. a ChangeEvent
D. a MouseMotionEvent
A B C D 8. Which of the following does not automatically invoke the repaint method
A. The panel first opens
B. An object on the panel changes location
C. A panel obstructing this panel closes
D. The panel is resized
A B C D 9. When a new button is added to an existing panel, what is the function that needs
to be invoked to make the button appear.
A. the paintComponent method
B. the paint method
C. the repaint method
D. the validate method
A B C D 10. The string concatenation operation in java is performed by
A. the & operator
B. the concat method
C. the + operator
D. the || operator
A B C D 11. The difference between a JLabel and a JTextField is
A. A JTextField can hold more text than a JLabel
B. A JTextField may allow the user to edit the value, while a JLabel can
never be edited by the user
C. A JTextField can not be initialized to a value while the JLabel must be
given an initial value.
D. All of the above are true.
A B C D 12. What type of exceptions does java not require to be handled by the program
A. an IOException
B. a RunTimeException
C. a ClassNotFoundException
D. all of the above require that the program detect and handle the exception
A B C D 13. An sequential data structure which restricts access to only one end of the data
structure, generally referred to as the top is
A. a deque
B. a dictionary
C. a queue
D. a stack
A B C D 14. The sequential data structure which implements a first-in first-out data restriction
is
A. a deque
B. a dictionary
C. a queue
D. a stack
A B C D 15. Which of the following is not a difference between an application and an applet
A. an application can be run directly at a system prompt, while an applet is
run within a webpage.
B. an applet has a void init method instead of a constructor.
C. an applet uses a paint method instead of a paintComponent method
D. you do not need the setVisible method invocation with an applet that you
do need with an application.
A B C D 16. What file extension is generally used for an image that includes a background
A. .doc
B. .gif
C. .jpg
D. .wav
A B C D 17. The class used to store a sound file is
A. AudioClip
B. ImageIcon
C. Image
D. Sound

Part D. (20 pts) The questions in this section pertain to the UML diagram given on the last page of this
exam booklet. Each question is worth 2 pts. Use the code presented here to determine how each object has
been declared and instantiated.

Perishable skimMilk = new Milk(32,new Date(5,15,08),2.45,45793);
NonPerishable tomatoSoup = new Soup(16,"tomato","Campbells",1.44,39162);
Soda cola = new Soda(48,"Coke",0.30,15386,3.49);
Soda rootbeer = new Soda(8,"Hires",0.05,14269,0.75);
Orange orange = new Orange(1,new Date(5,30,08),0.60,29874);
double tax, depToPay;

Valid Not Valid 1. skimMilk.setPrice(2.45);

Valid Not Valid 2. tax = cola.calculateTax();

Valid Not Valid 3. pass rootbeer to a parameter of type Taxable

Valid Not Valid 4. orange.expirationDate = new Date(5,25,08);

Valid Not Valid 5. System.println(skimMilk.price);

Valid Not Valid 6. tomatoSoup.manufacturer("SmartChoice");

Valid Not Valid 7. tomatoSoup.idNum = 36678;

Valid Not Valid 8. pass tomatoSoup to a parameter of type NonPerishable

Valid Not Valid 9. pass skimMilk to a parameter of type Grocery

Valid Not Valid 10. depToPay = rootbeer.calculateDeposit();





Perishable {abstract}

- expirationDate:Date

+ Perishable(Date,double,long)
+ setExpDate(Date):void
+ getExpDate():Date
PaperPlate

- size:int
- count:int

+ PaperPlate(int,int, String,
double,long)
+ calculateTax():double

Soda

- size:int
- deposit:double

+ Soda(int, double,String,
double.long,double)
+ calculateDeposit():double
+ calculateTax():double

Grocery {abstract}

- price:double
# idNum:long

+ Grocery(double,long)
+ setPrice(double):void
+ getPrice():double

Orange

- count:int

+ Orange(int,Date,
double,long)

Nonperishable {abstract}

# manufacturer:String

+ Nonperishable(String,double,long)
<<interface>>
Taxable

+ calculateTax():double
<<interface>>
DepositReq

+ calculateDeposit():double
Soup

- size:int
- type:String

+ Soup(int, String,
String, double,
long)
Milk

- size:int

+ Milk(int,Date,
double,long)

Anda mungkin juga menyukai