CS100J Final Class on Classes 10 February 2005

Slides:



Advertisements
Similar presentations
NHASFAA Conference- October 28, 2011 Norm Craige Associate Director Student Financial Services University of Vermont.
Advertisements

CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Faith Works: The Balancing Act Romans 14:1 –
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.
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.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
North Carolina Nursery & Landscape Association Inside The Box Customer Service Part 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.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
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…..
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.
Ch. 1.2 Health and Wellness 1.6 Components of Health 2.Wellness Continuum 3.4 Area’s of Influence 1.6 Components of Health 2.Wellness Continuum 3.4 Area’s.
1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes.
Integrating The Pacific Institute (TPI) Thought Patterns for Success.
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 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
1 CS100J 27 September 2005 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
ENTREPRENEURS UNIVERSITY OF KENTUCKY E-DISCOVERY.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
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.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Back to School Night
University of Kentucky E-discovery
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
Back to School Night
Mr. Chavel School Year Teacher Talk.
CS100J 21 February 2005 Casting About
Mr. Chavel School Year Teacher Talk.
CS1110 Classes, stepwise refinement 23 Sep 2009
Lecture 15: More Inheritance
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
ATTITUDE STRESS CULTURE
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS1316: Representing Structure and Behavior
Inheritance I Class Reuse with Inheritance
Charles Swindoll Charles Rozell "Chuck" Swindoll is an evangelical Christian pastor, author, educator, and radio preacher. He founded Insight for Living,
Extending Classes.
ELIST - Transnational Training II on Entrepreneurial Capacity Building 15th – 19th February 2016, Athens, GREECE.
CS1316: Representing Structure and Behavior
CMSC 202 Inheritance.
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
Class Reuse with Inheritance
Beginning Teacher Support Beaufort County Schools
Achieving Success In Middle School.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Inheritance.
Lecture 15: Inheritance II
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
Career Up! Coach Up!.
Attraction and Retention of Top Talent
Presentation transcript:

CS100J Final Class on Classes 10 February 2005 Attitude by Charles Swindoll. The longer I live, the more I realize the impact of attitude on life. Attitude, to me, is more important than facts, the past, education, money, failures, circumstances, success, and what other people think, or say, or do. It is more important than appearance, giftedness, or skill. It will make or break a company, a church, a home. The remarkable thing is we have a choice every day regarding the attitude that we will embrace for that day. We cannot change the past. We cannot change the fact that people will act in a certain way. We cannot change the inevitable. The only thing we can change is our attitude. I am convinced that life is 10% what happens to me and 90% how I react to it. And so it is with you also. We are all in charge of our own attitude.

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 255-8240 for an appointment with Gries. • See a consultant in the eng. library during the day and at Purcell Union in the evening. --they aren't very busy now. Click on "consulting hours" on the home page for the course. • Peer tutoring (free). On http://www.engineering.cornell.edu, click on "student services". On the page that comes up, click on "Learning Initialives (L.I.F.E.) in the left column, upper part. Then, click on "peer tutoring" in the left column.

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 that, but we’’ll handle them much later. Inheriting fields and methods and Overriding methods. Sec. 4.1 and 4.1.1: pp. 142–145 Function toString. Sec. 3.1.4, pp. 112–113. 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. 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. 4.3.1.) Constructors in a subclass —calling a constructor of the super-class. Sec. 4.1.3, pp. 147–148.

public class Employee {/ 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. It will also be placed on the web site

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: 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.

In Object, all toString can do is to give the name on the folder. Purpose of function toString: to give a string repre-sentation of the folder (object) in which it appears. Sec. 3.1.4, page 112 In Object, all toString can do is to give the name on the folder. In Employee, toString can tell you the values of the fields /** = String representation of this Employee */ public String toString() { return getName() + ", year ” + getStart() + ", salary ” + salary; } Nice Java rule. If you use the name c of a folder in a place where a String is needed, Java uses the value of c.toString().

Purpose of super and this Sec. 4.1, pages 144-145 Purpose of super and this Use this to refer to the object in which it appears Use super to refer 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

/** 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

Method equals in class Object. Sec. 4.3.1, page 154 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 Write equals in class Employee /** = “e is an Employee, with the same fields as this Employee */ public boolean equals(Employee e) { return e != null && this.name == e.name && this. start == e.start && this. salary == e.salary; } 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.

/** 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; } Subclass Executive 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.

public class Executive extends Employee { private double bonus; public Executive(String n, int d, double b) { … } public double getBonus() { … } public double getCompensation() { … } public String toString() { …} } Subclass Executive and its folders a0 Object name “Gries” start 1969 salary 10,000 Employee(String, int) setName(String) toString() getCompensation() equals(Object) toString() Employee Executive bonus Executive(String, int, double) getBonus() getCompensation() toString() 50,000.00 50,000

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 can 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.