1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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 and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
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 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
Slide 1. Slide 2 Administrivia Nate's office hours are Wed, 2-4, in 329 Soda! TA Clint will be handing out a paper survey in class sometime this week.
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,
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
Quote for the day: Computational thinking: a fundamental skill for everyone … [It] is … choosing an appropriate representation for a problem or modeling.
1 CS1110 Fall 2010 Instructors: David Gries & Lillian Lee CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering-
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 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
1 CS1110 Wrapper classes, stepwise refinement 27 Sept Prelim: Tuesday, 6 Oct, 7:30–9:00PM, Statler Auditorium Thursday: no reading. Be there or be square.
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.
1 CS1110 Lecture Sept Developing (String-processing) programs; class Vector; wrapper classes Prelim: 7:30-9PM Thursday 7 October. Last name A-K:
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
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.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
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 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.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
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 CS Nov 2009 Ragged arrays Reading for today: sec Reading for next time: chapter 16, applications and applets If you have a conflict with.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
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.
1 CS100J Spring Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
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.
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 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
Object-Oriented Concepts
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
CS1110 Classes, stepwise refinement 23 Sep 2009
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS1110 Wrapper classes, stepwise refinement 23 Feb
Review Session.
Lecture 10: Inheritance Subclasses and superclasses
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
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
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
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
CS100J Classes, stepwise refinement 21 September 2005
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes. Prelim 7:30pm-9:00pm Thursday, 8 Oct (we’ll announce which students should go to Olin 255 and which to Upson B17 later) If you have a conflict but did not receive an acknowledgment yesterday, ASAP to tell Maria Witlox what your conflict with the CS1110 prelim Review session: 1-3pm Sunday, 4 Oct., Phillips 101 Next time: Wrapper classes (Section 5.1) Have your iclickers out, set to frequency AB. Reminders: finish revising A1s; A3 due Wed at midnight; pick up uncollected quizzes up front.

2 Help: Get it now if you need it!! Operators are standing by!!! See for drop-in office hours and making appointments with Prof Gries, Prof Lee or any of the 5 graduate TAs; or click the link at the bottom of that page to view the consultants’ evening drop-in hours schedule. Free peer tutoring for engineering students is available: ask in Olin 167 or see services/learning/peer-tutoring/index.cfmhttp:// services/learning/peer-tutoring/index.cfm

3 iClicker continuous-feedback experiment All through class, try updating us on your “status”: A.I get it, and you could speed up. B.I get it, but don’t go any faster, it’s perfect. C.I’m getting most but not all of it, but prefer that you just keep going anyway. D.Could you repeat that just once more? E.You lost me.

4 An application: String processing, stepwise refinement, usefulness of Javadoc, problem solving Strings are a particularly important type, because lots of information (especially non-numerical data) is stored in Strings. For example, many webpages can, for many intents and purposes, be considered to be Strings. Application: “scraping” (extracting) live stock quotes from the Web.

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

6 Odds and ends on classes: outline of remainder of lecture Issues related to sub-classes: Inheriting fields and methods and overriding methods. Purpose of super and this. (Sec. 4.1 and 4.1.1: pp. 142–145) Constructors in a subclass —calling a constructor of the super-class. ( Sec , pp. 147–148) [There are a few more things regarding classes to learn after this, but we will handle them much later.] More than one constructor in a class; another use of this. (Sec , pp. 110–112)

7 Why provide more than one constructor? public Organism(int lev, int m, String nn) { …} Sec , page 110 Doing so is better for the user. From A1 (we assume you remember the specs): So, the user can write new Organism(4) instead of new Organism(4, 0, null). public Organism(int lev) { Organism(lev, 0, null); } For the programmer, it’d be great to have the one-parameter constructor call the other: We wish we could say this! public Organism(int lev) { …} But you HAVE to do it “this” way. Here, this refers to the other constructor. this(lev, 0, null);

8 Issues related to sub-classes The ability to extend existing subclasses to reuse/refine existing behavior is a terrific aspect of object-oriented programming. Example: modeling sulfur bacteria as photosynthesizing organisms that come in purple or green variations. a0 Object level 0 SBact(String) getColor() eat(Organism) toString() equals(Object) toString() Organism SBact color “green” eat(Organism) toString() getLevel() Organism(int) Organism(int, int, String) … sb a0 … We want sb.eat(victim) to have the effect appropriate for sulfur bacteria (i.e., nothing happens) rather than the effect appropriate for generic Organisms. (Thank you, Wikipedia.) [Program and tester will be posted to the course website.]

9 For the call sb.eat(v), which method eat is called? Overriding rule or bottom-up rule: Start at the bottom of the folder and search upward until a matching method is found. Terminology. SBact inherits methods and fields from Organism. Sbact overrides eat and toString. Sec. 4.1, page 142 sb a0 Object level 0 SBact(String) getColor() eat(Organism) toString() equals(Object) toString() Organism SBact color “green” eat(Organism) toString() getLevel() Organism(int) Organism(int, int, String) … … …

10 Purpose of super and this Suppose we’re overriding a method in order to modify it just a bit; so, we’d like to refer to the overridden method. The word super refers only to components in the partitions above it. Sec. 4.1, pages a0 Object level 0 SBact(String) getColor() eat(Organism) toString() equals(Object) toString() Organism SBact color “green” eat(Organism) toString() getLevel() Organism(int) Organism(int, int, String) … … … this refers to the name of the object in which it appears. We could have written this.getColor(), but it wasn’t necessary to. /** = String like A3 requires for Organism, but with “ SBact. ” in front. */ public String toString() { return getColor() + “ SBact. “ + super.toString(); } necessary

11 Calling (reusing) a superclass constructor from the subclass constructor public class SBact extends Organism { private String color; /** Constructor: A sulfur bacterium of color c [etc. Full program will be posted.]*/ public SBact(String c) { super(0); /* default Org. values, * lowest level */ color= c; } The first (and only the first) statement in a constructor has to be a call to a constructor of the superclass. If you don’t put one in, then this one is automatically used: super(); This corresponds to a natural principle: Fill in superclass fields first. Sec , page 147 a0 Object level 0 SBact(String) getColor() eat(Organism) toString() equals(Object) toString() … Organism SBact color “green” eat(Organism) toString() … Organism(int) … … …