Rational Numbers and switch Statement in CMSC 131 Fall 2006 - Prof. Bonnie J. Dorr, Study Guides, Projects, Research of Computer Science

A part of the lecture notes for the computer science principles 131 (cmsc 131) course taught by bonnie dorr at the university of california, berkeley, during the fall 2006 semester. The notes cover topics such as rational numbers, arithmetic operations, comparisons, error reporting, and the switch statement. Students are encouraged to complete project #4, which is assigned and due on march 16, 2006. The document also includes examples of testing the rational class using junit and error reporting.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-pja-1
koofers-user-pja-1 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 131 Fall 2006
Bonnie Dorr (adapted from Rance Cleaveland)
Lecture 20:
Rationals, Switch,
Break, Continue
Last time:
1. Aliasing and Mutability
2. Floating Point calculations
3. Example class development: Rational Numbers
Today:
1. Continuation of Rational numbers
2. switch
3. break
4. Case continuation (or “fall through”)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Rational Numbers and switch Statement in CMSC 131 Fall 2006 - Prof. Bonnie J. Dorr and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Lecture 20: CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Rationals, Switch,^ Break, Continue Last time: 1. Aliasing and Mutability 2. Floating Point calculations 3. Example class development: Rational Numbers Today: 1. Continuation of Rational numbers 2. switch 3. break 4. Case continuation (or “fall through”)

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Project #4 Is Assigned z^ It is due Friday, 3/16 at 11 pm z^ The project is closed^ z^ You must complete the project by yourself^ z^ Assistance can only be provided by teaching assistants(TAs) and instructors^ z^ You must not look at other students' code z^ Start now!^ z^ Read entire assignment from beginning to end beforestarting to code^ z^ Check out assignment now from CVS^ z^ Follow the instructions

exactly , as much of grading is

automated

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Comparisons z^ p/q = s/t if^ z^ p/q, s/t are in lowest terms, and^ z^ p = q and s = t z^ p/q < s/t if pt < qs

We will focus on this case.

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Using JUnit to test ourRational Class public^ class^

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,

1,^ 3));

r^ =^ new^ Rational(7); assertTrue (check(r,

7,^ 1));

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

Error Reporting Example private boolean

isValid() {

return^ (num > 0 && den > 0);} public^ Rational(

int^ num,^ int

den) {

this .num = num; this .den = den; if^ (!isValid()) {System.

out .println("Error in constructor... Exiting");System. exit (1);}

reduce();}

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

Is Rational immutable?^ YES! (So far.)What would make it a mutable class? (A public setter!)For example ….^ public^ void

addTo(Rational

a)^ { Rational^ answer

=^ add ( this

,^ a); num^ =^ answer.num;den^ =^ answer.den;}

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

The^ switch

Statement

z^ A way of doing case analysis based on a control expression^ z^ Assume^

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

z^ Case chosen depends on value of control expression z^ Optional^ default
case tells what to do if no other cases are applicable

CMSC 131 Fall 2006Bonnie Dorr (adapted from Rance Cleaveland)

The^ switch

Statement:

General Form switch (^ 〈 control-expression

〉^ ) { case^ 〈 case-label-

〉^ :statement-sequence-

break;casecase-label-

〉^ :statement-sequence-

break;… casecase-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)

Case Continuation z^ Case continuation also called “cascading case behavior”, “fallingthrough to the next case”, etc. z^ It is occasionally handy for combining of cases^ e.g. case-insensitivity^ switch^

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

The^ default

Case

z^ default^ is optional^ If omitted, and no case matches, then the switch statement does nothing z^ However: you should always include a default case, even if you want nothing tobe done if no case matches (you should never rely on implicit behavior!) z^ Although cases are not required to be in order … (following is legal):^ switch ( option ) {^ case 2:^ …^ case 9:^ …^ default:^ …^ case 1:^ …^ } z^ … it is much better to list cases:^ z^ in increasing order^ z^ with^ default

last