





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: INTRODUCTION TO COMPUTER SCIENCE II; Subject: Computer Science; University: Oregon State University; Term: Fall 2007;
Typology: Exams
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Name: _______________________
You have 50 minutes to complete this midterm. You may detach the last two pages for your convenience. If you find that you are spending a large amount of time on a difficult question, skip it and return to it when you’ve finished some of the easier questions. Total marks for this midterm is 40.
Section I: Object-Oriented Design [20 points]
a) Encapsulation requires a strict division between interface and implementation. What is the interface and what is the implementation? Note: the interface mentioned here is a generic concept and is different from a Java Interface. [4 points]
This was a somewhat ambiguous question and we accepted the following responses:
In general, if your answer was reasonable according to any of these three interpretations, you received full or close to full marks.
b) What benefit is there to having a strict division between interface and implementation? [2 points]
The key concept here is that if you change the implementation, the interface need not change. This fact allows for the interchangability of components and it makes the code easier to understand at a high level. It also makes the code highly reusable because you can use it wherever the interface is suitable. If you mentioned any of these reasons, you received full credit.
c) Does the AsciiAnimator code violate the object-oriented principle of encapsulation? Explain your answer. [3 points]
Option 1 returns the entire ArrayList of AsciiFrames:
public class AsciiFrameManager {
/**
/**
... }
Option 2 provides functions to access the frames in the ArrayList:
public class AsciiFrameManager {
/**
/**
/**
/**
... }
From an object-oriented perspective, which one of these options is better? Explain your answer [5 points]
Option #2 is better as it allows the programmer to change the ArrayList to another data structure and not have to modify any code outside of the AsciiFrameManager. By “wrapping” the ArrayList, we add an additional layer of abstraction which allows users of the AsciiFrameManager to not know about the ArrayList and only focus on the behavior of the AsciiFrameManager.
Section II: Classes [20 points]
public class SwissBankAccount extends Account implements SecureAccount { private double securityCode;
public SwissBankAccount(double initialBalance, int securityCode) { super(initialBalance); // this has to come first this.securityCode = securityCode; }
public void withdraw(double amount, int securityCode) { if( this.securityCode == securityCode ) { withdraw(amount); }
d) What is the difference between the protected access modifier and the private access modifier? [2 points]
A protected access modifier makes the instance field or method visible to all subclasses of the current class.
A private access modifier makes the instance field or method only visible to the current class.
a) Give one reason when you would prefer using an abstract base class over an interface. [2 points]
You would prefer an abstract base class if there are instance fields in the Polygon class that you would like to inherit in the Hexagon and Octagon classes.
b) Give one reason when you would prefer using an interface over an abstract base class. [2 points]
You would prefer an interface if you need to have Hexagon and Octagon implement multiple interfaces since you cannot inherit from multiple classses.
(Weaker reason – 1 pt) You would prefer an interface if you only have public functions (and not instance fields) that you require the Hexagon and Octagon classes to have.
Code for Question 1 (You may detach this page) public class AsciiAnimator { public static void main(String args[]) { try { JFrame frame; JLabel[] labels; JPanel displayPanel;
// Set up the GUI frame = new JFrame(); displayPanel = new JPanel(); labels = new JLabel[13]; GridLayout gl = new GridLayout(13,1); displayPanel.setLayout(gl); for (int i = 0; i < 13; i++) { labels[i] = new JLabel(""); labels[i].setFont(new Font("Courier", Font. PLAIN , 12)); displayPanel.add(labels[i]); } frame.add(displayPanel); frame.setSize(500, 300); frame.setVisible(true);
// Load the ASCII animation file BufferedReader br = new BufferedReader(new FileReader(args[0])); int labelNum = 0; String line = br.readLine(); while( line != null ) { if( line.equals(“=====”)) { Thread. sleep (200); labelNum=0; } else { labels[labelNum].setText(line); labelNum++; } line = br.readLine(); } br.close();
} catch (Exception e) { System. out .println(e.getMessage()); e.printStackTrace(); } } }