Download presentation
Presentation is loading. Please wait.
Published byJanel Page Modified over 9 years ago
1
Classes
2
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 class
3
Instance Variables What does a student object need to remember? – name (can be stored as a String.) – totalScore (can be stored as type double, a decimal number.) – numberOfScores (can be stored as type int, an integer value.)
4
Constructor(s) What needs to happen when we construct a new Student object? – All variables should be initialized in a constructor. – We should let the constructor know the name of the new student when it is being constructed. – The other variables are just 0. public Student (String newName) Constructors are unique methods! They do not have a return type!
5
Methods There were four methods in the requirements: – public String getName() This method returns a String and receives no input parameters. We call this an accessor method because it accesses data. – public void addScore(double score) When we add a score we have to send the score to the method, but we don’t need to return anything. (Other methods will provide info to the client.)
6
Methods (continued..) – public double getTotalScore() This method is another accessor which only returns a value to the client. – public double getAverage() The code in this method will include some calculations before replying.
7
Putting it all together public class Student { //Declare instance variables //Put constructor(s) here. //Put methods here. } No main method in this class. That is for the client (or runner) program to do. This is just the blue print for an object that we will use.
8
Declaring variables private String name; private double totalScore; private int numberOfScores; We’re now going to make all of our instance variables private rather than public. If a client wants to know about these variables, they can use an accessor method.
9
Writing a constructor public Student (String newName) { name = newName; totalScore = 0; numberOfScores = 0; } We initialize all of our instance variables in our constructor. We could provide more than one constructor if we chose to.
10
Writing the methods public String getName() { return name; } This is quite straight forward. The client has asked for the Student’s name and so we return it to them.
11
More methods… public double getTotalScore() public double getAverage() public void addScore(double score) { //add to the totalScore //increase the numberOfScores } You will need to write the code for these methods.
12
The (almost) finished product. public class Student { //Declare instance variables private String name; private double totalScore; private int numberOfScores; //Put constructor(s) here. public Student (String newName) { name = newName; totalScore = 0; numberOfScores = 0; } //Put methods here. public String getName() { return name; } public void addScore(double score) { /*write the code*/ } public double getTotalScore() { /*write the code*/ } public double getAverage() { /*write the code*/ } }
13
The Tester class public class StudentTester { public static void main(String[] args) { Student jill = new Student(“Jill”); jill.addScore(85); //Add more lines to test this class! } Here’s our main class. We declare a student object and use the methods provided in the Student class.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.