1 / 15

The 10 th and 11 th tutoring session Fall, 2012

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

roz
Download Presentation

The 10 th and 11 th tutoring session Fall, 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  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: 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

  5. 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

  6. 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

  7. 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

  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. 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

  10. 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

  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. 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

  13. 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

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

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

More Related