structuring models

29
Structuring Models CS1316: Representing Structure and Behavior

Upload: jabari

Post on 07-Jan-2016

54 views

Category:

Documents


0 download

DESCRIPTION

Structuring Models. CS1316: Representing Structure and Behavior. Story. Exploring classes, superclasses/subclasses, constructors, Strings, and println Let’s make a Person People have names Let’s make a Student a kind-of Person Students have ID numbers. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Structuring Models

Structuring Models

CS1316: Representing Structure and Behavior

Page 2: Structuring Models

Story

Exploring classes, superclasses/subclasses, constructors, Strings, and println

Let’s make a Person• People have names

Let’s make a Student a kind-of Person• Students have ID numbers.

Setting them new objects automatically How inheritance works, and how overriding

works

Page 3: Structuring Models

People have names

public class Person {

public String name;

}

> Person fred = new Person();> fred.namenull> fred.name = "Fred";> fred.name"Fred"

Page 4: Structuring Models

Should we be able to rename Fred?

> fred.name = "Mabel";

> fred.name

"Mabel"This is the what public instance variables (sometimes called fields) allow us to do?

Page 5: Structuring Models

If we wanted to control names, can do it in accessors> Person fred = new Person();> fred.setName("Fred");> fred.getName()"Fred"

public class Person { private String name; public void setName(String somename) {this.name = somename;} public String getName() {return this.name;}}

Could check in setName(): “Are you allowed to change my name?”

Could check in getName(): “Are you allowed to ask my name?”

Page 6: Structuring Models

Barney may not be bright,but is he really null?

> Person barney = new Person(); > barney.getName() null

How could we set up a person’s name right from the start?

Page 7: Structuring Models

Making new people have reasonable namespublic class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public void setName(String somename) {this.name = somename;} public String getName() {return this.name;}}

> Person barney = new Person()> barney.getName()"Not-yet-named"

Page 8: Structuring Models

The Constructor

A method with the same name as the class.

It’s called when a new instance of the class is created.

public Person(){ this.setName("Not-yet-named"); }

Page 9: Structuring Models

Constructors can take inputs (arguments)public class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public Person(String name){ this.setName(name); } public void setName(String

somename) {this.name = somename;} public String getName() {return this.name;}}

> Person barney = new Person("Barney")> barney.getName()"Barney"> Person wilma = new Person()> wilma.getName()"Not-yet-named"> wilma.setName("Wilma")> wilma.getName()"Wilma"

Both constructors exist. Java uses the right one depending on the arguments provided.

Page 10: Structuring Models

Must match inputs to some constructor

> Person agent99 = new Person(99)

java.lang.NoSuchMethodException: Person constructor

There are constructors for no inputs and String inputs, but not for double or int inputs.

Page 11: Structuring Models

Some discourse rules for Java In a Western, the hero carries a gun, never a bazooka

and never flies on a unicorn.• There are “discourse rules” in different genres.

There are such rules in Java.• Class names start with capital letters.

• They are never plurals. (Want more than one of them? Make an array or a list!)

• Instance variables and methods always start with lowercase letters.

• Methods should describe verbs—what objects know how to do.

• Accessors are typically set- and get- the name of the field.

Page 12: Structuring Models

But what is Barney?

> Person barney = new Person("Barney")

> barney

Person@63a721

Looks like “Person” then cuss word…

Page 13: Structuring Models

Making the printable representation of People better

public class Person {

private String name;

public String toString(){

return "Person named "+this.name;

}

> Person barney = new Person("Barney")> barneyPerson named Barney

toString() is called whenever an object needs to be converted to a string—such as when it is printed in the Interactions Pane

Page 14: Structuring Models

Students are people, too.

Let’s make Students• They are Persons

• But they also have ID#s

• (Not exactly the best model in the world. Real students also have homes, friends, etc. But you only model what you need, and right now, we just need another data element.)

Students are kind-of Persons• Students extend Persons

Page 15: Structuring Models

Making Students

public class Student extends Person {

private int idnum;

public int getID(){ return idnum;}

public void setID(int id) {idnum = id;}

}

Page 16: Structuring Models

Making Betsy

> Student betsy=new Student("Betsy")java.lang.NoSuchMethodException: Student constructor> Student betsy = new Student()> betsy.getName()"Not-yet-named"> betsy.setName("Betsy")> betsy.getName()"Betsy"> betsy.setID(999)> betsy.getID()999

Can’t make Betsy by name, because that only exists for Persons so far.Betsy does inherit the existing constructor for no input.

Betsy does inherit setName and getName.

And Betsy now has getID and setID

Page 17: Structuring Models

Who is Betsy?

> betsy

Person named Betsy

Students inherit the toString() of the parent class.

Page 18: Structuring Models

Setting up Student constructorspublic class Student extends Person { private int idnum; // Constructors public Student(){ super(); //Call the parent's constructor idnum = -1; } public Student(String name){ super(name); idnum = -1; } public int getID(){ return idnum;} public void setID(int id) {idnum = id;}}

If a Student has no ID, the default now is -1.

If we want to call the parent’s constructor, we must do it in the FIRST line.

Page 19: Structuring Models

Your turn: Actually try these in class

Make a new constructor for Student that takes a name and an ID, so that we can say:Student mark = new Student(“Mark”,29)

Make a toString() for Students so that they respond with name and ID.

Page 20: Structuring Models

Can we have a main() here?

Any class can have a public static void main(String[] args) method.

What would you use it for? How about for testing?

• Use it like we use the Interactions Pane

Page 21: Structuring Models

Getting values printed out

How do we get things printed to the Interactions Pane from inside a main()?

System.out.println(“Some String”)

Page 22: Structuring Models

A main() for Persons public static void main(String [] args){ Person fred = new Person("Fred"); Person barney = new

Person("Barney"); System.out.println("Fred is "+fred); System.out.println("Barney is

"+barney); }

Welcome to DrJava.> java PersonFred is Person named FredBarney is Person named Barney

As long as you start with a string, you can “add” (with “+”) any object and it will be made into a string.

Page 23: Structuring Models

The whole Person.java

public class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public Person(String name){ this.setName(name); } public String toString(){ return "Person named "+this.name; } public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} public static void main(String [] args){ Person fred = new Person("Fred"); Person barney = new Person("Barney"); System.out.println("Fred is "+fred); System.out.println("Barney is "+barney); }}

Page 24: Structuring Models

Exploring greetings

A subclass inherits all structure (data) and behavior (methods) from superclass.

Unless it overrides it.

Page 25: Structuring Models

A new method for Person

public void greet(){ System.out.println("Hi! I am "+this.name); }

> Person bruce = new Person("Bruce")> bruce.greet()Hi! I am Bruce> Person george = new Person("George W.")> george.greet()Hi! I am George W.> Student krista = new Student("Krista")> krista.greet()Hi! I am Krista

Should students greet() in the same way?

Page 26: Structuring Models

Trying to give Student a greet()

Page 27: Structuring Models

This way works

public void greet(){

System.out.println("Hi, I'm "+this.getName()+

", but I got to run to class...");

}

Page 28: Structuring Models

Changes Student, but not Person

Welcome to DrJava. > Person george = new Person("George W.") > george.greet() Hi! I am George W. > Student krista = new Student("Krista") > krista.greet() Hi, I'm Krista, but I got to run to class...

Page 29: Structuring Models

Think about what happens here

When we tell krista to greet()• “I am a Student. Do I know how to

greet()? If I don’t, I’ll ask my parent, Person, to handle it.”

• “I do know how to greet()! I’ll execute that method!”

• “Uh-oh. I don’t know how to getName(). Oh, Parent?!? Can you handle getName()?”

public void greet(){ System.out.println("Hi, I'm "+this.getName()+ ", but I got to run to class..."); }