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.

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.
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.
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 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.
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 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
CS100J CS100M is in Kimball Bll. Course Management System for CS100J is now populated with students who were pre-registered. Look at course web page to.
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.
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.
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.
CS100J 09 September 2003 Course Management System for CS100J is now populated with students who were pre-registered. Look at course web page to see how.
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.
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 2012 Lecture 2: Objects and classes in Java 1.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
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.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
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.
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.
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 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.
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 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
Classes and Objects.
Lecture 10 Review of classes
CS100J. Lecture 2, 24 September Objects & classes
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Fall2017 Lecture 4: The class hierarchy; static components
Set & Get Methods ICS 111: Introduction to Computer Science I
CS 1110 Please do this now. Draw three variables on a piece of paper as shown to the right: b 4 c 7 d false Below is a “program” consisting of 3 assignment.
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
CS100J 06 September 2005 The class definition
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
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
Presentation transcript:

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. --Ken Olson, president, chairman and founder of Digital Equipment Corp., The company was a huge player in computer hardware and software in CS academia in the 1970’s. The old 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. The company was bought by COMPAQ in the late 1990’s. Quiz 2 on Tuesday: How do you execute an assignment statement? How do you evaluate a new expression (see slide 10)? What is the purpose of a constructor (see slide 9)?

2 CS100J, 03 February 2005 Reading for this lecture: Section 1.4, 1.5, and 1.7 (not 1.6). Read all the “style notes”, too. Summary of lectures: On course home page, click on “Handouts” and then “Outline of lectures held so far”. Today: Class Object, method toString() Fields (variables in a folder), and getter and setter methods for them. Constructors. Static components.

3 Class Object: The superest class of them all See 1/2-page section on page 154. The reason for this will become clear later. You need this information to do assignment A1. Every class that does not extend another one automatically extends class Object. public class C { … } is equivalent to public class C extends Object { …}

4 Class Object: The superest class of them all Bill Patient name B. Clinton address New York owes $ See 1/2-page section on page 154. Bill Patient name B. Clinton address New York owes $ equals(Object) toString() this is really this Object Because it is always there, to avoid clutter, we don’t generally draw the partition for superclass Object

5 Method toString() Bill Patient name B. Clinton address New York owes toString() $ equals(Object) toString() Object Convention: c.toString() returns a representation of folder c. Put following method in Patient. public String toString() { return name + “ ” + address + “ ” + owes; } The expression c automatically does c.toString()

6 Field: a variable that is in each folder 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.

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

8 We need a way to initialize fields when a folder is first created new Chapter() creates a folder 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)

9 The purpose of a constructor is to initialize (some) fields of a newly created folder /** 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

10 New description of execution 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 (0 for int, for example). 2. Put the folder in file-drawer Chapter. 3. Execute the constructor call Chapter(“I am born”, 1, null) 4. Use the name of the new folder as the value of the new-expression. Memorize this new definition! Today! Now! a0 Chapter title … number … previous … getTitle() setTitle(String t) Chapter(String t, int i, Chapter c)

11 You can have more than one constructor /** 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; } /** Constructor: an instance with title t, chapter number i, and previous chapter null */ public Chapter(String t, int i) { title= t; number= i; previous= null; } Makes it easier, more flexible, for the “user” who is using the class

12 A static field does not appear in each folder. It appears in the file drawer, by itself, on a piece of paper. There is only ONE copy of it. a0 Chapter title “peace” public class Chapter { private int title; // Number of chapter private static int numberOfChapters= 0; } a1 Chapter title “truth” numberChaps 2 File drawer for class Chapter Reference the static variable using Chapter.numberChaps Use a static variable when you want to accumulate information about all (or some) folders.