Creating Classes part 3 Barb Ericson Georgia Institute
Creating Classes part 3 Barb Ericson Georgia Institute of Technology Oct 2005 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
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 grade. Array – A 0 length grade. Array Georgia Institute of Technology
Create a Method Exercise • Create a method (get. Average) 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 to. String method to call the get. Average method and add the average to the returned string • Use the debugger to check that this is working correctly – Stop in the get. Average method Georgia Institute of Technology
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 student 1 = new Student(“Sue Clark”); – System. out. println(student 1. 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
Accessors and Modifiers • Accessors – Are public methods that return data • In such a way as to protect the data for this object • Syntax public field. Type get. Field. Name() • Example public String get. Name() { return this. name; } • Modifiers – Are public methods that modify data • In such a way as to protect the data for this object • Syntax public return. Type set. Field. Name(type name); • Example public void set. Name(String the. Name) {this. name = the. Name; } Georgia Institute of Technology
Naming Conventions • Accessors – also called Getters – Use get. Field. Name for non boolean fields – Use is. Field. Name for boolean fields • Modifiers – also called Setters and Mutators – Use set. Field. Name – Sometimes return a boolean value to indicate if the value was set successfully • Examples – get. Name and set. Name Georgia Institute of Technology
Creating Student Accessors • Add a method to get the name public String get. Name() { 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 get. Grade(int index) { return this. grade. Array[index]; } Georgia Institute of Technology
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 grade. Array 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
Name Modifier • Setting the name only if currently null public boolean set. Name(String the. Name) { boolean result = false; if (this. name == null) { this. name = the. Name; result = true; } return result; } Georgia Institute of Technology
Grade Modifier public boolean set. Grade(int index, double new. Grade) { boolean result = false; if (new. Grade >= 0 || this. grade. Array != null || index < this. grade. Array. length || index >= 0) { this. grade. Array[index] = new. Grade; result = true; } return result; } 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
Adding a Main Method • We have been typing stuff in the interactions pane in Dr. Java – To try out Java code and to try methods • Most development environments make you write a main method to start execution – Dr. Java 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 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 student 1 = new Student(); System. out. println(student 1); Student student 2 = new Student(“Sue Clark”); System. out. println(student 2); } Georgia Institute of Technology
Execute the Main Method • In Dr. Java 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 F 2) • It will do – java Student – In the interactions pane – Which executes the main in the Student class 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
- Slides: 16