Presentation is loading. Please wait.

Presentation is loading. Please wait.

Barb Ericson Georgia Institute of Technology Oct 2005

Similar presentations


Presentation on theme: "Barb Ericson Georgia Institute of Technology Oct 2005"— Presentation transcript:

1 Barb Ericson Georgia Institute of Technology Oct 2005
Creating Classes part 3 Barb Ericson Georgia Institute of Technology Oct 2005 Georgia Institute of Technology

2 Georgia Institute of Technology
Learning Goals Computing concepts Adding a method To calculate the average of the grades Creating accessors and modifiers That protect the fields in the object Declaring a main method To execute the desired task Georgia Institute of Technology

3 Calculating the Grade Average
Now that a student has an array of grades one of the things we probably would like is to Show the average when you print information about the student To calculate an average Sum the grades Divide by the number of grades The length of the array We need to be careful of A null gradeArray A 0 length gradeArray Georgia Institute of Technology

4 Create a Method Exercise
Create a method (getAverage) that will calculate and return the average of the grades in the grade array It will return 0 if the grade array is null It will return 0 if the grade array length is 0 Otherwise it will return the sum of the grades / the number of grades Modify the toString method to call the getAverage method and add the average to the returned string Use the debugger to check that this is working correctly Stop in the getAverage method Georgia Institute of Technology

5 Accessing Fields from Other Classes
Fields are usually declared to be private So that code in other classes can’t directly access and change the data Try this in the interactions pane Student student1 = new Student(“Sue Clark”); System.out.println(student1.name); You will get an exception Short for exceptional event – error Outside classes can not use object.field to access the field value Unless it is declared with public visibility Georgia Institute of Technology

6 Accessors and Modifiers
Are public methods that return data In such a way as to protect the data for this object Syntax public fieldType getFieldName() Example public String getName() { return this.name;} Modifiers Are public methods that modify data public returnType setFieldName(type name); public void setName(String theName) {this.name = theName; } Georgia Institute of Technology

7 Georgia Institute of Technology
Naming Conventions Accessors – also called Getters Use getFieldName for non boolean fields Use isFieldName for boolean fields Modifiers – also called Setters and Mutators Use setFieldName Sometimes return a boolean value to indicate if the value was set successfully Examples getName and setName Georgia Institute of Technology

8 Creating Student Accessors
Add a method to get the name public String getName() { return this.name;} What about a method to get the array of grades? If someone gets the array they can directly change the grades in the array It is safer to return the grade at an index Then other classes can’t directly change the grade public double getGrade(int index) { return this.gradeArray[index];} Georgia Institute of Technology

9 Creating Student Modifiers
We need public methods That let other classes ask for the grade to change or the name to change Our class is responsible for making sure this only happens in such a way as to keep the data valid and not cause errors Setting a grade The grade must be >= 0 The gradeArray must not be null The index must be < the length of the array and >=0 Setting a name We can decide if this can be changed or not once it isn’t null Georgia Institute of Technology

10 Georgia Institute of Technology
Name Modifier Setting the name only if currently null public boolean setName(String theName) { boolean result = false; if (this.name == null) this.name = theName; result = true; } return result; Georgia Institute of Technology

11 Georgia Institute of Technology
Grade Modifier public boolean setGrade(int index, double newGrade) { boolean result = false; if (newGrade >= 0 || this.gradeArray != null || index < this.gradeArray.length || index >= 0) { this.gradeArray[index] = newGrade; result = true; } return result; Georgia Institute of Technology

12 Georgia Institute of Technology
Add a Field Exercise Add a picture field to student that will hold a Picture object of a student Add an accessor to get the value of this field Add a modifier to set the value of this field Add a method (show) that will show the picture If it isn’t null Georgia Institute of Technology

13 Georgia Institute of Technology
Adding a Main Method We have been typing stuff in the interactions pane in DrJava To try out Java code and to try methods Most development environments make you write a main method to start execution DrJava allows this too Each class can have a main method declared as follows: public static void main(String[] args) It is public so that it can be called by other classes It is static because no object of the class exists when it is executed It doesn’t return anything so the return type is void You can pass several arguments to the main method and these are put in an array of strings One of the reasons why we use DrJava is to let students use the interactions pane to try things out without having to use a main. Many books start with a main method but don’t ever explain it. Georgia Institute of Technology

14 Georgia Institute of Technology
Main Method Add a main method to Student Put the statements that you have been doing in the interactions pane in the main method public static void main(String[] args) { Student student1 = new Student(); System.out.println(student1); Student student2 = new Student(“Sue Clark”); System.out.println(student2); } Georgia Institute of Technology

15 Execute the Main Method
In DrJava you can run the main method in the class that is displayed in the definitions pane By clicking on Tools then Run Document’s Main Method (or press key F2) It will do java Student In the interactions pane Which executes the main in the Student class Georgia Institute of Technology

16 Georgia Institute of Technology
Summary To calculate an average Sum all the values and divide by the number of values Be careful not to divide by 0 Be careful not to access an array if it is null Fields are usually declared to be private To protect the data from misuse by other classes So you need to provide public accessor (getter) and modifier (setter) methods That still protect the data Use a main method to begin execution public static void main(String[] args) {} Georgia Institute of Technology


Download ppt "Barb Ericson Georgia Institute of Technology Oct 2005"

Similar presentations


Ads by Google