Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 classes -How to create objects -What is a instance method and what is a class method -How to create and use methods

2 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

3 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

4 Assuming in this tutoring room, we have: Haydon Elena Robert Kevin A student A tutor What are the data we have here? What are the actions on those data?

5 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

6 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 ClassTutor Class Data: Name: Haydon Java Level: 30000 Teaching Experience: 2000

7 Haydon Elena 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

8 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

9 Haydon Elena 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

10 Haydon Elena 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

11 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

12 Haydon Elena Robert Kevin Data: Name: Elena Java Level: 100 Actions: Learn ( hours ) Data: Name: Robert Java Level: 120 Actions: Learn ( hours ) Data: Name: Kevin Java Level: 102 Actions: 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

13 Elena Robert Kevin Data: Name: Elena Java Level: 100 Actions: Learn ( hours ) Data: Name: Robert Java Level: 120 Actions: Learn ( hours ) Data: Name: Kevin Java Level: 102 Actions: 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

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

15 Please let me know your questions. I will be here till 8:30pm


Download ppt "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."

Similar presentations


Ads by Google