6.170 recitation 2 allen miu february 14, 2002. outline ps2 instructions mdd aliasing debugging tips...

18
6.170 Recitation 2 Allen Miu February 14, 2002

Upload: ashley-gibbs

Post on 12-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

6.170 Recitation 2

Allen MiuFebruary 14, 2002

Page 2: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

Outline

PS2 instructions MDD Aliasing Debugging tips Peer review

Page 3: 6.170 Recitation 2 Allen Miu February 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

Page 4: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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

Page 5: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

Example

LinkedList

List

Vector

implements, extends

ListIterator

uses, depends

Page 6: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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!

Page 7: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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

Page 8: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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

Page 9: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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

Page 10: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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!”);

Page 11: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

PS0 & PS1

if(string1.equals(string2) == true) …---------------------------------if(string1.equals(string2)) …

Page 12: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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

Page 13: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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;

Page 14: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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.

Page 15: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

Problem 3: findPrimesEvenFaster

findPrimesFaster(int nValues, boolean printPrimes) …

Vector primeList = new Vector(1);Vector primeList = new Vector(nValues);

Page 16: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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

Page 17: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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

Page 18: 6.170 Recitation 2 Allen Miu February 14, 2002. Outline PS2 instructions MDD Aliasing Debugging tips Peer review

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.