Class Hierarchy - Object Oriented Programming and Data Structures - Lecture Slides, Slides of Object Oriented Programming

Lecture from Object Oriented Programming and Data Structures course with following key points: Class Hierarchy, Staticcomponents, Specifications of Booleanfunctions, Testing, Test Cases for Number of Children, Method to String, Intro to Static Components, Java Application

Typology: Slides

2013/2014

Uploaded on 01/29/2014

sundar
sundar 🇮🇳

4.7

(9)

104 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The class hierarchy; static components
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Class Hierarchy - Object Oriented Programming and Data Structures - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

The class hierarchy; static components

Specifications of boolean functions

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

A bit about testing

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

“Child 2”

j mom pop

children

w

0

name

BFly

s

“Mumsie”

null mom

pop

children

null

1

name

BFly

j

“Opa”

null mom pop

children

null

1

name

BFly

b

“Popsi”

mom (^) null pop

children

b

2

name

BFly

w

“Child 1”

mom null pop

children

w

name

L

1

BFly

If L0 gets a mom,

say j0, the mom’s

number of children

must increase.

You should test this.

Class Object: the superest class of them all

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@af

W

lname “Obama”

ssn 123456789

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

Object

W@af

lname “Obama”

ssn 123456789

boss null

W

getSsn() …

toString() …

toString() in Object returns the name of the object: W@af

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 W@af

toString() … c.toString() calls this method

In appropriate places, the expression

c automatically does c.toString()

Another example of toString()

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

Point

x 9 y (^5)

Function toString should give the values in the

fields in a format that makes sense for the class.

Intro to static components

W@af

W

lname “Om”

boss (^) null

isBoss(W c) {

W@b

W

lname “Jo”

boss W@af

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 W@af x W@b

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

W@af

W

lname “Om”

ssn 35

boss (^) null

isBoss(W)

W@b

W

lname “Jo”

ssn 21

boss W@af

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();

y isBos(W,W)

W@af x W@b

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

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