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.

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

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
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;
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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,
10-Jun-15 Fibonacci Numbers A simple example of program design.
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
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.
CS 1110 Prelim I: Review Session. Exam Info Prelim 1: 7:30–9:00PM, Thursday, 6 October, Statler Auditorium Look at the previous Prelims Arrive early!
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.
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.
1 CS Sept 2010 Customizing a class Quote for the day: I have traveled the length and breadth of this country and talked with the best people, and.
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.
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.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 CS2111 is no longer required for affiliation with Computer Science How to install newest Java Royal Flush is better than Full.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
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.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
1 CS Sept 2010 Customizing a class Quote for the day: I have traveled the length and breadth of this country and talked with the best people, and.
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
Lecture 10 Review of classes
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
CSC 113 Tutorial QUIZ I.
Testing and debugging A short interlude 2-Dec-18.
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
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
Concepts for this lecture: Class Instance of a class, (an object)
Class Structure 25-Feb-19.
Testing and debugging A short interlude 17-Apr-19.
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS100J Classes, stepwise refinement 21 September 2005
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 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp. 385—388), 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 have had to gloss over so far. Keep your iClickers and a sheet of paper out. A1: due Sat, Sept 12, on CMS; form groups by Wed. Ignore “Extended Until” on CMS (We put in a fake extension to work around a CMS limitation.) 1

22 Quiz on Thursday. Know the purpose of a constructor and how a new-expression is evaluated –and be able to evaluate a new-expression.

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!) } Developing test cases first, in “critique” mode, can prevent wasted work. 3

“Child 2” j0 mom pop children w0 0 name Elephant s0 isMale() … “Mumsie” null mom pop children null 1 name Elephant j0 isMale() … “Opa” null mom pop children null 1 … name Elephant b0 isMale() … “Popsi” null mom pop children b0 2 name Elephant w0 isMale() … “Child 1” null mom pop children w0 name L0 isMale() … 1 Elephant A1 possible test cases dealing with the number of children 4

Spec, headers for methods in class Worker /** 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.*/ public Worker(String n, int s, Worker b) /** = worker's last name */ public String getLname() /** = last 4 SSN digits without leading zeroes. */ public int getSsn() /** = worker's boss (null if none) */ public Worker getBoss() /** Set boss to b */ public void setBoss(Worker b) a1 Worker lname “Obama” ssn boss null … a0 Worker lname “Biden” ssn 2 boss a1 … a0 w2w1 5 String int Worker int Worker

/** Test constructor and getters*/ public void testConstructor() { Worker w1= new Worker(“Obama", , null); assertEquals(“Obama”, w1.getLname()); assertEquals(6789, w1.getSSN4()); assertEquals(null, w1.getBoss()); Worker w2= new Worker(“Biden", 2, w1); assertEquals(“Biden”, w2.getLname()); assertEquals(2, w2.getSSN4()); assertEquals(w1, w2.getBoss()); } Testing the constructor (also getter methods) Click button Test in DrJava to call all “testX methods”. assertEquals(x, y): test whether x (expected) equals y (computed); print error mess. and stop execution 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 WorkerTester.java] 6

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 { …} 7

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

7 Method toString() a1 Worker lname Obama ssn boss toString() null equals(Object) toString() Object Convention: c.toString() returns a representation of folder c, giving info about the values in its fields. Put following method in Worker. /** = representation of this Worker * [etc., see full program] */ public String toString() { return …; } In appropriate places, the expression c automatically does c.toString() getBoss() …

10 Another example of toString() /** An instance represents a point (x, y) in the plane */ public class Point { private int x; // the x-coordinate private int y; // the y-coordinate /** Constructor: An instance for point (xx, yy) */ public Point(int xx, int yy) { } /** = a representation of this point in form “(x, y)” */ public String toString() { return ; } Getter and setter methods are not given on this slide Function toString should give the values in the fields in a format that makes sense for the class. Example: “(3, 5)” Fill these in

10 A static method appears not in each folder but only once, in the file drawer. Make a method static if it doesn’t need to be in a folder because it doesn’t reference the contents of the “containing” folder. /** = “b is c’s boss”. Precondition: b and c are not null. */ public static boolean isBoss(Worker b, Worker c) { return b == c.getBoss(); } /** = “this object is the c’s boss”. Precondition: c is not null. */ public boolean isBoss(Worker c) { return this == c.getBoss(); } keyword this refers to the name of the object in which it appears

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 all the folders. Declaration: (goes inside class definition, just like field declarations) private static int numberOfWorkers; // no. of Worker objects created … a0 Worker lname “Biden” a1 Worker “Obama” lname numberOfWorkers 2 File drawer for class Worker Reference the variable by Worker.numberOfWorkers. Class, not var holding folder name a1 x …… a0 y 12