1 CS1110 11 Sept 2007. Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson, founder.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

1 For more info: CS Sep 2008 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston to.
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,
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.
CS100J Today’s topic: Objects and classes Reading for this lecture: Section 1.3 PLive: Activities 3-3.1, 3-3.2, (not 3-3.3), 3-4.1, Summary.
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 30 January 2007 The class definition Course Management System (CMS) for CS100J is populated with students who were pre-registered. Look at course.
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.
1 CS1130 Spring 2011: David Gries & Walker White Transition to Object-Oriented Programming Assumes programming knowledge in a language like Matlab, C,
1 CS1110 Fall 2011: David Gries & Steve Marschner CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering- type problems.
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.
Object Oriented Software Development
1 CS1110 Fall 2010 Instructors: David Gries & Lillian Lee CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering-
CS/ENGRD 2110 SPRING 2015 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.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
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.
CS100J Spring 2006 CS100J: 11 weeks of programming using Java and 2 weeks using Matlab. David Gries is teaching CS100J. Graeme Bailey is teaching a special.
This material is approved for public release. Distribution is limited by the Software Engineering Institute to attendees. Sponsored by the U.S. Department.
COP3502: Introduction to Computer Science Yashas Shankar.
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.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Diagnostic Pathfinder for Instructors. Diagnostic Pathfinder Local File vs. Database Normal operations Expert operations Admin operations.
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.
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.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 CS1100 Fall Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems CS100J:
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
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 For more info: CS100J 7 February 2006 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
1 CS1110. Lecture 2, 28 Jan Objects & classes PLive: Activities 3-3.1, 3-3.2, (not 3-3.3), 3-4.1, Summary of lectures: On course page,
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
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 CS100J Spring Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems.
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.
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.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
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
Development Environment
CS/ENGRD 2110 Fall 2017 Lecture 3: Fields, getters and setters, constructors, testing
Testing and Debugging.
CS100J. Lecture 2, 24 September Objects & classes
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
Sit next to someone. Today, we do some work in pairs.
CS100J 06 September 2005 The class definition
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
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
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
Workshop for Programming And Systems Management Teachers
Presentation transcript:

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 of Digital Equipment Corp. (DEC), The company was a huge player in computer hardware and software in CS academia in the 1970’s. The PDP machines were well known. The VAX had unix on it, and C, and Lisp. It was the main computer in most CS departments of any stature. DEC was bought by COMPAQ in the late 1990’s. Quiz 2 on Tuesday (16 September): How do you evaluate a new expression (see slide 8)? What is the purpose of a constructor (see slide 7)? Fields (variables in a folder); 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)

2 One-on-One Sessions Next two weeks, hold a 1/2-hour one-on-one session on a computer with each student in CS1110. Purpose: See how well you understand what we have done, let you ask questions, give you help. Graded 0-1: you get 1 if you took part in a session. Not counted in course grade. Purpose: to help you. Instructors : Gries, TAs, consultants. How to sign up: Visit the CMS for the course. Click on the assignment One-on-one. You will see a list of times and instructors. Choose one. First-come-first-served. Not registered in the CMS? Maria Witlox immediately and ask her to register you:

3 YOU CAN EXECUTE AN ASSIGNMENT STATEMENT Quiz webpage said to look at top of page 28. It says: To execute the assignment, evaluate the expression and store the value in the variable. Quiz did not ask for a description of how the computer does it, or what you type in DrJava, it asked for you to execute the assignment statement. j 32 j= j + 2; 34 j = j + 2 j = j = 34

4 Field: a variable that is in each folder of a class. a0 Chapter title … number … previous … public class Chapter { private String title; // Title of the chapter private int number; // Number of the chapter private Chapter previous; // previous chapter (null if none) } We generally make fields private instead of public, so that they cannot be referenced from methods that are outside the class. Declarations of fields

5 Getter and setter methods a0 Chapter title … number … previous … /** An instance describes a chapter of a book */ public class Chapter { // Title of the chapter private String title; /** = title of the chapter */ public String getTitle() { return title; } /** Set chapter title to t */ public void setTitle(String t) { title= t; } Getter methods (functions) get or retrieve values from a folder. Setter methods (procedures) set or change fields of a folder getTitle() setTitle(String t)

6 Initialize fields when a folder is first created new Chapter() creates a object but doesn’t allow us To say what values should be in it. We would like to be able to say: new Chapter(“I am born”, 1, null) to set the title to “I am born”, the chapter number to 1, and the previous chapter to null. For this, we use a new kind of method, the constructor. a0 Chapter title … number … previous … getTitle() setTitle(String t)

7 Purpose of a constructor: To initialize (some) fields of a newly created object /** An instance describes a chapter of a book */ public class Chapter { private String title; // Title of chapter private int number; // No. of chapter private Chapter previous; // previous // chapter (null if none) /** Constructor: an instance with title t, chapter number i, and previous chapter p (null if none) */ public Chapter(String t, int i, Chapter p) { title= t; number= i; previous= p; } a0 Chapter title … number … previous … getTitle() setTitle(String t) Chapter(String t, int i, Chapter c) The name of a constructor is the name of the class. Do not put a type or void here

8 New description of evaluation of a new-expression new Chapter(“I am born”, 1, null) 1.Create a new folder of class Chapter, 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 Chapter(“I am born”, 1, null) 3. Use the name of the new object as the value of the new- expression. a0 Chapter title … number … previous … getTitle() setTitle(String t) Chapter(String t, int i, Chapter c) Memorize this new definition! Today! Now!

9 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 specification of the method even before you write the method. A feature called Junit in DrJava helps us develop test cases and use them. You have to use this feature in assignment A1.

10 1. c1= new Chapter(“one”, 1, null); Title should be: “one”; chap. no.: 1; previous: null. 2. c2= new Chapter(“two”, 2, c); Title should be: “two”; chap. no.: 2; previous: c1. To create a testing framework: select menu File item new Junit test case…. At prompt, put in class name ChapterTester. This creates a new class with that name. Save it in same directory as class Chapter. 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

11 /** A JUnit test case class. * Every method starting with "test" will be called when running * the test with JUnit. */ public class ChapterTester 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

12 A testMethod to test constructor and getter methods /** Test first constructor and getter methods getTitle, getNumber, and getPrevious */ public void testConstructor() { Chapter c1= new Chapter("one", 1, null); assertEquals("one”, c1.getTitle(), ); assertEquals(1, c1.getNumber()); assertEquals(null, c1.getPrevious()); Chapter c2= new Chapter("two", 2, c1); assertEquals("two”, c2.getTitle()); assertEquals(2, c2.getNumber()); assertEquals(c1, c2.getPrevious()); } 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.