Writing the Class in Java
Turn a class design into Java with private variables, getters and setters, and methods, then create an object from it with new.
Apprentice · Java · OOP · Updated
Now that we have defined a class in UML we need to convert it to Java code.
Defining the Class
Each class is declared in its own file. The file name must match the class name exactly.
Refrigerator.java
public class Refrigerator
{
// class variables and methods defined here
}
Defining the Variables
All variables defined inside a class should be declared as private. This means that the variable can only be accessed inside the class.
Variables are exposed using getters and setters. Getters and setters are used to read or change the values of the variables in the class.
public class Refrigerator
{
private int temperature;
private int capacity;
private int usedSpace;
public int getTemperature()
{
return temperature;
}
public void setTemperature(int temperature)
{
this.temperature = temperature;
}
// ... add getters and setters for capacity and usedSpace here
}
Creating an Object
With a class defined, you are ready to create an object from the class template. A class is a new data type, so you can declare variables of that type, just like int or String.
Refrigerator fridge = new Refrigerator();
fridge.setTemperature(32);
int currentTemperature = fridge.getTemperature();
System.out.println("The temperature is: " + currentTemperature);
Defining Methods
Methods are used to add custom functionality to a class. These functions are closely related to the variables defined in the class. For example, if you want to put something into the fridge (or remove it) your function would also modify the amount of used space (capacity) of the refrigerator.
public class Refrigerator
{
private int temperature;
private int capacity;
private int usedSpace;
// return true if there is room for the item - false if not
public boolean addItem(int itemCapacity)
{
if (usedSpace + itemCapacity <= capacity)
{
usedSpace += itemCapacity;
return true;
}
return false;
}
}
Do it: write a kitchen cupboard class
The kitchen needs a cupboard as well as a refrigerator. A cupboard holds plates, and like the refrigerator it guards its own count: plates go in only through its methods, and it refuses plates it has no room for.
The class keeps two numbers, as private variables:
capacity: how many plates it can hold, changed withsetCapacity.plates: how many plates it holds now:0when it's new.
addPlates gets one value:
count: how many plates to put in, anint(8).
For example:
Cupboard cupboard = new Cupboard();
cupboard.setCapacity(12);
cupboard.addPlates(8);
cupboard.addPlates(5);
| Call | Returns | Why | Plates after it |
|---|---|---|---|
setCapacity(12) |
nothing (void) |
the cupboard can now hold 12 | 0 |
addPlates(8) |
true |
0 + 8 = 8, and 8 is 12 or fewer | 8 |
addPlates(5) |
false |
8 + 5 = 13, more than 12, so nothing changes | 8 |
So after these calls, cupboard.getCapacity() returns 12 and cupboard.getPlates() returns 8.
Your job: finish the class Cupboard.
- Add two private
intvariables, namedcapacityandplates. getCapacity()returns the capacity, andsetCapacity(int capacity)changes it.getPlates()returns how many plates it holds now.addPlates(int count): if the plates it holds now pluscountmake the capacity or fewer, add them and returntrue. Otherwise returnfalseand change nothing.- Each cupboard keeps its own values: plates added to one don't appear in another.
In the practice box, everything goes in one file, Main.java, with nothing around it: since Java 25, a small program can be written this way. So the class sits in that file as class Cupboard, instead of in a file of its own as public class Cupboard.
Practiced live on the lesson page: write the Java right there and check it.