









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
We will focus on this case.
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
RationalTest
extends^ TestCase
private boolean
check(Rational
r,^ int^ n,^ int
d)^ {
return^ r.getNumerator()
==^ n^ &&^ r.getDenominator()
==^ d;
} public^ void^
testConstructorsEasy()
Rational^ r^ =
new^ Rational(1,
assertTrue (check(r,
r^ =^ new^ Rational(7); assertTrue (check(r,
} public^ void^
testEquals()
assertTrue ( new
Rational^ (7,
5).equals( new
Rational(70,
assertFalse (
new^ Rational(7,
5).equals( new
Rational^ (7,
} public^ void^
testToString()
assertEquals
( new^ Rational(3,5).toString(),
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
addTo(Rational
a)^ { Rational^ answer
=^ add ( this
,^ a); num^ =^ answer.num;den^ =^ answer.den;}
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
grade^ is a variable of type
char z^ Then following does a case analysis on value of
grade switch (grade) {^ case^ ‘A’:^ System.out.println
(“I’m^ very^ happy”); break; case ‘B’: System.out.println
(“I’m relatively happy”); break; case ‘C’: System.out.println
(“At least I get credit”); break; default:^ // All other cases System.out.println
(“Check with the professor”); break; }
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
〉^ ) { case^ 〈 case-label-
〉^ : 〈 statement-sequence-
〉 break;case 〈 case-label-
〉^ : 〈 statement-sequence-
〉 break;… case 〈 case-label-n
〉^ : 〈 statement-sequence-n
〉 break;default : 〈 default-statement-sequence
〉 break;}
The control-expression isone of the following types: char, int, short, byte^ Our text says it cannotbe a byte or short.This is wrong!^ Each case label must be a value intype of control expression You may have any number of statements,including if-else and loops The “break” statement jumpsout of the switch statement^ The optional “default” case isexecuted if no other case matches
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
(grade)^ { case ‘a’: case ‘A’: System.out.println
(“I’m^ very
happy”);
break; … } z Be very careful about using this cascading behavior! z Always insert^ break
statements after every case z^ Then remove ones you do not want
CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)
last