150 likes | 255 Views
The 10 th and 11 th tutoring session Fall, 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 . Haidong Xue. 5:30pm—8:30pm 10/9/2012 and 10/10/2012 . CSc2310 Tutoring
E N D
The 10th and 11th tutoring sessionFall, 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 HaidongXue 5:30pm—8:30pm 10/9/2012 and 10/10/2012
CSc2310 Tutoring • Time: 5:30pm-8:30pm • Tutor: HaidongXue • 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
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
Assuming in this tutoring room, we have: A student Elena A tutor A student Haydon Robert • What are the data we have here? • What are the actions on those data? A student Kevin
Data and actions on data: • Data: • Name: Elena • Java Level: 100 • Data: • Name: Haydon • Java Level: 30000 • Teaching Experience: 2000 Elena • Data: • Name: Robert • Java Level: 120 Haydon Robert We can use 4 objects to organize all the data here. To avoid creating the variables for students 3 times, we can use classes • Data: • Name: Kevin • Java Level: 102 Kevin
Student Class Tutor Class • Data: • Name: Elena • Java Level: 100 • Data: • Name: Haydon • Java Level: 30000 • Teaching Experience: 2000 Elena • Data: • Name: Robert • Java Level: 120 Haydon Robert • 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 • Data: • Name: Kevin • Java Level: 102 Kevin
Data: • Name: Elena • Java Level: 100 • Data: • Name: Haydon • Java Level: 30000 • Teaching Experience: 2000 Elena Haydon • Data: • Name: Robert • Java Level: 120 class Student{ String name; intjavaLevel; } Robert class Tutor{ String name; intjavaLevel; } • Data: • Name: Kevin • Java Level: 102 Kevin How to initialize those data for each object? Using Constructors
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
Data: • Name: Elena • Java Level: 100 • Data: • Name: Haydon • Java Level: 30000 • Teaching Experience: 2000 Elena Haydon • Data: • Name: Robert • Java Level: 120 class Student{ String name; intjavaLevel; public Student( String n, int l ){ name=n; javaLevel=l; } } Robert constructor • Data: • Name: Kevin • Java Level: 102 class Tutor{ String name; intjavaLevel; intteachingExp; public Tutor( String n, intl, intexp){ name=n; javaLevel=l; teachingExp=exp; } } Kevin constructor
Data: • Name: Elena • Java Level: 100 • Data: • Name: Haydon • Java Level: 30000 • Teaching Experience: 2000 Elena Haydon class Student{ String name; intjavaLevel; public Student( String n, int l ){ name=n; javaLevel=l; } } • Data: • Name: Robert • Java Level: 120 Robert class Tutor{ String name; intjavaLevel; intteachingExp; public Tutor( String n, intl, intexp){ name=n; javaLevel=l; teachingExp=exp; } } • Data: • Name: Kevin • Java Level: 102 Kevin 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
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
Data: • Name: Elena • Java Level: 100 • Actions: • Learn ( hours ) • Data: • Name: Haydon • Java Level: 30000 • Teaching Experience: 2000 Elena Haydon • Data: • Name: Robert • Java Level: 120 • Actions: • Learn ( hours ) Robert class Student{ String name; intjavaLevel; public Student( String n, int l ){ name=n; javaLevel=l; } public void learn( int hours){ javaLevel = javaLevel + hours; } } • Data: • Name: Kevin • Java Level: 102 • Actions: • Learn ( hours ) Kevin The “learn” action
class Student{ String name; intjavaLevel; public Student( String n, int l ){ name=n; javaLevel=l; } public void learn( int hours){ javaLevel = javaLevel + hours; } } • Data: • Name: Elena • Java Level: 100 • Actions: • Learn ( hours ) Elena • Data: • Name: Robert • Java Level: 120 • Actions: • Learn ( hours ) Robert • Data: • Name: Kevin • Java Level: 102 • Actions: • Learn ( hours ) What happened when a method is called? After Robert having learnt Java for 1 hour, whose Java level will increase? Kevin Remember that when you learn Java, only your Java skill increases When calling a method of an object, it does not affect other objects’ private data
Simulate today’s tutoring session class Student{ String name; intjavaLevel; 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; intjavaLevel; intteachingExp; public Tutor( String n, intl, intexp){ 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);
Please let me know your questions. I will be here till 8:30pm