Barb Ericson Georgia Institute of Technology Oct 2005

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
More Methods and Arrays Material from Chapters 5 to 7 that we didn’t cover in 1226.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
The need for Programming Languages
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 3
Lecture 15: More Inheritance
Creating and Modifying Text part 2
Barb Ericson Georgia Institute of Technology Dec 2009
CS Week 13 Jim Williams, PhD.
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
CS1316: Representing Structure and Behavior
Manipulating Pictures, Arrays, and Loops part 5
Introduction to Java part 2
CS1316: Representing Structure and Behavior
Arrays versus ArrayList
Private.
Introduction to Processing Digital Sounds part 3
Barb Ericson Georgia Institute of Technology Oct 2005
Recap Week 2 and 3.
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Introduction to Java, and DrJava
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Barb Ericson Georgia Institute of Technology June 2005
Lecture 15: Inheritance II
Lecture 5- Classes, Objects and Methods
Barb Ericson Georgia Institute of Technology May 2006
Introduction to Java part 2
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
Creating and Modifying Text part 3
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
More on Creating Classes part 3
LCC 6310 Computation as an Expressive Medium
Methods/Functions.
CS 240 – Advanced Programming Concepts
Presentation transcript:

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

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 gradeArray A 0 length gradeArray Georgia Institute of Technology

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

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

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

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

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

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

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

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

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

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

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

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

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