CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

1 For more info: CS Sep 2008 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston to.
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
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;
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
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!
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/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
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.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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 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.
CS/ENGRD 2110 SPRING 2014 Lecture 4: The class hierarchy; static components 1.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
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.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
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.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
1 For more info: CS February 2009 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 SPRING 2016 Lecture 4: The class hierarchy; static components 1.
Arguments to method main, Packages, Wrapper Classes, Characters, Strings CS2110, Recitation 2.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Object Oriented Programming Lecture 2: BallWorld.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Topic: Classes and Objects
Lecture 10 Review of classes
Java Primer 1: Types, Classes and Operators
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Fall2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
CSC 113 Tutorial QUIZ I.
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
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 4: The class hierarchy; static components
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Methods/Functions.
Presentation transcript:

CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1

References to text and JavaSummary.pptx 2  A bit about testing and test cases  Class Object, superest class of them all. Text: C.23 slide 30  Function toString() C.24 slide  Overriding a method C15–C16 slide  Static components (methods and fields) B.27 slide 21, 45  Java application: a program with a class that declares a method with this signature: public static void toString(String[])

Homework 3 1.Read the text, Appendix A.1–A.3 2.Read the text, about the if-statement: A.38–A.40 3.Visit course website, click on Resources and then on Code Style Guidelines. Study 2. Format Conventions 4.5 About then-part and else-part of if-statement

Specifications of boolean functions 4 /** Return true if this Butterfly is male and false if not. */ public boolean isMale() /** Return “this Butterfly is male”. */ public boolean isMale() Says same thing. Shorter, no case analysis. Think of it as return value of sentence “this Butterfly is male” Do you say, “it returns absolute value of -20? Of course not. Mathematicians may say simply “that’s the absolute value of 60 abs(-20) /** = “this Butterfly is male”. */ Read as: the call isMale() equals the value of the sentence “this Butterfly is male”.

What is “the name of” the object? 5 The name of the object below is It contains a pointer to the object –i.e. its address in memory, and you can call it a pointer if you wish. But it contains more than that. “Mumsie” null mom pop children null 1 name BFly b BFly Variable b, declared as Bfly b; contains not the object by the name of the object or a pointer to the object.

A bit about testing 6 Test case: Set of input values, together with the expected output. Develop test cases for a method from its specification --- even before you write the methods body. /** = number of vowels in word w. Precondition: w contains at least one letter and nothing but letters*/ public int numberOfVowels(String w) { … } Developing test cases first, in “critique” mode, can prevent wasted work and errors. How many vowels in each of these words? creek syzygy

Test cases for number of children 7 “Child 2” j0 mompop children w0 0 name BFly s0 “Mumsie” null mom pop children null 1 name BFly j0 “Opa” null mompop children null 1 name BFly b0 “Popsi” null mompop children b0 2 name BFly w0 “Child 1” null mom pop children w0 name L0 1 BFly If L0 gets a mom, say j0, the mom’s number of children must increase. You should test this.

Class W (for Worker) 8 /** Constructor: worker with last name n, SSN s, boss b (null if none). Prec: n not null, s in with no leading zeros.*/ public W(String n, int s, W b) /** = worker's last name */ public String getLname() /** = last 4 SSN digits */ public String getSsn() /** = worker's boss (null if none) */ public W getBoss() /** Set boss to b */ public void setBoss(W b) W lname “Obama” ssn boss null W(…) getLname() getSsn() getBoss() setBoss(W) Contains other methods! toString() equals(Object) hashCode()

Class Object: the superest class of them all 9 Java: Every class that does not extend another extends class Object. That is, public class W {…} is equivalent to public class W extends Object {…} W lname “Obama” ssn boss null W(…) getLname() getSsn(), getBoss() setBoss(W) Object toString() equals(Object) hashCode() We draw object like this We often leave off the top partition to reduce clutter; we know that it is always there

Method toString 10 Object lname “Obama” ssn boss null W getSsn() … toString() … toString() in Object returns the name of the object: Java Convention: Define toString() in any class to return a representation of an object, giving info about the values in its fields. New definition of toString() overrides the definition in partition Object c toString() … c.toString() calls this method In appropriate places, the expression c automatically does c.toString()

Method toString 11 Object lname “Obama” ssn boss null W getSsn() … toString() … toString() in Object returns the name of the object: public class W { … /** Return a representation of this object */ public String to String() { return “Worker ” + lname + “.” + “ Soc sec: …” + getSSn() + “.” + (boss == null ? “” : “Boss ” + boss.lname + “.”); } c toString() … c.toString() calls this method

Another example of toString() 12 /** An instance represents a point (x, y) in the plane */ public class Point { private int x; // x-coordinate private int y; // y-coordinate … /** = repr. of this point in form “(x, y)” */ public String toString() { return “(” + x + “, ” + y + “)”; } Point x 9 y 5 Function toString should give the values in the fields in a format that makes sense for the class. (9, 5)

Intro to static components 13 W lname “Om” boss null isBoss(W c) { …} W lname “Jo” boss isBoss(W c) { return this == c.boss; } /** = “this object is c’s boss”. Pre: c is not null. */ public boolean isBoss(W c) { return this == c.boss; } keyword this evaluates to the name of the object in which it appears x.isBoss(y) is false y x y.isBoss(x) is true Spec: return the value of that true-false sentence. True if this object is c’s boss, false otherwise

Intro to static components 14 W lname “Om” ssn 35 boss null isBoss(W) W lname “Jo” ssn 21 boss isBoss(W) /** = “this object is c’s boss”. Pre: c is not null. */ public boolean isBoss(W c) { return this == c.boss; } /** = “b is c’s boss”. Pre: b and c are not null. */ public boolean isBoss(W b, W c) { return b == c.getBoss(); } isBos(W,W) isBoss(W,W) y x Body doesn’t refer to any field or method in the object. Why put method in object?

Intro to static components 15 W lname “Om” ssn 35 boss null isBoss(W) W lname “Jo” ssn 21 boss isBoss(W) /** = “b is c’s boss”. Pre: b and c are not null. */ public static boolean isBoss(W b, W c) { return b == c.getBoss(); } isBos(W,W) y x static: there is only one copy of the method. It is not in each object Box for W (objects, static components) x.isBoss(x, y) y.isBoss(x, y) Preferred: W.isBoss(x, y)

Java application 16 Java application: bunch of classes with at least one class that has this procedure: public static void main(String[] args) { … } Type String[]: array of elements of type String. We will discuss later Running the application consists of calling method main Convention: if method main doesn’t use parameter args, then call it with argument null

One use of static variable: maintain info about all objects 17 W lname “Bid” W “Ob” lname numObjects 2 Box for W public class W { private static int numObjects; … } To have numObjects contain the number of Objects of class W that have been created, simply increment it in constructors /** Constructor: */ public W(…) { … numObjects= numObjects + 1; }