Object-Oriented Programming - Computer Science - Midterm 1 | CS 162, Exams of Computer Science

Material Type: Exam; Class: INTRODUCTION TO COMPUTER SCIENCE II; Subject: Computer Science; University: Oregon State University; Term: Fall 2007;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-2mg
koofers-user-2mg 🇺🇸

4

(1)

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Pg 1/9
Name: _______________________
CS 162 Midterm 1
Spring 2007
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 Marks
Object-Oriented Programming
/ 20
Classes
/ 20
Total
/ 40
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Object-Oriented Programming - Computer Science - Midterm 1 | CS 162 and more Exams Computer Science in PDF only on Docsity!

Name: _______________________

CS 162 Midterm 1

Spring 2007

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 Marks

Object-Oriented Programming / 20

Classes / 20

Total / 40

Section I: Object-Oriented Design [20 points]

  1. These questions deal with the AsciiAnimator code (on page 8) which implements an ASCII Animator that is almost identical to the one you wrote in Assignment #2. The AsciiAnimator compiles without errors and it plays the baseball ASCII animation file perfectly. You will answer questions about the design of this code and about object- oriented programming in general.

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:

  1. If you considered the interface and implementation as general concepts, then the answer should be:
    • The interface defines what a system is designed to do (service view that users need to understand). It says nothing about how the assigned task is being performed
    • The implementation is the view that the programmers get to see. It describes how the class actually carries out the behavior specified by the interface.
  2. If you were applying the general concepts of interface and implementation to this specific example, then the correct answer is:
    • There is no interface to the code because it is all in the main function
    • The implementation is everything else
  3. If you were interpreting interface as the user interface, then we accepted answers which said something to the effect of:
    • The interface was the GUI or the files being read in
    • The implementation was everything else eg. the loader, the animation player, etc.

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 {

/**

  • An ArrayList of the frames in the ASCII animation */ private ArrayList frames;

/**

  • Returns the ArrayList of AsciiFrames */ public ArrayList getFrames() { return frames; }

... }

Option 2 provides functions to access the frames in the ArrayList:

public class AsciiFrameManager {

/**

  • An ArrayList of the frames in the ASCII animation */ private ArrayList frames;

/**

  • Returns the first frame in the animation */ public AsciiFrame getFirstFrame() {…}

/**

  • Returns the next frame in the animation */ public AsciiFrame getNextFrame() {…}

/**

  • Returns true if there are more frames in the animation and false otherwise */ public boolean hasMoreFrames() {…}

... }

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]

  1. The following questions deal with the Account class and the SecureAccount interface which are on page 9. a) Write Java code for a class called SwissBankAccount which must have the following characteristics:
  • It is a subclass of Account [1 point] We were looking for the extends keyword.
  • It implements the SecureAccount interface [2 points] You needed to add the implements keyword and also implement the isSecure() function.
  • Write a constructor with the following form: SwissBankAccount(double initialBalance, int securityCode) This constructor will call the Account constructor that takes the initial balance as an argument. The security code passed in as an argument will serve as the official security code for the SwissBankAccount object. [3 points] To answer this question correctly, you needed a private member variable for the security code. You then needed to call super(initialBalance) with the first line of the constructor. Finally, you needed to store the security code in the member variable
  • Write a withdraw function with the following form: void withdraw(double amount, int securityCode) If the security code matches the official security code, the balance is decremented by the amount. If it doesn’t match, nothing happens. Hint: Pay close attention to the access modifier on the balance instance field in the Account class. [3 points] For full credit, you needed to check if the security code is correct and if it is, call the withdraw() method of Account. Note that you cannot access the balance member variable of Account because it is private!

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.

  1. We have two classes called Hexagon and Octagon. These two classes clearly share some similarities which should be put into a common Polygon entity in order to reuse code. The programmer has a choice between creating an interface for a Polygon or an abstract class for a Polygon.

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(); } } }