1 CS1110 10 February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.

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 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;
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
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.
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.
RECURSION Lecture 7 CS2110 – Spring Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
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.
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:
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Unit 3: Java Data Types Math class and String class.
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.
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
From C++ to Java A whirlwind tour of Java for C++ programmers.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
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 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
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.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes.
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.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
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.
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.
Programming Fundamentals Enumerations and Functions.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
CS 100Lecture 201 Announcements P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday:
Lecture 5:Interfaces and Abstract Classes
Chapter 7: User-Defined Functions II
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Java Primer 1: Types, Classes and Operators
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 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
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
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Sit next to someone. Today, we do some work in pairs.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
CS 1110 Prelim I: Review Session Spring 2011
CS100J Classes, stepwise refinement 21 September 2005
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

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 PLive lectures 2.5.1– Prelim: Thu 19 Feb, 7:30 to 9:00, Baker 200 Review: Sun 15 Feb, 1–3, Phillips 101 Reading for next lecture: the same For today: Turn in Assignment 2. Pick up three handouts: Assignment 3, due next Monday. “Group” early on CMS and watch the course “Announcements” webpage. Prelim review notes Today’s slides (Suggestion: get a 3-ring binder and a 3-hole punch. Take notes on 3-hole-punched looseleaf paper).

2 The inside-out rule (see p. 83) Code in a construct can reference any of the names declared or defined in that construct, as well as names that appear in enclosing constructs. (If a name is declared twice, the closer one prevails.) File drawer for class Virus Virus a0 name getNameAndPop() {return name + virusPop;} Virus a1 name getNameAndPop() {return name + virusPop;} virusPop Remember frame boxes and figuring out variable references?

3 Method parameters participate in the inside-out rule: remember the frame. setName(String n) { name= n; } Virus a0 name setName(String name) { name= name; } Wrong a1 name Parameter n would be found in the frame for the method call. Parameter name “blocks” the reference to the field name.

4 A solution: this and super Within an object, this refers to the name of the object itself. File drawer for class Virus virusPop setName(String name) { this.name= name; } Virus a0 name setName(String name) { this.name= name; } Virus a1 name In folder a0, this refers to a0. In folder a1, this refers to a1.

5 A solution: using this and super Within a subclass object, super refers to the superclass. toString() { … } otherMethod { … … super.toString() … } Object a1 Elephant method equals() method toString() Because of the keyword super, this calls toString in the Object partition.

6 Strings are (important) objects that come with useful methods. String s= "abc d"; abc d Text pp. 175–181 discusses Strings Look in CD ProgramLive Look at API specs for String s.length() is 5 (number of chars) s.charAt(2) is 'c' (char at index 2) s.substring(2,4) is "c " (NOT “c d”) s.substring(2) is "c 4" " bcd ".trim() is "bcd" (trim beginning and ending blanks) DO NOT USE == TO TEST STRING EQUALITY! s == t tests whether s and t contain the name of the same object, not whether the objects contain the same string. Use s.equals(t) Note the “index (number) from 0” scheme:

7 Developing a string function /** Precondition: s contains a sequence of unsigned integers separated by commas (each possibly followed by blanks). There are no blanks or commas at the beginning and end of s. = s but with its first integer removed (remove also following comma, if there is one, and following blanks). E.g. s = "52, 0, 76385" Return "0, 76385" s = "000, 11" Return "11" s = "00" Return "" */ public static String removeInt(String s)

8 Developing a string function /** Precondition: s contains a sequence of unsigned integers separated by commas (each possibly followed by blanks). There are no blanks or commas at the beginning and end of s. = s IF the first integer isn’t 0 =s but with its first integer and delimiting comma and blanks removed if the first integer is originally 0 E.g. s = "52, 0, 76385" Return s s = "000, 11" Change s to "11" s = "00" Change s to "" */ public static String removeZero(String s)

9 Principles and strategies Principle: Write a method spec before you write the body. Mañana Principle: Write the specification of a method and “stub” it in, so that it can be compiled and produces something that allows further development. Put off its complete development until later. (Mañana means tomorrow, or an indefinite time in the future.) Compile often: Compiling often will help you catch syntax errors quickly and easily. Intersperse program development with testing: The worst thing you can do is to write a complete program and then begin testing. Because if there is an error, you have no idea where it is and how to find it. However, if you test each method as completely as possible after writing it, then any errors should be localized to that method.