6.170 recitation 2 allen miu february 14, 2002. outline ps2 instructions mdd aliasing debugging tips...
TRANSCRIPT
6.170 Recitation 2
Allen MiuFebruary 14, 2002
Outline
PS2 instructions MDD Aliasing Debugging tips Peer review
Submitting PS2
Staple your problems in a correct order Include test cases and outputs on hard copies Include only the modified files on hard copies
No javadoc printouts please! Check Handout S3 for how to write code specs Check Handout S5 for new electronic
submission instructions
Module Dependency Diagram
Consists of nodes and arcs Each node represents an abstraction Each arc represents a relationship between
two nodes A using arc (solid arrow) shows the
dependency of a module on a particular specification
An extension arc (hollow arrow) shows an implementation of a module that meets a certain specification
Example
LinkedList
List
Vector
implements, extends
ListIterator
uses, depends
Why MDD?
Documentation purpose When errors are detected, you know
which modules to reconsider and fix Divide work among group membersA good programmer always have a
mental MDD of her program. Of course, a pro will also have a written MDD too!
Aliasing (equals() vs ==)
String str0 = "rules!";String str1 = "Allen "+str0;String str2 = str1; // alias
if(str1 == str2) System.out.println("The two Allen's are "+
"the same String instances.”);if(str1.equals(str2)) System.out.println("The two Allen's are "+
"eqivalent Strings. ”+str1);
Aliasing (equals() vs ==)
String str0 = "rules!";String str1 = "Allen "+str0;String str2 = "Allen "+str0;
if(str1 == str2) System.out.println("The two Allen's are "+
"the same String instances.");if(str1.equals(str2)) System.out.println("The two Allen's are "+
"eqivalent Strings. "+str1);
Debug Good old System.out.println(“help”); Assertions
Debugger: jdb, kdeveloper(complicated to use but powerful; use as your last resort)
if (unexpected_condition) { System.out.println(“I failed at ‘unexpected_condition’”); System.exit(1);}
Usefulness of toString()
class MyObject { public boolean isAlive;
… public String toString() {
return “MyObject lives: “+isAlive; }}
--------------------------------------------------------…MyObject obj = new MyObject();System.out.println(“MyObject lives: ”+obj.isAlive);
--------------------------------------------------------
…MyObject obj = new MyObject();System.out.println(obj + “-- and I can still “+
concatenate all I want!”);
PS0 & PS1
if(string1.equals(string2) == true) …---------------------------------if(string1.equals(string2)) …
PS0 & PS1
str1 = input.substring(pos, pos+sub);str2 = input.substring(pos+sub, pos+sub+sub);------------------------int endPos = pos+sub;str1 =input.substring(pos, endPos);Str2 =input.substring(endPos, endPos+sub);
Problem 1:Financial Calculator
int numberOfYears = 4; interestRate = Math.pow((value/principal),
(1/numberOfYears)) - 1;
-------------------------------int numberOfYears = 4; interestRate = Math.pow((value/principal),
(1/(double)numberOfYears)) - 1;
Problem 2: findPrimesFaster
int upperLimit =(int)Math.floor(Math.sqrt((double)i));
A: for (int j = 2; j <= upperLimit; j++) v.s.
B: for (int j = 2; j <= Math.sqrt(i); j++)-----------------------------------------------------------------------------------------
Good compilers will turn “B” into “A” but bad ones will not.
Problem 3: findPrimesEvenFaster
findPrimesFaster(int nValues, boolean printPrimes) …
Vector primeList = new Vector(1);Vector primeList = new Vector(nValues);
Problem 4: StringScrambler
String output = ""; ...while (words.hasMoreTokens()) {StringBuffer reversedWord = new
StringBuffer((String)words.nextToken());…output += reversedWord.reverse();
}return output;-------------------------------------------StringBuffer output = new StringBuffer();… … … output.append(…);
Problem 5: Intersection
Must check whether the denominator can be zero before dividing.
Clearly specify and handle error cases. coincident, return (NaN, NaN)
parallel, return (-Infinity,NaN) coincident or parallel, return null throw new Exception();
Problem 6: Automobilepublic Automobile(String make, String model, String year) {super(4);...
}-----------------------------------------------------------------------------------------
public String toString() {String description = “Automobile with …” + super.toString();
}Cannot access “numberOfWheels” directly.