darrell bethea june 7, 2011 · returning a value from a method does not print it out to the screen...

26
Darrell Bethea June 7, 2011

Upload: others

Post on 08-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Darrell BetheaJune 7, 2011

Page 2: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

3

Page 3: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Some review

More inheritance

4

Page 4: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Returning a value from a method does NOT print it out to the screen◦ The returned value is given to the caller of the

method◦ It is up to the method caller to do something with

this value

6

Page 5: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

public class NumberUtils{ private static Random rnd = new Random(); public static int roll20SidedDie() { return rnd.nextInt(20) + 1; }}

public class Tester{ public static void main(String[] args) { NumberUtils.roll20SidedDie(); // what does this do? }}

Generates a random die roll, but it ignores the value returned from the roll20SidedDie method

Have we printed anything to the screen?

7

Page 6: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

public class NumberUtils{ private static Random rnd = new Random(); public static int roll20SidedDie() { return rnd.nextInt(20) + 1; }}

public class Tester{ public static void main(String[] args) { int num = NumberUtils.roll20SidedDie(); // what does this do? }}

Puts the random number returned by roll20SidedDie into the variable num

Have we printed anything to the screen?

8

Page 7: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

public class NumberUtils{ private static Random rnd = new Random(); public static int roll20SidedDie() { return rnd.nextInt(20) + 1; }}

public class Tester{ public static void main(String[] args) { int num = NumberUtils.roll20SidedDie(); // what does this do? System.out.println(num); }}

Puts the random number returned by roll20SidedDie into the variable num

Then prints the value of num to the screen

9

Page 8: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

What is a base class? What is a derived class?

10

Page 9: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

We have discussed before how classes of objects can have relationships

11

Person

Student Employee

Undergrad Grad

Masters Doctoral Nondegree

Faculty Staff

Transportation

Car Airplane Animal

ElephantHorse Camel

Page 10: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

12

AnimalReptile MammalHumanCrocodile Whale

Animal

Reptile Mammal

HumanCrocodile Whale

Page 11: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

This inheritance relationship is known as an is-a relationship

A Doctoral student is a Grad student A Grad student is a Student A Student is a Person

Is a Person a Student?◦ Not necessarily!

13

Page 12: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Define a general class Later, define specialized classes based on the

general class These specialized classes inherit properties

from the general class

14

Page 13: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

public class Person{ private String name; public Person() { name = “No name yet”; } public void setName(String newName) { name = newName; } public String getName() { return name; }}

15

Person

- name

+ setName(String newName) : void+ getName() : String

Page 14: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

public class Student extends Person{ private int id; public Student() { super(); id = 0; } public Student(String stdName, int idNumber) { setName(stdName); setID(idNumber); } public void setID(int idNumber) { id = idNumber; } public int getID() { return id; }}

16

Student

- id

+ setID(int idNumber) : void+ getID() : int

Person

- name

+ setName(String newName) : void+ getName() : String

Page 15: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

What if the class Person had a method called printInfo?

public class Person{ // a bunch of other stuff // ... public void printInfo() { System.out.println(name); }}

17

Page 16: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

What if the class Student also had a method called printInfo?

public class Student extends Person{ // a bunch of other stuff // ... public void printInfo() { System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); }}

18

Page 17: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Both Person and Student have a printInfo() method

Student std = new Student("John Smith", 37183);std.printInfo(); // calls Student’s printInfo method, // not Person’s

Output would be:Name: John SmithID: 37183

19

Page 18: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

You often want derived classes to perform custom behavior

For example, drawing shapes

20

public class Shape{ public void draw(Graphics g) { }}

public class Circle extends Shape{ public void draw(Graphics g) { g.drawOval(…arguments…); }}

public class Rectangle extends Shape{ public void draw(Graphics g) { g.drawRect(…arguments…); }}

Page 19: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Given this inheritance heirarchy…

21

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 20: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Person p = new Person();◦ Yes!

22

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 21: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

HighJumper h = new HighJumper();◦ Yes!

23

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 22: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Person p = new Athlete();◦ Yes! An Athlete is a Person, so this is okay

24

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 23: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Skydiver s = new Person();◦ No! A Person is not necessarily a Skydiver, so

this is illegal

25

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 24: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Athlete ath = new Athlete();XGamesSkater xgs = ath;◦ No! An Athlete is not necessarily an XGamesSkater,

so this is illegal

26

Person

Athlete

HighJumper

Skydiver

ExtremeAthlete

XGamesSkater

Page 25: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

We are creeping up to the idea of polymorphism◦ Enables the substitution of one object for another as

long as the objects have the same interface◦ More details later

27

Page 26: Darrell Bethea June 7, 2011 · Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller

Every class in Java is derived from the class Object◦ Every class in Java is an Object

28

Animal

Reptile Mammal

HumanCrocodile Whale

Object

Person

Student Employee