- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Accessors and Mutators
You saw how to edit a method that returned the ID of a student. In Object-Oriented programming, you usually want to make an Object's variables private
which will prevent external code from directly accessing them. Instead, other code should use public methods of your Object for getting and modifying variable values. These methods are commonly called Accessors and Mutators.
Accessors
Accessor methods simply return the value of one of the Object's variables. For example, getStudentID()
returned the Student's ID, so it's an accessor. To get the ID of student1
in other code, you would type student1.getStudentID()
.
Question: Why couldn't I just make the student's ID (SID
) public, which would let me type student1.SID
to get the ID?
Answer: That would work but it would violate the principle of Encapsulation, and would be much less flexible. For example, let's say you want to always return additional information with the ID. With an accessor, you were able to modify the method easily so any code that accessed the ID will receive the extra information. (If you directly accessed the variables, you would need to edit your code everywhere the variable is accessed.)
You could also run additional checks in the accessor, such as checking that there is a Student ID in the first place.
Mutators
Mutators are methods that change the value of an Object's variables. They can do this by directly changing the value of a variable. For example, in a Car class, you could have a method to set the speed:
class Car{
private int speed;
public void setSpeed(int carSpeed){
speed = carSpeed;
}
}
As with accessors, you should use a method to change a variable's values since this provides greater flexibility and control. For example, you could add code to make sure the speed variable is set realistically:
public void setSpeed(int carSpeed){
if(carSpeed >= 0 && carSpeed < 140){
speed = carSpeed;
}
}
Mutator methods can also modify a variable's value instead of re-assigning a value. In a Car, you will probably use an accelerate method instead of setting the speed directly. It could change the speed by a set amount each time, or by a provided amount:
accelerate by set amount:
public void accelerateBy5(){
speed = speed + 5;
}
accelerate by provided number:
public void accelerate(int amount){
speed = speed + amount;
}
Instance Variables
Accessors and Mutators deal with a specific type of variable: an instance variable, which is a variable available to the whole instance, or Object. Variables that are declared inside methods are only available within the method itself. To create a variable available everywhere, you need to declare it outside of any method, usually at the beginning of the block of Class code.
For example, in the People Project, the Student class defines an instance variable SID
, which is accessible by all other methods in Student.
Task
- Create a new private instance variable
GPA
for Student. It should be of typedouble
. - Create a method
setGPA
which should let you directly set the GPA of Student.
Testing out your code:
- Compile your code and create a new Student Object on the Object bench.
- Invoke your
setGPA
method and provide a GPA for the Student. - Inspect your Object to check that the GPA was set.
Challenge
Fill in the two accessors (getSpeed
and getColor
) and one mutator (accelerate
) in the code below. The accessors should simply return the speed and color of the Car. The mutator accelerate
should modify the speed by the provided amount. However, if the change will cause the speed to be less than 0 or greater than 140 it should do nothing.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
calo
Jan 26, 12:43 PMyour missing input values here i guess
Learneroo
Jan 26, 12:45 PM@calo, in this challenge, there is no input.
calo
Jan 26, 12:50 PMArent we missing here an input for the options ?
calo
Jan 26, 2:33 PMyup my bad ..that was before I had the answer right
Lance
Feb 10, 8:23 PMthe input values are at the top:
Car c = new Car("green");
System.out.println( c.getColor() );
c.accelerate(5);
System.out.println( c.getSpeed());
jack
Mar 14, 9:34 AManybody know the answer. please post
jack
Mar 14, 9:44 AMplease post answers no comprendo
Learneroo
Mar 14, 9:47 AMJack, I will add a hint to help out. You only need to ask for help once! Also, please update your email info. Thanks.
jack
Mar 15, 11:02 AMoh thank you I had something capitalized that shouldn't have been
thales
Jul 8, 1:11 PMI have no idea what to do here? Please help me!
thales
Jul 8, 1:22 PMi figured it out, unbelievable i would never of guessed!
public void accelerate(int amount)
{
if (speed+amount<=0 || speed+amount>=140){
thales
Jul 8, 1:37 PMit is very hard to see the 'big picture'