1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.

Slides:



Advertisements
Similar presentations
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Advertisements

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, .
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
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.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
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 CS1110 Lecture Sept Developing (String-processing) programs; class Vector; wrapper classes Prelim: 7:30-9PM Thursday 7 October. Last name A-K:
The Java Inheritance Hierarchy CSIS 3701: Advanced Object Oriented Programming.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
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 CS2111 Spring Instructor: David Gries You job: Come to class each week, take part. My job in first few weeks: Get you to under- stand and be facile.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
10-Nov-15 Java Object Oriented Programming What is it?
1 CS100J Wrapper classes, stepwise refinement 19 Feb When insults had class "A modest little person, with much to be modest about." Churchill "I never.
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.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 CS1110 Wrapper classes, stepwise refinement 30 September Chocolate bar has grooves that divide it into squares. This is one example. Others would be.
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 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 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Lecture 10 Review of classes
Chapter 11 Inheritance and Polymorphism
CS100J 21 February 2005 Casting About
CS1110 Classes, stepwise refinement 23 Sep 2009
Lecture 15: More Inheritance
Chapter 10 Thinking in Objects
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS1110 Wrapper classes, stepwise refinement 23 Feb
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 9 Inheritance and Polymorphism
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 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
Inheritance.
Lecture 15: Inheritance II
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
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 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text as a reference. 1. Want to know about type int? Look it up in text. 2. Want to know about packages? Look up “packages”, 3. How is the new-exp evaluated? Look it up. 4. etc.

2 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 , pp. 144–145. More than one constructor in a class; another use of this. Sec , pp. 110–112. Constructors in a subclass —calling a constructor of the super-class; another use of super. Sec , pp. 147–148. Wrapper classes. Read Section 5.1.

3 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, 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. Terminology. Employee inherits methods and fields from Object. Employee overrides function toString. Sec. 4.1, page 142 This class is on page 105 of the text.

4 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. /** = String representation of this Employee */ public String toString() { return this.getName() + ", year ” + getStart() + ", salary ” + salary; } ok, but unnecessary /** = name of this object */ public String toStringUp() { return super.toString(); } necessary Sec. 4.1, pages a0 Object name “Gries” start 1969 salary 50, getName() setName(String n) {…} toString() toStringUp() { …} equals(Object) toString() Employee

5 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

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

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

88 Wrapper classes. Read Section 5.1 a0 Integer ??? 5 Soon, we’ll wish to deal with an int value as an object. "Wrapper class" Integer provides this capability. An instance of class Integer contains, or "wraps", one int value. Can’t change the value. immutable. Integer(int) Integer(String) toString() equals(Object) intValue() Instance methods: constructors, toString(), equals, intValue. Static components: MIN_VALUE MAX_VALUE toString(int) toBinary(int) valueOf(String) parseInt(String) Static components provide important extra help.

99 Sandwich wrapper Spring rolls wrappers cupcake wrapper a0 Integer ??? 5 What is a wrapper? Something that holds another thing –wraps around it an int wrapper

10 wriggle wrapper

11 Each primitive type has a corresponding wrapper class. When you want to treat a primitive value of that type as an object, then just wrap the primitive value in an object of the wrapper class! Primitive typeWrapper class intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean Each wrapper class has: Instance methods, e.g. equals, constructors, toString, Useful static constants and methods. You don't have to memorize the methods of the wrapper classes. But be aware of them and look them up when necessary. Use Gries/ Gries, Section 5.1, and ProgramLive, 5-1 and 5-2, as references. Integer k= new Integer(63); int j= k.intValue();

12 Class Vector 12 An instance of class Vector maintains an expandable/shrinkable list of objects. Use it whenever you need to maintain a list of things. Values of primitive types cannot be placed directly into the list of a Vector. That’s why we have the wrapper classes. In the interactions pane, we will do a few things, like this: import java.util.*; Vector v= new Vector(); v v.add(new Integer(2)); v.add(3); v.add(‘c’); In newer versions of Java, v.add(1) is allowed; the 1 is wrapped in an Integer object and the name of that object is added to v. Doesn’t work in older versions.