+ All Categories
Home > Documents > Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded)...

Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded)...

Date post: 27-Apr-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
21
Implementing classes From last time: Scanner example: TestReadLine.java Example: Student class instance variables method definitions scope and lifetime of variables public vs. private constructors Implementing classes [Bono] 1
Transcript
Page 1: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Implementing classes

• From last time: Scanner example: TestReadLine.java

• Example: Student class• instance variables• method definitions• scope and lifetime of variables• public vs. private• constructors

Implementing classes [Bono] 1

Page 2: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Announcements• PA1 will be available later this week. Due Wed.

2/5

• Lab 3 has advanced prep: see lab for details (will be published sometime this weekend).

• Don't forget to respond to final exam survey ( < 1/2 have responded)

• Students who missed first class, who are not officially enrolled, or have no previous experience need to see me after class or in office hours today.

Implementing classes [Bono] 2

Page 3: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

From last time…How do you write code that combines word-by-word reading (e.g., with nextInt() or next()) and line-by-line reading (i.e., with nextLine())?

• Specific example: write code that when run does…

Enter your age: 32Enter your whole name: Joseph P. Blow

See TestReadLine.java

Implementing classes [Bono] 3

Page 4: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

POLL: Review of object references / String

String greeting = “Hello”;String greeting2 = greeting;

Using objects 2 [Bono] 4

Page 5: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

class example: Student

• Student class stores information about a student in a course.

• Keeps track of name and total score.• Allows client to add scores to total.

Implementing classes [Bono] 5

Page 6: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Student class interfaceStudent stud = new Student("Joe");

// has name "Joe" and total score of 0

Student blank = new Student();// has empty name and total score of 0

String name = stud.getName();

int total = stud.getTotalScore();

stud.addQuiz(score);// adds quiz score to the total //for this student

Complete code in Student.javaImplementing classes [Bono] 6

Page 7: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Instance variables

• a.k.a., fields, data members

public class Student {private String theName;private int totalScore;. . .

}

Implementing classes [Bono] 7

Page 8: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Using instance vars in a methodpublic class Student {

private String theName;private int totalScore; . . . public void addQuiz(int score) {

totalScore = totalScore + score;}. . .

}Student stud = new Student("Joe");Student stud2 = new Student("Mary");stud.addQuiz(7);stud2.addQuiz(9);

Implementing classes [Bono] 8

clientcode

classdef

Page 9: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Scope of variablespublic class Student {

private String theName;private int totalScore;. . . public void addQuiz(int score) {

totalScore = totalScore + score;}public int getTotalScore() {

return totalScore;} . . .

}

Student stud = new Student("Joe");stud.addQuiz(7);stud.addQuiz(10);int tot = stud.getTotalScore();int tot2 = totalScore; // error (undefined)int score2 = score; // error (undefined)

Implementing classes [Bono] 9

clientcode

classdef

Page 10: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Scope: local varspublic class Student {

. . . // Note: addBonus not part of actual class

public void addBonus(int percent) {int bonus = Math.round(

totalScore*(percent/100.0));totalScore = totalScore + bonus;

}. . .

}

Student stud = new Student("Joe");stud.addQuiz(7);stud.addQuiz(10);stud.addBonus(10);

Implementing classes [Bono] 10

clientcode

Page 11: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Poll: New version of addBonuspublic class Student {

. . . // Note: addBonus not part of actual class

public void addBonus(int percent) {int bonus = Math.round(

totalScore*(percent/100));totalScore = totalScore + bonus;

}. . .

}

Student stud = new Student("Joe");stud.addQuiz(7);stud.addQuiz(10);stud.addBonus(10);

Implementing classes [Bono] 11

clientcode

Page 12: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Visibility of private instance varspublic class Student {

private String theName;private int totalScore;. . .public void addQuiz(int score) {

totalScore = totalScore + score;} . . .

}

Student stud = new Student("Joe");stud.addQuiz(7);stud.addQuiz(10);int tot = stud.totalScore; // error (pvt)totalScore = 10; // error (undefined)

Implementing classes [Bono] 12

clientcode

classdef

Page 13: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Why do we make instance variables private?

Implementing classes [Bono] 13

Page 14: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Constructors

• Purpose: initialize all fields in the object

• Other methods can assume object is in a valid state.

• call: new Student(“Joe”);– internally initialized all the fields of this Student

object.

Implementing classes [Bono] 14

Page 15: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Constructor examplepublic class Student {

private String theName;private int totalScore;. . .

public Student(String name) {theName = name;totalScore = 0;

}. . .

}

Student stud = new Student("Joe");

Implementing classes [Bono] 15

clientcode

classdef

Page 16: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Default initialization

• Without any constructorsor before constructor executed:

instance vars get "default values"– 0 for numbers; null for obj. refs– recall: local variables are not automatically

initialized

• better to initialize them explicitly

Implementing classes [Bono] 16

Page 17: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

What is null?• Default init. of object ref. instance var. is null• null example: (shown with local var)

int len; // Example with local variablesString s; // locals are not init'dlen = s.length(); // crashess = null;len = s.length(); // crashesif (s != null) {len = s.length(); // safe call

}

• null ≠ empty strings = ""; // empty stringlen = s.length(); // 0

Implementing classes [Bono] 17

Page 18: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Default constructor

• The constructor with no parameters• Do we need to write any code in the body of

our Student default constructor?• No-code version:

// creates student with empty name and// total score of 0public Student() { }

Implementing classes [Bono] 18

Page 19: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

ShadowingSuppose instance variable identifier was the same as a

parameter (or local var) identifier:

public class Student { // this code doesn’t workprivate String name; private int totalScore; . . .public Student(String name) {

name = name;totalScore = 0;

}. . .

}

Implementing classes [Bono] 19

Page 20: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

One solution for ShadowingCorrect code where field identifier is the same as the

parameter identifier:

public class Student {private String name; private int totalScore;. . .public Student(String name) {

this.name = name;totalScore = 0;

}. . .

}

Implementing classes [Bono] 20

Page 21: Implementing classesbytes.usc.edu/cs455/curr/lectures/notes/implClasses.pdf( < 1/2 have responded) •Students who missed first class, who are not officially enrolled, or have no previous

Test programs• Student class is not a complete program• Could use in some larger app, or…• first create a class to test the Student class:– StudentTester, will contain main method.– each class in it's own file:

this one in StudentTester.java

Implementing classes [Bono] 21


Recommended