Preview only show first 10 pages with watermark. For full document please download

Final Exam 1 Solution | C (programming Language) | Java ...

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 ...

   EMBED

  • Rating

  • Date

    December 2017
  • Size

    55.1KB
  • Views

    9,107
  • Categories


Share

Transcript

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 02L Tu 11-12:15 05L Tu 12:30-1:45 06L Tu 2-3:15 Part C __________/ 34 pts. Part D __________/ 20 pts. 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 1pt public class MyApp extends JFrame implements ActionListener__________ { private BoxPanel _aBoxPanel; 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 this.add(_aBoxPanel);_______________________________ 2pts // instantiate and set up the timer, including setting its listener _aTimer = new Timer(INTERVAL, this);______ _______ _ _____________________________________________ 4pts _aTimer.start(); this.setVisible(true); } // handle timer ticks public void actionPerformed(ActionEvent e)___ { aBoxPanel.addBox(); } public static void main(String [] args) { MyApp myApp = new MyApp("This is a test"); } 2pts 2pts } _______ 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 _aBoxList; { public BoxPanel( ) { super(); this.setBackground (Color.WHITE); // instantiate an empty ArrayList of Box _aBoxList = new ArrayList(); _ } public void paintComponent (Graphics pen) { // display the background of the panel super.paintComponent(pen);____________________________ // change the pen to a Graphics2D object Graphics2D betterPen = (Graphics2D) pen; _______________ 2 pt 1 pt 2 pts 2 pts 4 pts // display all of the Boxes in _aBoxList _for(int i=0; i<_aBoxList.size(); i++)_________________________ _______aBoxList.get(i).fill(betterPen);_______________________ ________________________________________________________ ______________________________________________________ } public ArrayList getList() { return _aBoxList; } public void addBox() { int x, y; // declare and instantiate a box _Box aBox = new Box();__________________________________ // 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()); _ // set the location of the box bases on x and y aBox.setLocation(x, y);___________________________ _______ // place the box in the ArrayList _aBoxList.add(aBox);____________________________________ // update the display of the panel repaint();______________________________________________ } 1 pts 2 pts 2 pts 2 pts 1pt } 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 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 testMe(Node a) { if (a == null) return null; Node temp, aNode; aNode = new Node (a.getData()); if (a.getNext() == null) return aNode; temp =testMe(a.getNext()); aNode.setNext(temp); return aNode; } public class Node { private elemType _data; private Node _next; public Node(elemType elem) { _data = elem; _next = null; } public Node getNext() { return _next; } public elemType getData() { return _data; } public void setNext(Node aNode) { _next = aNode; } } a is A->B->C->D aNode = A temp is aNode from next activation record a is B->C->D aNode = B temp is aNode from next activation record a is C->D aNode = C temp is aNode from next activation record a is D aNode = D temp is aNode from next activation record 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) 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 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); 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); 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 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 2. A B C D 3. A B C D 4. A B C D 5. 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. A B C D 8. The type of event generated by a slider is A. a SliderEvent B. an ActionEvent C. a ChangeEvent D. a MouseMotionEvent 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. A B C D 10. A B C D 11. A B C D 12. A B C D 13. A B C D 14. A B C D 15. A B C D 16. 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 The string concatenation operation in java is performed by A. the & operator B. the concat method C. the + operator D. the || operator 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. 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 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 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 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. 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(); Grocery {abstract} # price:double idNum:long + + + Grocery(double,long) setPrice(double):void getPrice():double Nonperishable {abstract} Perishable {abstract} - # manufacturer:String expirationDate:Date + Perishable(Date,double,long) + setExpDate(Date):void + getExpDate():Date Orange - count:int + Orange(int,Date, double,long) + Nonperishable(String,double,long) Soda Milk - size:int + Milk(int,Date, double,long) PaperPlate Soup - size:int - deposit:double - size:int - type:String - + Soda(int, double,String, double.long,double) + calculateDeposit():double + calculateTax():double + Soup(int, String, String, double, long) + PaperPlate(int,int, String, double,long) + calculateTax():double size:int count:int <> DepositReq <> Taxable + calculateDeposit():double + calculateTax():double