CSc 335 Spring 2007 Practice Quiz 1, Quizzes of Computer Science

A practice quiz for csc 335 spring 2007 class. It includes questions on java interfaces, modeling an online bookstore system, collections, and guis and events.

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-zdq
koofers-user-zdq 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 335 Spring 2007 ___
Practice Quiz 1 Section Leader ____________ Name _______________________ 50 pts max
1. Interfaces (10pts)
In the box, write class MyInteger so its objects can be stored inorder into a TreeSet or sorted with
java.util.Collections.sort. Each compareTo message must return the difference in value with a
negative value indicating the object is less than the argument. Complete the unit test by filling in the ____'s.
public interface Comparable<T> {
public int compareTo(T other);
}
import static org.junit.Assert.*;
import org.junit.Test;
public class MyIntegerTest {
@Test
public void testMyInteger() {
MyInteger a = new MyInteger(2);
MyInteger b = new MyInteger(5);
assertEquals(____, a.compareTo(b));
assertEquals(____, b.compareTo(a));
assertEquals(____, a.compareTo(a));
}
}
2. Find the Objects (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 CatCard has enough money 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
Create a list of the five most important objects to model this system along with the major responsibility:
Candidate Object Major Responsibility
pf2

Partial preview of the text

Download CSc 335 Spring 2007 Practice Quiz 1 and more Quizzes Computer Science in PDF only on Docsity!

C Sc 335 Spring 2007 ___

Practice Quiz 1 Section Leader ____________ Name _______________________ 50 pts max

1. Interfaces (10pts)

In the box, write class MyInteger so its objects can be stored inorder into a TreeSet or sorted with

java.util.Collections.sort. Each compareTo message must return the difference in value with a

negative value indicating the object is less than the argument. Complete the unit test by filling in the ____'s.

public interface Comparable { public int compareTo(T other); } import static org.junit.Assert.*; import org.junit.Test; public class MyIntegerTest { @Test public void testMyInteger() { MyInteger a = new MyInteger(2); MyInteger b = new MyInteger(5); assertEquals (____, a.compareTo(b)); assertEquals (____, b.compareTo(a)); assertEquals (____, a.compareTo(a)); } }

2. Find the Objects (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 CatCard has enough money 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

Create a list of the five most important objects to model this system along with the major responsibility:

Candidate Object Major Responsibility

3. Collections Write the output generated by this code 10pts

String input = "momma"; java.util.Map<Character, Integer> tm = new java.util.TreeMap<Character, Integer>(); for (int index = 0; index < input.length(); index++) { char key = input.charAt(index); // containsKey returns true if the key already exists if ( tm .containsKey(key)) { Integer count = tm .get(key); count = count + 1; tm .put(key, count); } else { tm .put(key, 1); } } Set keys = tm .keySet(); for (Character next : keys) { System.out.print(next + " "); } System.out.println(); Collection accounts = tm .values(); for (Integer count : accounts) { System.out.print(count + " "); }

4. GUIs and Events (15pts)

The C Sc 335 Section Leaders are all confused! They can’t remember what a valid number is! It is in

your best interest as a C Sc 335 student to help the SL’s out of their predicament before your grade

gets too messed up. Finish the program with a graphical user interface that will allow your section

leaders to enter text into a field and be told whether or not that text represents a valid number. If the

input is not a number, put ERROR! in the middle. Hint use Double.parseDouble(String) that throws an

exception if the String is not a valid number

// Assume all imports exist public class NumberFrame extends JFrame { public static void main(String[] args) { new NumberFrame().setVisible(true); } private JTextField myField = new JTextField(); private JLabel myLabel = new JLabel("Nothing Yet...", JLabel.CENTER); JLabel prompt = new JLabel("Enter a number below"); // initialize the JFrame public NumberFrame() { setUpLayout(); setUpListeners(); } private void setUpListeners() { NumberListener listener = new NumberListener(); myField.addActionListener(listener); } private void setUpLayout() { setTitle("Numbers"); setSize(180, 110);

Write answer here on two

lines