1 CS1110 10 Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;

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

1 CS100J. Lecture 2, 25 January 2007 Today’s topic: Objects and classes Reading for this lecture: Section 1.3. It’s most important that you study this.
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
Georgia Institute of Technology DrJava Appendix A Barb Ericson Georgia Institute of Technology May 2006.
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.
Quote for the day: Computational thinking: a fundamental skill for everyone … [It] is … choosing an appropriate representation for a problem or modeling.
UML Basics & Access Modifier
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
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.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
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 Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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!
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
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 SPRING 2014 Lecture 4: The class hierarchy; static components 1.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
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 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
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 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.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
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.
CS/ENGRD 2110 FALL 2016 Lecture 3: Fields, getters and setters, constructors, testing 1.
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
The need for Programming Languages
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
CS/ENGRD 2110 Fall 2017 Lecture 3: Fields, getters and setters, constructors, testing
CS100J 21 February 2005 Casting About
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2018 Lecture 3: Fields, getters and setters, constructors, testing
Debugging with Eclipse
Creating Objects in a Few Simple Steps
CS1316: Representing Structure and Behavior
Sit next to someone. Today, we do some work in pairs.
Barb Ericson Georgia Institute of Technology Oct 2005
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
CS/ENGRD 2110 Spring 2019 Lecture 3: Fields, getters and setters, constructors, testing
Barb Ericson Georgia Institute of Technology June 2005
Debugging with Eclipse
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
Workshop for Programming And Systems Management Teachers
Presentation transcript:

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; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors. Sec (p. 111–112) Testing methods. Appendix I.2.4 (p. 486) Next time: Testing using JUnit. Object: the superest class of them all. pp 153–154. Function toString. Static components Sec. 1.5 (p. 47). Assignment A1 out, due Friday 18 Sept Writing and testing a class definition Labs and one-on-ones (schedule yours on CMS) will help you with it.

2 Field: a variable that is in each folder of a class. a0 Worker name … ssn … boss … We generally make fields private instead of public, so methods that are outside the class can’t reference them. (Slightly confusingly at first, you can access them in the DrJava interactions pane.) public class Worker { } /** An instance is a worker in a certain organization. */ Declarations of fields private String name; // Last name (null if unknown/none) private int ssn; // Social security #: in private Worker boss; // Immediate boss (null if none)

3 Getter and setter methods a0 Worker name … ssn … boss … In the definition of Worker (we post our code on the website): /** = worker’s last name*/ public String getName() { return name; } /** Set worker’s last name to n */ public void setName(String n) { name= n; } /** = last 4 SSN digits, as an int*/ (Try writing it yourself. Should there also be a setter? What about for boss?) Getter methods (functions) get or retrieve values from a folder. Setter methods (procedures) set or change fields of a folder getName() setName(String t)

4 Initialize fields when a folder is first created We would like to be able to use something like new Worker(“Obama”, 1, null) to create a new Worker, set the last name to “Obama”, the SSN to , and the boss to null. For this, we use a new kind of method, the constructor. a0 Worker name … ssn … boss … getName() setName(String t) …

5 Purpose of a constructor: To initialize (some) fields of a newly created object In the class definition of Worker: /** Constructor: an instance with last name n, SSN s (an int in , and boss b (null if none) */ public Worker(String n, int s, Worker b) { name= n; ssn= s; boss= b; } a0 Worker name … ssn … boss … getName() … Worker(String n, int s, Worker b) The name of a constructor: the name of the class. Do not put a type or void here

6 New description of evaluation of a new-expression new Worker(“Obama”, 1, null) 1.Create a new folder of class Worker, with fields initialized to default values (e.g. 0 for int) –of course, put the folder in the file drawer. 2. Execute the constructor call Worker(“Obama”, 1, null) 3. Use the name of the new object as the value of the new- expression. a0 Worker name … ssn … boss … getName() setName(String t) Worker(String t, int i, Worker c) … Memorize this new definition! Today! Now!

7 Testing —using JUnit Bug: Error in a program. Testing: Process of analyzing, running program, looking for bugs. Test case: A set of input values, together with the expected output. Debugging: Process of finding a bug and removing it. Get in the habit of writing test cases for a method from the method’s specification --- even before you write the method’s body. A feature called Junit in DrJava helps us develop test cases and use them. You have to use this feature in assignment A1.

8 1. w1= new Worker(“Obama”, 1, null); Name should be: “Obama”; SSN: 1; boss: null. 2. w2= new Worker(“Biden”, 2, w1); Name should be: “Biden”; SSN: 2; boss: w1. To create a testing framework: select menu File item new Junit test case…. At prompt, put in class name WorkerTester. This creates a new class with that name. Save it in same directory as class Worker. The class imports junit.framework.TestCase, which provides some methods for testing. Need a way to run these test cases, to see whether the fields are set correctly. We could use the interactions pane, but then repeating the test is time-consuming. Here are two test cases

9 /** A JUnit test case class. * Every method starting with "test" will be called when running * the test with JUnit. */ public class WorkerTester extends TestCase { /** A test method. * (Replace "X" with a name describing the test. Write as * many "testSomething" methods in this class as you wish, * and each one will be called when testing.) */ public void testX() { } One method you can use in testX is assertEquals(x,y) which tests whether expected value x equals y

10 A testMethod to test constructor and getter methods /** Test first constructor and getter methods getName, getSSN4, and getBoss*/ 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()); } Every time you click button Test in DrJava, this method (and all other testX methods) will be called. first test case second test case assertEquals(x,y): test whether x equals y ; print an error message and stop the method if they are not equal. x: expected value, y: actual value. A few other methods that can be used are listed on page 488.