CS1110 Classes, stepwise refinement 23 Sep 2009

Slides:



Advertisements
Similar presentations
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, .
Advertisements

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.
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!
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.
0 CS100J September 2007 CS100J: 12 weeks programming using Java, 2 using Matlab. David Gries. CS100M: 7 weeks of Matlab and 7 of Java. Daisy Fan. CS100H:
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 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
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,
1 CS1110 Fall 2011: David Gries & Steve Marschner CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering- type problems.
1 CS1110 Fall 2010 Instructors: David Gries & Lillian Lee CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering-
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.
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.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
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.
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 CS1100 Fall Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems CS100J:
1 CS1110 Fall Instructors: David Gries & Lillian Lee CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering-
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.
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.
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
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.
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 February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.
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
CS1110 Spring Instructor: David Gries
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
Lecture 15: More Inheritance
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS1110 Wrapper classes, stepwise refinement 23 Feb
Lecture 9-2: Interacting with the Superclass (super);
CS100J Final Class on Classes 22 September 2005
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
CS1316: Representing Structure and Behavior
CS1316: Representing Structure and Behavior
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
Java Programming, Second Edition
Lecture 14: Inheritance Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Inheritance.
Lecture 15: Inheritance II
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 11 Inheritance and Polymorphism
Building Java Programs
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:

CS1110 Classes, stepwise refinement 23 Sep 2009 Next: wrapper classes. Section 5.1 of class text Miscellaneous points about classes. More on stepwise refinement. Prelim 7:30-9:00 Thursday, 7 October, Olin Hall 155 & 255 Review session: 1:00-3:00, Sunday, 3 Oct., Philips 101 Prelim conflict? Email Maria Witlox by Friday. Tell her what the conflict is (which course, work, reason for being out of town, etc.) mwitlox@cs.cornell.edu

A1: 130 done 82 to go Let’s finish early this weekend. You can still work on A3 as your A1 iterative process proceeds. But the A1 part must be completed before you submit A3. Points are deducted on A3 if there are errors in A1.

Help: Get it now if you need it!! • Call Cindy 255-8240 for an aptmnt with David Gries. • Email Lillian Lee to make an aptmnt: llee@cs.cornell.edu • See a consultant in the ACCEL Lab: Sun, Mon, Tues, Wed, Thurs during office hours. • See a TA. • Peer tutoring (free). Ask in Olin 167 or visit http://www.engineering.cornell.edu, click on "student services". On the page that comes up, click on "Engineering Learning Initiatives (ELI.) " in the left column, upper part. Then, click on "peer tutoring" in the left column.

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. 4.1.1, pp. 144–145. More than one constructor in a class; another use of this. Sec. 3.1.3, pp. 110–112. Constructors in a subclass —calling a constructor of the super-class; another use of super. Sec. 4.1.3, pp. 147–148. Then, we develop a nice function to anglicize integers, e.g. for 235, produce “two hundred thirty five”.

Employee c= new Employee(“Gries”, 1969, 50000); c.toString() c a0 Sec. 4.1, page 142 c a0 a0 Object name “Gries” start 1969 salary 50,000.00 getName() setName(String n) … toString() equals(Object) toString() Employee 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. This class is on page 105 of the text. Terminology. Employee inherits methods and fields from Object. Employee overrides function toString.

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. Sec. 4.1, pages 144-145 a0 Object name “Gries” start 1969 salary 50,000.00 getName() setName(String n) {…} toString() toStringUp() { …} equals(Object) toString() Employee /** = 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

/** Constructor: a person with name n, year hired d, salary s */ A second constructor in Employee Provide flexibility, ease of use, to user Sec. 3.1.3, page 110 /** 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; } First constructor /** Constructor: a person with name n, year hired d, salary 50,000 */ public Employee(String n, int d) { name= n; start= d; salary= 50000; } 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

Principle: Fill in superclass fields first. 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; } Sec. 4.1.3, page 147 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 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.

public class Executive extends Employee { public Executive(String n, int d, double b) { bonus= b; } One constructor in Employee First statement in constructor: constructor call. If none, Java inserts: super(); 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 Is above program okay? A. Compiles with no change B. Compiles with super() inserted C. Doesn’t compile

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

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. 2-5. • 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 method specifications —before writing the bodies • Separate your concerns: focus on one issue at a time

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.

What numbers should we look at first? A: Small numbers B: Numbers >= 100 C: Numbers >= 1000

What numbers should we look at first? C: 0..19 D: 1..19

How many test cases do we need to test ang19? B: 2 E: 5 C: 10 D: 19