1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.

Slides:



Advertisements
Similar presentations
The human brain … … tricks us whenever it can!. The phenomenal power of the human mind I cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg.
Advertisements

I hope you like & find the following useful. Please press enter, or click anywhere on the screen to continue. You can navigate from the bottom left hand.
BT101: Hermeneutics Introduction. A. Description of Hermeneutics 1. General Hermeneutics The study of the activity of interpretation;
CS100J 18 September 2003 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are,
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.
What is science? Science: is a process by which we gain knowledge deals only with the natural world collects & organizes information (data/evidence) gives.
Sensation and Perception Chapter 3. Psychophysics This is how we experience our physical world. Classroom demo judging weight of pill bottles. Which one.
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.
Logo Design. UNTITLED Cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg. The phaonmneal pweor of the hmuan mind: aoccdrnig to a rscheearch.
Inclusive Learning Through Technology Damian Gordon.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
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.
T HE H UMAN M IND. The phaonmneal pwer of the hmuan mnid Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deson’t mttaer in what oredr the ltteers.
What do you see?. O lny srmat poelpe can raed tihs. I cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg. The phaonmneal pweor.
Teaching reading.
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.
Taxonomy, Ontology, Semantics and Other Painful Things By Francis Hsu
~ Thought Journal ~ SILENTLY read the following passage. When you are finished, SILENTLY write down your reaction in your thought journal. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
Editing Documents Dr. Anatoliy Tmanov Pennsylvania State University.
The phenomenal power of the human mind   I cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg.The phaonmneal pweor of the hmuan mnid!
Please read this sentence and count the number of F’s:
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.
The human brain … … tricks us whenever it can!. The human mind is so non-literal! I cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg.
Sensation & Perception
I cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg. The phaonmneal pweor of the hmuan mnid. Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy,
Read each slide for directions
The Eye and Optical Illusions Chatfield Senior High.
1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes.
Readability Make sure at least the first and last letter of your word are very readable. If some of the other in between letters must sacrifice their readability.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
1.
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.
Welcome to Group Dynamics LDSP 351 Dr. Crystal Hoyt.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
Inspiring Youth to Live their Dreams! Scott Shickler Founder & CEO.
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.
Aoccdrnig to rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoatnt tihng is taht the frist.
What Is Neuropsychological (Neurocognitive) Testing?
CS100J 21 February 2005 Casting About
The human brain … … tricks us whenever it can!.
The phenomenal power of the human mind   I cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg The phaonmneal pweor of the hmuan mnid!
Elements of Effective Literacy Instruction in Grades 5-8
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
Please read the sign..
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
Even though the next page may look weird, you can still read it!
Unit 4: Sensation & Perception
Science and the Scientific Method
Closing IBS Lecture Fall 2008.
Sensation and Perception
Science and the Scientific Method
Sabotage Effective Communication
Human factors Age and vision.

Science and the Scientific Method
How does your brain perceive objects?
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 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is that the frsit and lsat ltteer be at the rghit pclae. The rset can be a total mses and you can sitll raed it wouthit porbelm.Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe. Miscellaneous points about classes. More on stepwise refinement. CMS allows you to submit an assignment several times. We grade only the last one submitted (but access to all of them). Prelim 7:30-9:00 Thursday, 22 Feb. Review session: 1:00-3:00, Sunday, 18 Feb., in Philips 101

2 Help: Get it now if you need it!! One-on-one help from TAs. For info, get on the course website and click "Staff-info". Call Cindy Pakkala for an appointment with Gries. See a consultant in the ACCEL Green room 2:30 to 11:00 (to 6:00 on Fri-Sat). They aren't very busy now. Peer tutoring (free). On click on "student services". On the page that comes up, click on "Learning Initiatives (L.I.F.E.) in the left column, upper part. Then, click on "peer tutoring" in the left column. Take an AEW courses. Ask in Olin 167.

3 Notes on assignment A3 3. Testing for null /** = ”r is this Elephant’s brother". Precondition: r not null. */ public boolean isBrotherOf(Elephant r) { // No need to test for null. It’s the caller’s duty not to have r null. } /** = ”r1 and r2 are not null, and r1 and r2 are siblings */ public static boolean areSiblings(Elephant r1, Elephant r2) { // The result depends on r1, r2 not being null, and the // return expression should somehow include that test. return … } In ElephantTester, when testing areSiblings, need to test calls like areSiblings(m1, null) areSiblings(null, m2)

4 Content of this lecture This lecture contains some final miscellaneous points to round out your knowledge of classes and subclasses. There are a few more things to learn after this, but we’’ll 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. Method equals in class Object. Sec. 4.3 and 4.3.1, pp. 154–155. (We do not cover the method at the end of Sec ) Constructors in a subclass —calling a constructor of the super-class. Sec , pp. 147–148.

5 public class Employee {/** Instance: a person's name, year hired, and salary */ private String name; // Employee's name private int start; // Year hired private double salary= 50000; // Salary /** Constructor: a person with name n, year hired d, salary s */ public Employee(String n, int d) { name= n; start= d; salary= s;} /** = name of this Employee */ public String getName() { return name; } /** Set the name of this Employee to n */ public void setName(String n) { name= n; } /** = year this Employee was hired */ public int getStart() { return start; } /** Set the year this Employee was hired to y */ public void setStart(int y) { start= y; } /** = Employee's total compensation (here, the salary) */ public double getCompensation() { return salary; } /** Change this Employee’s salary to d */ public void changeSalary(double d) { salary= d; } /** = String representation of this Employee */ public String toString() { return getName() + ", year " + getStart() + ", salary " + salary; } } This class is on page 105 of the text.

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: To find out which is used, start at the bottom of the class and search upward until a matching one is found. Also called the bottom-up rule. Terminology. Employee inherits methods and fields from Object. Employee overrides function toString. Sec. 4.1, page 142

7 Purpose of super and this In a method, this equals the name of the object in which it appears. super is similar but refers only to components in the super-class partition of the object (and 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

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 Subclass Executive/** An executive: an employee with a bonus. */ public class Executive extends Employee { private double bonus; // yearly 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; } /** = this executive’s bonus */ public double getBonus() { return bonus; } /** = this executive’s yearly compensation */ public double getCompensation() { return super.getCompensation() + bonus; } /** = a representation of this executive */ public String toString() { return super.toString() + ", bonus " + bonus; } } super(n,d) calls a constructor in the super- class to initialize the superclass fields super. means that the function in the superclass will be called.

10 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 to a constructor of the superclass. If you don’t put one in, then this one is automatically used: super(); Principle: Fill in superclass fields first. Sec , page 147

11 Function does not override equals in Object because the parameter has a different type. It’s a new, different function. We’ll fix redo this function later in the course. Method equals in class Object. /** = “the name of this object is the same as the name of obj */ public boolean equals(Object obj) { return this == obj; } a0 Object name “Gries” start 1969 salary 50,000 equals(Object) Employee /** = “e is an Employee, with the same fields as this Employee */ public boolean equals(Employee e) { return e != null && this.name.equals(e.name) && this. start == e.start && this. salary == e.salary; } Write equals in class Employee Sec , page 154 Don’t use == with Strings

12 More about equals To test whether two String values contain the same string, use function equals. a0 “xyz”String a1 “xyz”String a0 s0 a1 s1 s1.equals(s2) is true. s1 == s2 asks whether a0 == a1, which is false To test whether two Rhinos are the same Rhino, use ==. To test whether r2’s father is r1, use r2.father == r1 a3 == a3 a3 Rhino father null a4 Rhino father a3 r1 a4 r2