You are on page 1of 4

Homework 1

Note: The numbers in parentheses refer to the Review Exercises and Programming Exercises in Big Java 3rd Edition in cases where the numbers are different from those in the 4th Edition. Problem 1, JUnit Review Based on the Bank ccount c!ass in Big "ava#s $ection %.&' write a "(nit test c!ass ca!!ed Bank ccountTest that tests each method in Bank ccount c!ass.
public class BankAccountTest { BankAccount ba; @Before public void setUp() throws Exception { ba new BankAccount(!"#$%&'()*"%++); , @After public void tear-own() throws Exception { ba null; , @Test public void testdeposit() { ba.deposit(%++); assertE/uals(#+++*ba.0etBalance()); , @Test public void test1e0ativedeposit() { ba.deposit(2"++); assertE/uals(3Unable to deposit ne0ative a4ount.3*"%++*ba.0etBalance()); , @Test public void testwithdraw() { ba.withdraw("++); assertE/uals("#++*ba.0etBalance()); , @Test public void test1e0ativewithdraw() { ba.withdraw(2"++); assertE/uals(3Unable to withdraw ne0ative a4ount.3*"%++*ba.0etBalance()); , ,

)*+P ,&, -omework , Rev. ./0.10,.

Problem 2, R9.1 $uppose C is a c!ass that imp!ements the interfaces I and J. 2hich of the fo!!owing assignments re3uire a cast4 A and 5
5c ...; 6i ...; 78 ...;

a. c i; b. 8 c; c. i 8; Problem 3, R9.2 modified $uppose C is a c!ass that imp!ements the interfaces I and J. 2hich of the fo!!owing assignments wi!! throw an exception when the5 are executed in se3uence4
C c = new C();

a. I i = c; b. J j = (J) i; c. C d = (C) i;
6 do not believe an9 of these will throw an exception.

)*+P ,&, -omework , Rev. ./0.10,.

&

Problem 4, P9.2 modified 6efine a c!ass Quiz that imp!ements the Measurable interface. 3ui7 has a score and a !etter grade 8such as B+9. (se the first imp!ementation of the DataSet c!ass 8in $ection :.,9 to process a co!!ection of 3ui77es. 6isp!a5 the average score and the 3ui7 with the highest score 8both the !etter grade and score9.
class :ui; i4ple4ents <easurable { private double score; private =trin0 0rade; public :ui; (double a=core* =trin0 a>rade) { score a=core; 0rade a>rade; , @?verride public double 0et<easure() { return score ; , public void set<easure (double s) { score s; , public =trin0 0et>rade() { return 0rade ; , public double 0et=core() { return score ; , , interface <easurable { double 0et<easure:; , class -ata=et { private double su4; private <easurable 4axi4u4; private int count; public -ata=et() { su4 +; count +; 4axi4u4 null; , public void add(<easurable x) { su4 su4 @ x.0et<easure(); if (count + A A 4axi4u4.0et<easure() B x.0et<easure())4axi4u4 count@@; , )*+P ,&, -omework , Rev. ./0.10,.

x;

public double 0etAvera0e() { if (count +) return +; else return su4 C count; , public <easurable 0et<axi4u4() { return 4axi4u4; , =9ste4.out.print6n(0etAvera0e()) =9ste4*out.println(4ax.0et=core()); =9ste4.out.print6n(4ax.0et>rade()); ,

)*+P ,&, -omework , Rev. ./0.10,.

You might also like