By Cameron Kleiman, Lauren Mangibin, Nirali Devgan
- Learn how to create a class to represent an object
- Add private instance variables in your class
- Add constructors to your class
- Add accessor and mutator methods for your class
- Add a
toString()
method to your class
For our tutorial, we will be creating a Java object class based on a pencil. Our pencil will have a few properties such as sharpness and color. The maximum sharpness is 3, and the default color is yellow.
First, let's set up our Pencil object and give it variables of sharpness (an int
) and color (a String
). These are our object class's properties
public class Pencil {
private int pencilSharp;
private String pencilColor;
}
Next, we need to give our Pencil
class a default constructor, or assign it default values, should the user choose to specify none.
public class Pencil {
// ...
public Pencil(){
pencilSharp = 3;
pencilColor = "yellow";
}
}
Afterwards, create an initialization constructor in order to create your own unique pencil.
public class Pencil {
// ...
public Pencil(){
pencilSharp = 3;
pencilColor = "yellow";
}
public Pencil(int sharp, String color) {
pencilSharp = sharp;
pencilColor = color;
}
}
Don't worry about the fact that we have two Pencil
constructors. That's just the way Java works. You
need to specify a default constructor and one that has options.
Add a mutator method to change the color and the sharpness. A mutator method changes internal values within our object. While this may seem redundant, it is necessary so the user can have direct access to the object's properties.
We will only allow the user to do realistic operations on their Pencil
. For example, the user can sharpen their Pencil
to give it a sharpness value of 3, or they can paint their pencil
to change its colors. These are both mutator methods.
public class Pencil {
//...
public int sharpen() {
pencilSharp = 3;
return pencilSharp;
}
public String paint(String newColor) {
pencilColor = newColor;
return pencilColor;
}
}
What if the user wants to know the sharpness or color of their Pencil
? These are where accessor methods come in, as they allow the user to access certain properties of Pencil
.
Add int
and String
to the method names to tell Java what type of value will be returned.
public class Pencil {
//...
public int getSharpness() {
return pencilSharp;
}
public String getColor() {
return pencilColor;
}
}
Now, we want the user to be able to do something with their Pencil
. They should be able to write()
with their Pencil
.
However, they will not be able to write with their Pencil
if it is dull, so we will want to remind them to sharpen()
their Pencil
. This will return a String
, so we will need to tell Java that.
public class Pencil {
//...
public String write() {
if(pencilSharp > 0) {
pencilSharp--;
return "Success. You have written!";
}
else {
return "Your pencil is too dull to write. Sharpen it!";
}
}
}
However, we will want to have something to tell Java if someone tries to call System.out.println()
(or any other method requiring a general summary of our Pencil
) on our Pencil
object.
Next, we will display the color and sharpness of the pencil using a toString()
method.
public class Pencil {
//...
public String toString(){
return ("You have a sharpness of " + pencilSharp + ". It is " + pencilColor + ".");
}
}
Now, create another PencilRunner.java
program in the same folder to utilize some of our Pencil
's features.
Create a new Pencil
with a variable of your choosing using the new
keyword. Initialize it using the default constructor. Print out its default values using the toString()
method.
You don't need to put regular.toString()
in the System.out.println()
statement because Java knows to automatically execute the toString()
function when you try to access the Pencil
object
as a variable.
class PencilRunner {
public static void main(String args[]) {
Pencil regular = new Pencil();
System.out.println(regular);
}
}
Finally, let's change our Pencil
color and write three times with regular
. We need to wrap our write()
functions in a System.out.println()
to make sure they work properly.
class PencilRunner {
public static void main(String args[]) {
Pencil regular = new Pencil();
System.out.println(regular);
regular.paint("red");
System.out.println(regular.write());
System.out.println(regular.write());
System.out.println(regular.write());
}
}
Tip: Try and call write()
one more time on regular
. It should give you an error statement in the console. Try using sharpen()
to reset your sharpness.
Have fun paint()
-ing, write()
-ing, and sharpen()
-ing your Pencil
. Create multiple pencils and enjoy.
Make sure to wrap your method calls in System.out.println()
to confirm everthing outputs corrrectly.
Use the getSharpness()
and getColor()
methods to read out the properties of your pencil.