1 CS1110 22 Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.

Slides:



Advertisements
Similar presentations
Inheritance Writing and using Classes effectively.
Advertisements

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 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
1 CS1110 Stepwise refinement, wrapper classes 29 Sept Prelim: 8PM Thursday 8 October (next week) If you have a conflict and have not been contacted, .
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
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 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
CS 1110 Prelim I: Review Session. Introduction My name: Bruno Abrahao – We have four TA’s in the room to help you individually Shuang Zhao Nam Nguyen.
1 CS100J 18 September 2007 More on Methods. Developing methods. Also: The inside-out rule; and the use of this and super. Read sec. 2.5 on stepwise refinement.
1 CS100J 20 September 2005 More on Methods. Developing methods Also: About the use of this and super Read section 2.5 on stepwise refinement Listen to.
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 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
1 CS1130 Spring 2011: David Gries & Walker White Transition to Object-Oriented Programming Assumes programming knowledge in a language like Matlab, C,
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
1 CS1110 Lecture Sept Developing (String-processing) programs; class Vector; wrapper classes Prelim: 7:30-9PM Thursday 7 October. Last name A-K:
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
1 CS1110 Fall 2011 David Gries, Steve Marschner Reading for this lecture and previous lecture: Sections 1.1, 1.2, 1.3. Lab 1 will give you practice with.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
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 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.
1 CS1100 Fall Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems CS100J:
1 CS100J Classes, stepwise refinement 14 Feb 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
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.
Principled approach to teaching OO first David Gries Computer Science, Cornell 1.
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.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
1 CS1110 Classes, stepwise refinement 12 Feb 2009 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
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 February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
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
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
Lecture 10 Review of classes
CS100J 21 February 2005 Casting About
CS1110 Classes, stepwise refinement 23 Sep 2009
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Can perform actions and provide communication
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
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Sit next to someone. Today, we do some work in pairs.
Inheritance.
Chapter 11 Inheritance and Polymorphism Part 1
CS 1110 Prelim I: Review Session Spring 2011
CS100J Classes, stepwise refinement 21 September 2005
CS100J 22 February 2005 More on Methods. Developing methods
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make apptmnt with Marschner or Gries. See consultant in ACCEL Lab See a TA. Peer tutoring (free). Olin 167 Prelim, Thurs, 6 Oct, 7-9:30PM Conflict? Complete assignment P1Conflict on CMS

2 The new-expression Some of you still are confused about how the new- expression is evaluated. If you do not understand how it is evaluated and cannot evaluate it yourself, you do not understand classes and objects. So much of object- oriented (OO) programming is embodied in evaluation of the new expression —that you must understand it. Next slides: We again go through evaluation of a new- expression. As we do it, copy everything we do onto your own paper. Don’t understand? Ask a question!! After the lecture, memorize what we did!

3 t= new Book(“Truth is all”, 2345) ; Above is an assignment statement. If we ask you to evaluate the expression in the assignment statement, you do that and nothing more. You don’t mention variable t at all! Execution of the assignment consists of 2 steps: 1.Evaluate the expression (here, new Book(…)) and 2.Store the value of the expression in the variable (here, t) Why mention t when discussing evaluation of the expression? We ask you to be more precise and careful in what you do than you have ever been —because programming requires it. Remember what the Director of Google Research said (see assignment A1 handout).

4 t= new Book(“Truth is all”, 2345) ; b7 Book Book(String, int) … other methods and fields In explaining how to evaluate the new-expression, you do NOT need to know what the definition of class Book is. You do NOT have to know what fields class Book has. Step 1. Draw an object of class Book; Step 2. Execute the constructor call; (you expect that it initializes the fields of b7) t= new Book(“Truth is all”, 2345) ; constructor call Step 3. Use the name of the new object (b7) as the value of the new- expression. b7 t If you want to complete the assignment, store the value in t. But it is NOT part of evaluating the new-expression.

5 Content of this lecture Go over miscellaneous points to round out your knowledge of classes and subclasses. There are a few more things to learn after this, but we will handle them much later. Inheriting fields and methods and overriding methods. Sec. 4.1 and 4.1.1: pp. 142–145 Purpose of super and this. Sec , pp. 144–145. More than one constructor in a class; another use of this. Sec , pp. 110–112. Constructors in a subclass —calling a constructor of the super-class; another use of super. Sec , pp. 147–148.

6 Employee c= new Employee(“Gries”, 1969, 50000); c.toString() a0 Object name “Gries” start 1969 salary 50, getName() setName(String n) … toString() equals(Object) toString() Employee c a0 Which method toString() is called? Overriding rule, or bottom-up rule: To find out which is used, start at the bottom of the class and search upward until a matching one is found. Terminology. Employee inherits methods and fields from Object. Employee overrides function toString. Sec. 4.1, page 142 This class is on page 105 of the text.

7 Purpose of super and this this refers to the name of the object in which it appears. super is similar but refers only to components in the partitions above. /** = String representation of this Employee */ public String toString() { return this.getName() + ", year ” + getStart() + ", salary ” + salary; } ok, but unnecessary /** = toString value from superclass */ public String toStringUp() { return super.toString(); } necessary Sec. 4.1, pages a0 Object name “Gries” start 1969 salary 50, getName() setName(String n) {…} toString() toStringUp() { …} equals(Object) toString() Employee

8 A second constructor in Employee Provide flexibility, ease of use, to user /** Constructor: a person with name n, year hired d, salary s */ public Employee(String n, int d, double s) { name= n; start= d; salary= s; } /** Constructor: a person with name n, year hired d, salary 50,000 */ public Employee(String n, int d) { name= n; start= d; salary= 50000; } First constructor Second constructor; salary is always 50,000 /** Constructor: a person with name n, year hired d, salary 50,000 */ public Employee(String n, int d) { this(n, d, 50000); } Another version of second constructor; calls first constructor Here, this refers to the other constructor. You HAVE to do it this way Sec , page 110

9 a0 Object name “Gries” start 1969 salary 10,000 Employee(String, int) toString() getCompensation() toString() … Employee Executive bonus Executive(String, int, double) getBonus() getCompensation() toString() 50,000 Calling a superclass constructor from the subclass constructor public class Executive extends Employee { private double bonus; /** Constructor: name n, year hired d, salary 50,000, bonus b */ public Executive(String n, int d, double b) { super(n, d); bonus= b; } The first (and only the first) statement in a constructor has to be a call on another constructor. If you don’t put one in, then this one is automatically used: super(); Principle: Fill in superclass fields first. Sec , page 147

10 Anglicizing an Integer anglicize(“1”) is “one” anglicize(“15”) is “fifteen” anglicize(“123”) is “one hundred twenty three” anglicize(“10570”) is “ten thousand five hundred seventy” /** = the anglicization of n. Precondition: 0 < n < 1,000,000 */ public static String anglicize(int n) { }

11 Principles and strategies Develop algorithm step by step, using principles and strategies embodied in “stepwise refinement” or “top-down programming. READ Sec. 2.5 and Plive p Take small steps. Do a little at a time Refine. Replace an English statement (what to do) by a sequence of statements to do it (how to do it). Refine. Introduce a local variable —but only with a reason Compile often Intersperse programming and testing Write a method specifications —before writing the bodies Separate your concerns: focus on one issue at a time

12 Principles and strategies Mañana Principle. During programming, you may see the need for a new method. A good way to proceed in many cases is to: 1. Write the specification of the method. 2. Write just enough of the body so that the program can be compiled and so that the method body does something reasonable, but no the complete task. So you put off completing this method until another time —mañana (tomorrow) —but you have a good spec for it. 3. Return to what you were doing and continue developing at that place, presumably writing a call on the method that was just “stubbed in”, as we say.