the 10 th and 11 th tutoring session fall, 2012 haidong xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012...

15
The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 - What is a class, what is an object and why we need classes - How to create objects - What is a instance method and what is a class method - How to create and use methods

Upload: rafe-eaton

Post on 26-Dec-2015

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

The 10th and 11th tutoring sessionFall, 2012

Haidong Xue

5:30pm—8:30pm10/9/2012 and 10/10/2012

- What is a class, what is an object and why we need classes

- How to create objects- What is a instance method and what is a class

method- How to create and use methods

Page 2: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

• CSc2310 Tutoring• Time: 5:30pm-8:30pm• Tutor: Haidong Xue• Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/index.html

There are 2 sections:1. Review - What is a class, what is an object and why we need classes- How to create objects- What is a instance method and what is a class method- How to create and use methods

2. Q&A- Answer your questions about java programming

Page 3: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Class and Object

• What does a program do?– Manipulate data

• There are many ways to organize data in a program, and a very convenient and popular way is Object Oriented, i.e.: using objects to organize data

• Let’s set the tutoring room as a example

Page 4: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Assuming in this tutoring room, we have:

Haydon

Elena

Robert

Kevin

A student

A student

A student

A tutor

• What are the data we have here?• What are the actions on those data?

Page 5: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Data and actions on data:

Haydon

Elena

Robert

Kevin

Data: • Name: Elena• Java Level: 100

Data: • Name: Robert• Java Level: 120

Data: • Name: Kevin• Java Level: 102

Data: • Name: Haydon• Java Level: 30000• Teaching Experience: 2000

We can use 4 objects to organize all the data here. To avoid creating the variables for students 3 times, we can use classes

Page 6: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Haydon

Elena

Robert

Kevin

Data: • Name: Elena• Java Level: 100

Data: • Name: Robert• Java Level: 120

Data: • Name: Kevin• Java Level: 102

• By name, “class” means a type of objects• Thereby, we can divide the objects here

into 2 classes: Student and Tutor• Then, we only need to define 1 student

class and 1 tutor class

Student Class Tutor Class

Data: • Name: Haydon• Java Level: 30000• Teaching Experience: 2000

Page 7: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

HaydonElena

Robert

Kevin

Data: • Name: Elena• Java Level: 100

Data: • Name: Robert• Java Level: 120

Data: • Name: Kevin• Java Level: 102

class Student{ String name; int javaLevel;}

class Tutor{ String name; int javaLevel;}

Data: • Name: Haydon• Java Level: 30000• Teaching Experience: 2000

How to initialize those data for each object?Using Constructors

Page 8: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Constructors

• A method of a class, with the same name as the class name

• With no return type• They are automatically called when creating a

object

Page 9: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

HaydonElena

Robert

Kevin

Data: • Name: Elena• Java Level: 100

Data: • Name: Robert• Java Level: 120

Data: • Name: Kevin• Java Level: 102

class Student{ String name; int javaLevel; public Student( String n, int l ){ name=n; javaLevel=l; }}class Tutor{ String name; int javaLevel; int teachingExp; public Tutor( String n, int l, int exp ){ name=n; javaLevel=l; teachingExp=exp; }}

Data: • Name: Haydon• Java Level: 30000• Teaching Experience: 2000

constructor

constructor

Page 10: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

HaydonElena

Robert

Kevin

Data: • Name: Elena• Java Level: 100

Data: • Name: Robert• Java Level: 120

Data: • Name: Kevin• Java Level: 102

class Student{ String name; int javaLevel; public Student( String n, int l ){ name=n; javaLevel=l; }} class Tutor{

String name; int javaLevel; int teachingExp; public Tutor( String n, int l, int exp ){ name=n; javaLevel=l; teachingExp=exp; }}

Data: • Name: Haydon• Java Level: 30000• Teaching Experience: 2000

To create the objects in this tutoring room: Student elena= new Student(“Elena”, 100);Student robert= new Student(“Robert”, 120);Student kevin= new Student(“Kevin”, 102);Tutor haydon= new Tutor(“Haydon”, 30000, 2000);

create objects

Page 11: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Methods

• Methods are used to represent the actions on data

• E.g.: A student here may learn java, and their java level may then increase

• Those actions are defined by Methods

Page 12: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

HaydonElena

Robert

Kevin

Data: • Name: Elena• Java Level: 100Actions:• Learn ( hours )

Data: • Name: Robert• Java Level: 120Actions:• Learn ( hours )

Data: • Name: Kevin• Java Level: 102Actions:• Learn ( hours )

Data: • Name: Haydon• Java Level: 30000• Teaching Experience: 2000

class Student{ String name; int javaLevel;

public Student( String n, int l ){ name=n; javaLevel=l; }

public void learn( int hours){ javaLevel = javaLevel + hours; }}

The “learn” action

Page 13: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Elena

Robert

Kevin

Data: • Name: Elena• Java Level: 100Actions:• Learn ( hours )

Data: • Name: Robert• Java Level: 120Actions:• Learn ( hours )

Data: • Name: Kevin• Java Level: 102Actions:• Learn ( hours )

class Student{ String name; int javaLevel;

public Student( String n, int l ){ name=n; javaLevel=l; }

public void learn( int hours){ javaLevel = javaLevel + hours; }}

What happened when a method is called?After Robert having learnt Java for 1 hour, whose Java level will increase?

When calling a method of an object, it does not affect other objects’ private data

Remember that when you learn Java, only your Java skill increases

Page 14: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Simulate today’s tutoring session class Student{ String name; int javaLevel; public Student( String n, int l ){ name=n; javaLevel=l; } public void learn( int hours){ javaLevel = javaLevel + hours; } public void showStatus(){ System.out.println(name + " with Java level " + javaLevel); }}

class Tutor{ String name; int javaLevel; int teachingExp; public Tutor( String n, int l, int exp ){ name=n; javaLevel=l; teachingExp=exp; } public void teach(Student s, int hours){ s.learn(hours); }}

Your code:Tutor haydon = new Tutor( “Haydon”, 30000, 1000);

Page 15: The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need

Please let me know your questions.

I will be here till 8:30pm