Object-Oriented Programming and Design - Practice Midterm Exam | C SC 335, Exams of Computer Science

Material Type: Exam; Class: Object-Oriented Programming and Design; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-k7d
koofers-user-k7d 🇺🇸

5

(1)

9 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSc335 Fall04 Practice Midterm Section Leader________ Name ________________________
Assume all imports are present
1. Write the output
ArrayList list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
ArrayList list2 = new ArrayList();
list2.add("d");
list2.add(list);
list2.add("e");
System.out.println(list2);
2. Place a check mark in the space provide if the line o code compiles
String s1;
Object o1;
Object o2 = new String("abc"); // Compiles ______
Integer anInt = new Object(); // Compiles ______
Integer int2 = (Integer)o1; // Compiles ______
String s2 = o2; // Compiles ______
System.out.println((String)o2); // Compiles ______
System.out.println((Integer)s2); // Compiles ______
3. List 3 differences between a Java interface and a Java abstract class
4. Something about Iterator, perhaps show output
ArrayList list = new ArrayList();
list.add(new Integer(3));
list.add(new Integer(5));
list.add(new Integer(1));
list.add(new Integer(4));
Iterator itr = list.iterator();
System.out.println(itr.hasNext());
System.out.println(itr.next());
System.out.println(itr.next());
System.out.println(itr.next());
5. Write the output of this code fragment
JTextField inputField = new JTextField("123point4");
double input = 0.0;
try
{
input = Double.parseDouble(inputField.getText());
System.out.println("Good");
}
catch (RuntimeException rte)
{
System.out.println("Bad");
}
System.out.println("Ugly");
1
pf3
pf4
pf5

Partial preview of the text

Download Object-Oriented Programming and Design - Practice Midterm Exam | C SC 335 and more Exams Computer Science in PDF only on Docsity!

CSc335 Fall04 Practice Midterm Section Leader________ Name ________________________

Assume all imports are present

1. Write the output

ArrayList list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); ArrayList list2 = new ArrayList(); list2.add("d"); list2.add(list); list2.add("e"); System.out.println(list2);

2. Place a check mark  in the space provide if the line o code compiles

String s1; Object o1; Object o2 = new String("abc"); // Compiles ______ Integer anInt = new Object(); // Compiles ______ Integer int2 = (Integer)o1; // Compiles ______ String s2 = o2; // Compiles ______ System.out.println((String)o2); // Compiles ______ System.out.println((Integer)s2); // Compiles ______

3. List 3 differences between a Java interface and a Java abstract class

4. Something about Iterator, perhaps show output

ArrayList list = new ArrayList(); list.add(new Integer(3)); list.add(new Integer(5)); list.add(new Integer(1)); list.add(new Integer(4)); Iterator itr = list.iterator(); System.out.println(itr.hasNext()); System.out.println(itr.next()); System.out.println(itr.next()); System.out.println(itr.next());

5. Write the output of this code fragment

JTextField inputField = new JTextField("123point4"); double input = 0.0; try { input = Double.parseDouble(inputField.getText()); System.out.println("Good"); } catch (RuntimeException rte) { System.out.println("Bad"); } System.out.println("Ugly");

6 Object Oriented Analysis and Design The University of Arizona Bookstore has decided that there is just too

much traffic in the bookstore at the beginning of each semester. In an effort to reduce in house traffic, the

bookstore has decided to implement an online storefront where students can purchase all of their books online and

just pick them up sometime after they’ve been purchased. You are to do some analysis and come up with a model

for the bookstore’s new online front. Here are the specifications

o A student may remove one to many items from the shelf and place them into a shopping basket

o A student should be able to remove items from a shopping basket and place them back on the shelf

o A student should be able to purchase the items in their shopping basket

o To check out, a student must give their shopping basket to the cashier (there is only one cashier)

o The cashier creates an order that consists of a item, quantity, and a price based on the item’s ISBN

o If the student has enough money on their CatCard then the total amount will be deducted and the items will be

removed from inventory, a claim check confirmation for the order will be sent to the student’s email address

o If the CatCard funds are insufficient, place all back on the shelf

a) Create an initial list of objects for your system

b) Draw a UML class diagram showing all of your candidate objects and any relationships between them. Show

inheritance relationships, interface implementation, or just general association by just drawing a line. Write any

multiplicity adornment you can think of. You will likely have 1, and or * in a few places.

8. Tracing Inheritance

Using the classes on the right, for each line of code to the left, write the output in the space provided.

Abba a = new Abba("Kim"); Bar b = new Bar("Devon"); Goo g = new Goo("Chris"); a.one(); // __________________________________ System.out.println("\n"); a.two(); // __________________________________ System.out.println("\n"); b.one(); // __________________________________ System.out.println("\n"); b.two(); // __________________________________ System.out.println("\n"); b.three();// _________________________________ System.out.println("\n"); g.one(); // __________________________________ System.out.println("\n"); g.two(); // __________________________________ System.out.println("\n"); g.three();// __________________________________ System.out.println("\n"); class Abba extends Object { private String id; public Abba(String ID) { id = ID + " "; } public String getID() { return id; } public void one() { System.out.print("A1 " + getID()); } public void two() { System.out.print("A2 "); three(); } public void three() { System.out.print("A3 " + getID()); } } //////////////////////////////////// class Bar extends Abba { public Bar(String id) { super(id); } public void one() { two(); } public void three() { System.out.print("B3 " + getID()); } } //////////////////////////////////// class Goo extends Bar { public Goo(String ID) { super("X" + ID + "X"); } public void two() { System.out.print("G2 "); super.three(); } public void three() { System.out.print("G3 "); } }

9. Refactoring Given the three existing classes on the next page: Point, Circle and Rectangle,

establish a properly designed inheritance hierarchy with an abstract class named Shape. On the

following page, implement all classes to the right of the non-inheritance version. Completely implement

all constructors and all methods. Include all instance variables. You do not have to rewrite the Point

class.

The Circle and Rectangle classes must behave exactly the same with the inheritance hierarchy as

without. Remember that design issues are important. When you are finished, the program below must generate the

Required Output shown below

public class ShapesTwo

public static void main(String[] args)

int n = 3;

Shape[] shapes = new Shape[n];

shapes[0] = new Circle(5, 10, 2.0);

shapes[1] = new Rectangle(40, 10, 3.25, 5.75);

shapes[2] = new Circle(15, 30, 4.0);

for ( int i = 0; i < n; i++)

System.out.println(i + ") " + shapes[i].getArea() + " at " +

shapes[i].getX() + ", " +

shapes[i].getY());

Required Output

0) 3.141592653589793 at 5, 10

1) 18.6875 at 40, 10

2) 12.566370614359172 at 15, 30

public class Point { private int my_xPos; private int my_yPos; public Point(int x, int y) { my_xPos = x; my_yPos = y; } public int getX() { return my_xPos; } public int getY() { return my_yPos; } }

Implement the hierarchy here: Shape, Circle, and Rectangle

(no need to modify the Point class)