Section3Key 2 - is a sample class that Ihis Number. tests...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: is a sample class that Ihis Number. tests You should note that knew exactly cf NumberTest what the problems with Number were. Eest cases in your program is fine and expected. the writers Having more exposes the flaws in Number. / / A short program that public class NumberTest t public void main(String static args) { / / F l a w # 1 : D E F A U L T _ V A L U Ei s n ' t a constant ( T T D E F A U L T _ V A L U E": + N u m b e r . D E F A U L T _ V A L U E ); System.out.print,In : Number.DEFAULT_VALUE; int oldDefaultValue Number.DEFAULT_VALUE : 4 2; ('TDEFAULT_VALUE rr + Number.DEFAULT_VALUE) ; System.out.println : Number . D EFAULT_VALUE : o ldDefaultValue; () ; System. out .println / / Flaw // System. Number System. System. System. uses default value instead #22 Number (int value) given value ("Creating out.println a new Number (7) " ) i n : new Number (7); out . print In ttn: rf + n) ; 'r + n.getValueO); out . print In "n.getValueO: out . println ); of for all values returns true / / Flaw #3: isPrime fix FIaw #2 to actually see this / / NOTE: must first int values : {-1, 0, L, 4l; (int for i - 0; i < val-ues.Iength; i++1 1 n - new Number (values [i] ) ; (n); printlsPrime ) () ; System. out . printtn returns the same answer / / FIaw #4: isPrime is created // fix Flaw #2 to actually // NOfg: must first exposeFlaw4(7, 8); '71; exposeFlaw4 (8, ) until problem a new Number problem see this whether or not a Nurnber is prime in an easy-to-read / / prinEs (Number n) { private void printlsPrime static (n. isprime O ) t if (n. getValue ( ) + " is prime" ) I System. out .printIn else { ) (n. getValue ( ) + rr is NOT prime" ) ; System. out .println . I form i where isPrime always returns the same / / exposes a bug in isPrime a new Number is created / / answer until private firstValue, int secondValue) statj-c void exposeFlaw4 (int t ( "Creating System. out.println a new Number" ) i Number n new Number(firstValue); (n); printlsPrime n. setValue (secondValue) ,' (n) i printlsPrime l ...
View Full Document

Ask a homework question - tutors are online