Jav a The Art and Science of ERIC

  • Slides: 16
Download presentation
Jav a The Art and Science of ERIC S. ROBERTS CHAPTER 6 An Introduction

Jav a The Art and Science of ERIC S. ROBERTS CHAPTER 6 An Introduction to Computer Science Objects and Classes To beautify life is to give it an object. —José Martí, On Oscar Wilde, 1888 Chapter 6—Objects and Classes

Representing Student Information • Understanding the structure of a class is easiest in the

Representing Student Information • Understanding the structure of a class is easiest in the context of a specific example. The next four slides walk through the definition of a class called Student, which is used to keep track of the following information about a student: – The name of the student – The student’s six-digit identification number – The number of credits the student has earned (which may include a decimal fraction to account for half- and quarter-credit courses) – A flag indicating whether the student has paid all university fees • Each of these values is stored in an instance variable of the appropriate type.

Representing Student Information • In keeping with the modern object-oriented convention, these instance variables

Representing Student Information • In keeping with the modern object-oriented convention, these instance variables are declared as private. • private fields can only be accessed within the class they are defined in. • All access to these values is therefore mediated by the get/set methods exported by the Student class.

The Student Class These declarations define the instance variables that maintain the internal state

The Student Class These declarations define the instance variables that maintain the internal state of the object. All instance variables used in the text are private. public class Student { /* Private private instance variables */ String student. Name; int student. ID; double credits. Earned; boolean paid. Up; /* /* The student's name The student's ID number The number of credits earned Whether student is paid up */ */ /** * Creates a new Student object with the specified name and ID. * @param name The student's name as a String * @param id The student's ID number as an int */ public Student(String name, int id) { This comment describes the constructor. student. Name = name; student. ID = id; The constructor sets the instance variables. } page 1 of 4 skip code

The Student Class /** * Gets the name of this student. * @return The

The Student Class /** * Gets the name of this student. * @return The name of this student These methods retrieve the value of /** */ an instance variable of and are called * The Student class keeps track of the following pieces data public String get. Name() { getters. Because the student name * about a student: the student's name, ID number, the are number ofare and ID number fixed, there return student. Name; * credits the student has earned toward graduation, andsetters. whether no corresponding } * the student is paid up with respect to university bills. * All of this information is entirely private to the class. /** * Clients can obtain this information only by using the various * methods Gets thedefined ID number of this student. * by the class. * @return The ID number of this student */ */ This method changes the value of public int get. ID() { an instance variable and is called a public class Student { return student. ID; setter. /**} * Creates a new Student object with the specified name and ID. /** * @param name The student's name as a String * @param Sets the of credits earned. as an int * idnumber The student's ID number * @param credits The new number of credits earned */ */public Student(String name, int id) { public void set. Credits(double credits) { student. Name = name; credits. Earned = credits; student. ID = id; } } page 2 of 4 skip code

The Student Class /** * Gets the name number ofof this credits student. earned.

The Student Class /** * Gets the name number ofof this credits student. earned. * @return The name number ofof this credits student this student has earned */ public String double get. Name() get. Credits() { { return student. Name; credits. Earned; } /** * Gets Sets the whether ID number the student of this isstudent. paid up. * @return @param flag The ID Thenumber value of true this or student false indicating paid-up status */ public int voidget. ID() set. Paid. Up(boolean { flag) { return paid. Up student. ID; = flag; } Names for getter methods usually begin with the prefix get. The only /** exception is for getter methods that * Sets Returns thewhether number of thecredits studentearned. is paid up. return a boolean, in which case * @param @returncredits Whether. The thenew student number isof paid credits up earned the name typically begins with is. */ public void boolean set. Credits(double is. Paid. Up() { credits. Earned return paid. Up; = credits; } page 3 of 4 skip code

The Student Class /** Gets theanumber credits earned. * Creates stringof identifying this student.

The Student Class /** Gets theanumber credits earned. * Creates stringof identifying this student. The to. String method * @return The string number used of credits this student has earned tells Java how to display this student a value of this class. All */ of your classes should double to. String() get. Credits() public String { { override to. String. return student. Name credits. Earned; + " (#" + student. ID + ")"; } /** Classes often export named constants. /* constants * Public Sets whether the */ student is paid up. * @param flag of Thecredits value true or false indicating */ paid-up status /** The number required for graduation */public static final double CREDITS_TO_GRADUATE = 32. 0; public void set. Paid. Up(boolean flag) { paid. Up = flag; } } /** * Returns whether the student is paid up. * @return Whether the student is paid up */ public boolean is. Paid. Up() { return paid. Up; } page 4 of 4 skip code

Using the Student Class • Once you have defined the Student class, you can

Using the Student Class • Once you have defined the Student class, you can then use its constructor to create instances of that class. For example, you could use the following code to create two Student objects: Student chosen. One = new Student("Harry Potter", 123456); Student top. Student = new Student("Hermione Granger", 314159); • You can then use the standard receiver syntax to call methods on these objects. For example, you could set Hermione’s number-of-credits field to 97 by writing top. Student. set. Credits(97); or get Harry’s full name by calling chosen. One. get. Name();

Exercise: Design an Employee Class • Create a definition for a class called Employee,

Exercise: Design an Employee Class • Create a definition for a class called Employee, which keeps track of the following information: – – The name of the employee A number indicating the order in which this employee was hired A flag indicating whether the employee is still active The salary (a number that may contain a decimal fraction) • The name and employee number should be assigned as part of the constructor call, and it should not be possible to change them subsequently. By default, new employees should be marked as active. The salary field need not be initialized. • The class should export appropriately named getters for all four fields and setters for the last two.

The Employee Class /** * The Employee class keeps track of the following pieces

The Employee Class /** * The Employee class keeps track of the following pieces of * data about an employee: the name, employee number, whether * the employee is active, and the annual salary. */ public class Employee { /* Private instance variables */ private String employee. Name; /* private int employee. Number; /* private boolean active; /* private double annual. Salary; /* The employee's name */ The employee number */ Whether the employee is active */ The annual salary */ /** * Creates a new Employee object with the specified name and * employee number. * @param name The employee's name as a String * @param id The employee number as an int */ public Employee(String name, int id) { employee. Name = name; employee. Number = id; active = true; page 1 of} 4 skip code

The Employee Class /** * Gets the name of this employee. * Employee class

The Employee Class /** * Gets the name of this employee. * Employee class of the following pieces of * The @return The name ofkeeps this track employee * */data about an employee: the name, employee number, whether * the employee active, and the annual salary. public Stringis get. Name() { */ return employee. Name; } public class Employee { /** * Gets the employee number of this employee. /** * @return The employee number of this employee * */Creates a new Employee object with the specified name and * employee number. public int get. Employee. Number() { * @param name The employee's name as a String return employee. Number; * @param id The employee number as an int } */ /**public Employee(String name, int id) { * Sets whether the=employee is active. employee. Name name; * @param flag The value true or false indicating active status employee. Number = id; */ active = true; public void set. Active(boolean flag) { } active = flag; } page 2 of 4 skip code

The Employee Class /** * Gets Returns thewhether name ofthe this employee. is active.

The Employee Class /** * Gets Returns thewhether name ofthe this employee. is active. * @return The Whether namethe of this employee is active */ public String booleanget. Name() is. Active() { { return employee. Name; active; } /** * Gets Sets the employee's number salary. of this employee. * @return @param salary The employee The newnumber salaryof this employee */ public int voidget. Employee. Number() set. Salary(double salary) { { return annual. Salary employee. Number; = salary; } /** * Sets Gets whether the annual thesalary employee foris this active. employee. * @param @returnflag The annual value salary truefor or false this employee indicating works active status */ public void double set. Active(boolean get. Salary() { flag) { active return = annual. Salary; flag; } page 3 of 4 skip code

The Employee Class /** Returns awhether employee this is active. ** Creates string the

The Employee Class /** Returns awhether employee this is active. ** Creates string the identifying employee. @return The Whether theused employee is active ** @return string to display this employee */ */ public String booleanto. String() is. Active(){ { public return employee. Name active; return + " (#" + employee. Number + ")"; } } }/** * Sets the employee's salary. * @param salary The new salary */ public void set. Salary(double salary) { annual. Salary = salary; } /** * Gets the annual salary for this employee. * @return The annual salary for this employee works */ public double get. Salary() { return annual. Salary; } page 4 of 4 skip code

Exercise: Using the Employee Class • Now that you have defined Employee, write declarations

Exercise: Using the Employee Class • Now that you have defined Employee, write declarations for three variables that contain the names of the following three employees: Ebenezer Scrooge (employee #1), Jacob Marley (employee #2), and Bob Cratchit (employee #3). Employee founder = new Employee("Ebenezer Scrooge", 1); Employee partner = new Employee("Jacob Marley", 2); Employee clerk = new Employee("Bob Cratchit", 3); • Using these variables, write a Java statement that marks the Employee instance for Jacob Marley as inactive. partner. set. Active(false); • Write a Java statement that doubles Bob Cratchit’s salary. clerk. set. Salary(2 * clerk. get. Salary());

Quiz: Design a Product Class • Create a definition for a class called Product,

Quiz: Design a Product Class • Create a definition for a class called Product, which keeps track of the following information: – – Name of the product Weight of the product Price of the product Currency of the product price • The name and weight should be assigned as part of the constructor call, and it should not be possible to change them subsequently. • By default, price of each product is 10 TL. • The class should have appropriately getters for all four fields and setters for the last two.