q6soln - CS 302 Fall 2006 Quiz 6 (10 points) Solution This...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 302 Fall 2006 Quiz 6 (10 points) Solution This quiz is due no later than 5:00 pm Tuesday, December 12. You may turn the quiz in in—class on Tuesday, December 12 or drop it off in Rebecca Hasti's mailbox on the 5‘h floor of the CS building no later than 5:00 pm, Tuesday, December 12. You may use your lecture notes, the textbook, the CS 302 website, CodeLab, and Eclipse to answer these questions. You may not get help on this quiz from any other person except a CS 302 lab instructor or lecturer. Answers must be hand-written. Show all your work on the quiz. Question 1 [4 points] One of the pieces of the system for an on-line store is the following interface: public interface Shippable { double shippingCharge(); } Write an instantiable class Movie the implements the Shippable interface. A movie keeps track of its title (a string), the year it was released (an integer), and its format (a boolean, true if the format is DVD, false if the format is VHS). The Movie constructor should take a title, a year of release, and a (boolean) format as parameters (you may assume the value passed in for the year is reasonable, i.e., you do not have to do any checking of the parameter values). The Movie class should also have a toString method that returns a string representation of a movie in the form " ti tle ( year) ~forma t" (where forma t is either DVD or VHS). The shipping charges are $3.25 if the movie is in DVD format and $5.75 if it is in VHS format. Answer: public class Movie implements Shippable { private String title; 'private int year; private boolean isDVDformat; public Movie(String title, int year, boolean format) { this.title = title; this.year = year; isDVDformat = format; } public String toString() { return title + "(" + year + ")-" + (isDVDformat ? "DVD" : "VHS"); } public double shippingCharge() { return isDVDformat ? 3.25 : 5.75; // Note: this is the same as: // if (isDVDformat) return 3.25; // else return 5.75; Continues on back Question 2 [6 points] Suppose that the ExceptionGenerator class has a class method named throwOne () . The throwOne method creates and throws an exception each time it is called. The exception that gets thrown by throwOne is one of the following: 0 aClassCastExcepticn 0 anIndexOutOfBoundsException 0 aNullPointerException 0 aNumberFormatException Suppose that we are given the following class: public class Exceptichester { public static void main<Stringll args) { System.out.print("main["); try { methodl(); System.out.print("lAl"); } catch (NullPointerException e) { System.cut.print("NPE"); } System.out.println("]main"); } public static void method1() { System.out.print("ml<"); try { method2(); System.out.print("lB["); } catch (ClassCastException e) { System.out.print("CCE"); } catch (IndexOutOfBoundsException e) { System.out.print("IOBE"); } System.out.print(">ml"); } public static void method2() { System.out.print("m2:"); try { ExceptionGenerator.throwOne(); System.out.print("lC["); } catch (IndexOutOfBoundsException e) { System.out.print("IOBE"); } System.out.print(":m2"); } Assume that the ExceptionGenerator class and the ExceptionTester class each compiles without error. What is printed out when the Exception’l‘ester program is run and ExceptionGenerator.throwOne()finouc Parta:aClassCastException Answer: main [ml<m2 : CCE>ml IAI ]main Partb:anIndexOutOfBoundsException Answer: main [ml<m2 : IOBE:m2 1B 1 >ml [Al ]main Partc:aNullPointerException Answer: main [ml<mZ :NPE] main ...
View Full Document

This note was uploaded on 03/27/2008 for the course CS 302 taught by Professor Willbenton during the Fall '07 term at Wisconsin.

Page1 / 2

q6soln - CS 302 Fall 2006 Quiz 6 (10 points) Solution This...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online