CS1110 lecture 5 14 Sept 2010 Testing; the class Object; toString; static variables & methods Reading for this lecture: Testing with JUnit (Appendix I.2.4.

Slides:



Advertisements
Similar presentations
1 For more info: CS Sep 2008 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston to.
Advertisements

CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
1 CS Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
1 CS100J 1 February Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson,
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
CS100J 11 September 2003 Course Management System for CS100J is now populated with students who were pre-registered. Look at course web page to see how.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
1 CS Sept 2010 Inside-out rule; use of this, super Developing methods (using Strings). Read sec. 2.5, stepwise refinement Listen to Plive, 2.5.1–2.5.4.
CS/ENGRD 2110 SPRING 2014 Lecture 4: The class hierarchy; static components 1.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
1 CS November Testing/Debugging. And Applications Read chapter 14, pp. 385–401 Prelim 2 Max 100 Median 82 Mean 78.3 Min * ***************************
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
1 CS2111 is no longer required for affiliation with Computer Science How to install newest Java Royal Flush is better than Full.
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
1 For more info: CS100J 7 February 2006 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
1 Royal Flush is better than Full House CS March 31 Testing/Debugging. Also, about A6 Read chapter 14, pp. 385–401 Prelim 2 is Thursday eve, 14.
1 For more info: CS100J 08 February 2005 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
1 For more info: CS February 2009 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 SPRING 2016 Lecture 4: The class hierarchy; static components 1.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
1 CS Sept Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson, founder.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
CS Feb 2010 Customizing a class
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Some Eclipse shortcuts
CS1110 Classes, stepwise refinement 23 Sep 2009
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 16-Nov-18.
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
Testing and debugging A short interlude 2-Dec-18.
Sit next to someone. Today, we do some work in pairs.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Sit next to someone. Today, we do some work in pairs.
Barb Ericson Georgia Institute of Technology Oct 2005
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 4: The class hierarchy; static components
Class Structure 25-Feb-19.
Barb Ericson Georgia Institute of Technology June 2005
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
Testing and debugging A short interlude 17-Apr-19.
Barb Ericson Georgia Institute of Technology Oct 2005
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
For more info: CS100J 13 September 2005 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
CS Feb 2011 Customizing a class
CS100J Final Class on Classes 10 February 2005
Workshop for Programming And Systems Management Teachers
Presentation transcript:

CS1110 lecture 5 14 Sept 2010 Testing; the class Object; toString; static variables & methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp ), the class Object (pp ), function toString (pp ), static variables and methods (Sec. 1.5, p. 47). Reading for next two lectures: Executing method calls, if-statements, the return statement in a function, local variables. Chapter 2 except and This reading will some clarify some concepts, such as method parameters, that we’ve had to gloss over so far. Keep your iClickers and a sheet of paper out. A1 (still) due Saturday Sept 18 on CMS; group yourselves by Wed. Ignore “Extended Until” on CMS (We had to put in a fake extension to work around a CMS limitation.) 1 re: lab 03, quiz 2, etc. was sent on Saturday. Bouncing s: cabooserwar, blacktora4546, jfk54, tariq.mozaini, lukeg432, dc.mcmurtry10, khyjhcho.cabooserwar blacktora4546jfk54tariq.mozainilukeg432dc.mcmurtry10khyjhcho

Organizing and streamlining the testing process Testing: Process of analyzing, running program, looking for bugs (errors). Test case: A set of input values, together with the expected output. Develop test cases for a method from its specification --- even before you write the method’s body. /** = number of vowels in word w. Precondition: w contains at least one letter and nothing but letters*/ public int numberOfVowels(String w) { // (nothing here yet!) } Test cases “need”, “rhythm” reveal vagueness in the spec! Developing test cases first, in “critique” mode, can prevent wasted work. 2

“Slinky Dog” j0 bestFemaleFr bestMaleFr numIsBFOf w0 0 … dName Person s0 Person(String, int, boolean) … “Jessie” null bestFemaleFr bestMaleFr numIsBFOf b0 3 … dName Person j0 Person(String, int, boolean) … “Buzz Lightyear” j0 bestFemaleFr bestMaleFr numIsBFOf w0 2 … dName Person b0 Person(String, int, boolean) … “Woody” j0 bestFemaleFr bestMaleFr numIsBFOf b0 2 … dName Person w0 Person(String, int, boolean) … “Lotso” null bestFemaleFr bestMaleFr numIsBFOf L0 … dName L0 Person(String, int, boolean) … 1 Person “talking telephone” null bestFemaleFr bestMaleFr numIsBFOf null 0 … dName Person t0 Person(String, int, boolean) … A1 possible test cases 3

Specifications and headers for methods in class Worker, plus test cases /** Constructor: a worker with last name n (“” if none), SSN s, * and boss b (null if none). * Precondition: n is not null, s in with no leading zeros, * so SSN should be given as */ public Worker(String n, int s, Worker b) /** = worker's last name */ public String getName() /** Set worker's last name to n ("" if none). * Precondition: n is not null.*/ public void setName(String n) /** = last 4 SSN digits without leading zeroes. */ public int getSSN4() /** = worker's boss (null if none) */ public Worker getBoss() /** Set boss to b */ public void setBoss(Worker b) a1 Worker name “Obama” ssn boss null … a0 Worker name “Biden” ssn 2 boss o1 … a1a0 w2w1 4

/** Test constructor and getters*/ public void testConstructor() { Worker w1= new Worker(“Obama", , null); assertEquals(“Obama”, w1.getName()); assertEquals(6789, w1.getSSN4()); assertEquals(null, w1.getBoss()); Worker w2= new Worker(“Biden", 2, w1); assertEquals(“Biden”, w2.getName()); assertEquals(2, w2.getSSN4()); assertEquals(w1, w2.getBoss()); / // should test last name “”, too } A testMethod to test a constructor (and getter methods) Every time you click button Test in DrJava, all “testX methods” are called, in some order. first test case second test case assertEquals(x,y): test whether x (expected) equals y (computed); print an error message and stop the method if they are not equal. Pg 488 lists some other methods that can be used. File->new JUnit test case … [save in same directory as Worker.java] imports junit.framework.TestCase, with key methods. 5

Class Object: The superest class of them all A minor mystery: since Worker doesn’t extend anything, it seems that it should have only the methods we wrote for it. But it has some other methods, too. Java feature: Every class that does not extend another one automatically extends class Object. That is, public class C { … } is equivalent to public class C extends Object { …} 6

Class Object: The superest class of them all a1 Worker name “Obama” ssn boss null a1 Worker name “Obama” ssn boss null equals(Object) toString() … So this… is really this. Object Because it is always there, to avoid clutter, we don’t generally draw the partition for superclass Object. (A2 will be an exception). getBoss() … getBoss() … 7

Method toString() Convention: c.toString() returns a representation of folder c, giving info about the values in its fields. When a String is expected (or in the Interactions pane), the expression c is evaluated as c.toString(). /** = e.g., “Obama, XXX-XX-6789, boss null”, “Biden, XXX-XX-2, boss Obama” [see posted code for full spec]*/ public String toString() {return ??; } // bad scrunched style, slides are tiny  (E) none of the above (B) lname + “, XXX-XX-” + getSSN4() + “, boss ” + boss.getName() (D) lname + “, XXX-XX-” + getSSN4() + “, boss ” + getBoss() (C) getName() + “, XXX-XX-” + getSSN4() + “, boss ” + getBoss().getName() (A) “lname” + “, XXX-XX-” + “getSSN4()” + “, boss ” + “boss.getName()” 8

A static variable appears not in each folder, but as a single entity in the file drawer. It can be used to maintain information about multiple folders. Declaration: private static int numberOfWorkers; // no. of Worker objects created … a0 Worker name “Biden” a1 Worker “Obama” name numberOfWorkers 2 File drawer for class Worker Reference the variable by Worker.numberOfWorkers. Class, not var holding folder name a1 x …… a0 y 9

Make a method static if it doesn’t need to be in all the folders because: it wouldn’t reference the contents of the “containing” folder; equivalently, its actions/results would be the exactly the same no matter which folder it were in. getName() should not be static, but the following should: Sample call: Worker.moreThan100() Class, not var holding a folder name /** = “there are more than 100 workers”. */ public static boolean moreThan100() { return numberOfWorkers > 100; } A static method is also in the drawer, not in individual folders. 10

Should the following function be static? (A) yes (B) no /** = “a and b are not null and the last 4 digits of their SSNs are the same”. */ /* (This notation means that the function yields * the truth value of the quoted statement. * So, a and b are allowed to be null!) */ public [static??] boolean clashingSSNs(Worker a, Worker b) { return a != null && b != null && a.getSSN4() == b.getSSN4(); } 11