Practice Midterm - Object-Oriented Programming and Design | 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-xnp
koofers-user-xnp 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSc335 Fall04 Practice Midterm Section Leader________ Name ________________________
A. Object Oriented Design 15pts
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
oA student may remove one to many items from the shelf and place them into a shopping basket
oA student should be able to remove items from a shopping basket and place them back on the shelf
oA student should be able to purchase the items in their shopping basket
oTo check out, a student must give their shopping basket to the cashier (there is only one cashier)
oThe cashier creates an order that consists of a item, quantity, and a price based on the item’s ISBN
oIf 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
oIf the CatCard funds are insufficient, place all back on the shelf
1. Create an initial list of objects for your system
2. 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.
1
pf3
pf4
pf5
pf8

Partial preview of the text

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

CSc335 Fall04 Practice Midterm Section Leader________ Name ________________________

A. Object Oriented Design 15pts

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

1. Create an initial list of objects for your system

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

B. Java's Collection Framework, Version 1.4 25pts

Assume all imports are present

3. Write the output (Note: ArrayList is a Composite, which means one can contain another, and another)

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

4. Place a check mark  in the space provided if the line of code would compile

String s1 = "abc"; Object o1 = new Object(); // Compiles? ______ Object o2 = s1; // 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? ______

5. List two differences between a Java interface and a Java class

6. Show the 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());

7. 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");

D. Events 20pts

10. Complete the code such that all elements in the JList are selected one after the other and loop aroun

public class SearcherGUIWorks extends JFrame { public static void main(String[] args) { new SearcherGUIWorks().setVisible(true); } private JList listView = new JList(); private Object[] wordArray = null; public SearcherGUIWorks() { setUpLayout(); setUpModel(); setUpListeners(); } private void setUpListeners() { } private void setUpModel() { ArrayList myWords = new ArrayList(); Scanner readWords = null; try { readWords = new Scanner(new File("dictionary.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } while (readWords.hasNext()) myWords.add(readWords.nextLine()); wordArray = myWords.toArray(); listView.setListData(wordArray); } private void setUpLayout() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200, 300); setTitle("WordSearcher"); Container cp = this.getContentPane(); cp.add(inputField, BorderLayout.NORTH); cp.add(new JScrollPane(listView), BorderLayout.CENTER); }

11. Write interface Command as if it were in its own file

12. Complete class BorrowCommand as if it were in its own file. You will need to store references to the

Borrower and the Book, so you need a constructor. The execute method adjusts the Borrower and the Book

if everything is all right. However, if the Book is already borrowed, execute returns false.

// BorrowCommand attaches a specific book copy to a specific subscriber, both, the book // copy’s and the subscriber’s objects are supplied by the programmer. The BorrowCommand // must have its receivers supplied in the constructor. public class BorrowCommand implements Command { protected Borrower theBorrower; protected Book theBook; public BorrowCommand(Borrower aBorrower, Book aBook) { theBorrower = aBorrower; theBook = aBook; } // When the Borrow button is clicked, execute this public boolean execute() { System.out.println(theBorrower + " attempts to BORROW " + theBook); } public boolean undo() // a borrow by returning { System.out.println(theBorrower + " undo a Borrow" + theBook); } }

The completed class ReturnCommand is given to help answer question 2

// ReturnCommand detaches a specific book copy from a specific subscriber, both, the book copy’s

and

// the subscriber’s objects are supplied by the programmer. The ReturnCommand must have its receivers

// supplied in the constructor.

public class ReturnCommand implements Command

protected Borrower theBorrower;

protected Book theBook;

public ReturnCommand( Borrower aBorrower, Book aBook )

theBorrower = aBorrower;

theBook = aBook;

// When the Return button is clicked, execute this code

public boolean execute( )

System.out.println( theBorrower + " attempts to RETURN " + theBook );

if( theBook.isAvailable( ) ) //The book is not borrowed. Cannot return it.

return false;

// The borrower should no long hold the returned book

if(! theBorrower.removeBook( theBook ) ) //The book is not borrowed by this subscriber.

return false;

theBook.returnBook( );

return true;

public boolean undo( ) // a return

System.out.println( theBorrower + " undo a returnBook " + theBook );

if( theBook.isAvailable( ) )

theBook.checkOutBook( theBorrower );

theBorrower.addBook( theBook );

return true;

return false;