Java Data Types

An object-oriented programming language like Java provides the means to create models of the world. For example, you may want to make a model of a traffic simulation and one of the objects in this will be a car. We will need some definition of what a car is and we will need examples or instances of actual cars in the simulation. In object-oriented programming the car is defined by a class and objects are created as instances of the class. In this example the cars will have properties such as type and colour and they will also have methods such as move and stop.

Example 1: Shapes

In BlueJ classes appear as boxes in the main window. Open the Shapes file. Right-click on the Circle class and choose 'new Circle'. A dialogue box opens where you can give a name to the object:

The circle1 object appears on the object bench:

Note the notation of object:Class. You can add more circle objects or objects of the other types. Right click the circle1 object to reveal its methods:

Choose 'void makeVisible()'. A new window opens with a circle drawn in it:

To hide the circle choose the appropriate method. Note that some methods have empty brackets after them while others have things like this: 'void moveHorizontal (int distance)'. The 'void' word means that the method does not return a value. The words in the brackets define a parameter, which is a value that is passed into the method. In the case of moveHorizontal and moveVertical the parameter is of type integer and represents the distance on screen that the circle object will move. The makeVisible method and others like it have no parameter as they do not require a value for a property such as distance.

When you invoke a method with a parameter a dialogue box opens asking you for its value:

The type of value required is an integer such as 5 or 10. If you enter a value of another type the dialogue box will give an error message. Note that the word for 'integer' in Java is 'int' and, unlike Pascal, the declaration of type comes before the identifier.

The changeColor method takes a single parameter of type string (instructions are provided):

Here is the program with circle1 in red and a new object, square1, added:

To view the state of an object right click it in the object bench and choose 'inspect', which reveals the state of the object's properties:

Objects

Objects of the same class have the same fields, though the values of the fields in each separate object may differ. Objects of different classes will almost certainly have different fields. The number, types and names of fields are defined in a class and not in an object. Objects are instantiations of a class - this is a posh way of saying that they are copies of the class, just as a particular car, such as a Ford Focus, is a copy or instantiation of the Ford Focus class. Or, to follow Plato, there is an abstract notion of a table and a physical realisation of a table in every dining room. When an object of class circle is created it is automatically given the field that are defined in the class, just as a actual table has the things we expect from the abstract model such as legs and a top to put things on. The precise values of the circle and the table are stored in the object or an actual table itself.

Methods are also defined in classes and each object includes all the methods of the class. Methods are automatically linked to objects so if there are many objects of the same type active a method call applies only to the intended object and not to any other.

Object Interaction

Objects can create other objects and they can call each other's methods. A program may create one object when it runs and that single object may create many new objects when it is running. You can see this for yourselves in the Shapes program which uses the square, triangle and circle methods to create a simple scene.

Source Code

To view source code double click the class icon. When a class has been edited its icon has a striped appearance and the next step should be to compile the new code. To compile a class right click it and choose Compile.

Another Example

Open the LabClasses example. Compile if necessary and then add a new student. Fill in the details:

You can now call a method such as 'getname' (right click on the Student1 instance).

If you made a mistake you can edit the name with change name. Note that this is a void function as it does not return a new value.

Objects as Parameters: The LabClass Class

Create a LabClasses object and enter the number of students

Now you can use the class's methods to set the instructor, the room and the time.

Now you can enrol students in the Labclass. Call the method by right-clicking the Labclass instance and then click on the student instances to enroll them. In the diagram below student1 and student2 have already been enrolled so student3would be added.

(After clicking on student3 object.)

Now call the Number of Students method:

Display the Labclass object:

The code:

public class LabClass
{
private String instructor;
private String room;
private String timeAndDay;
private List<Student> students;
private int capacity;

/**
* Create a LabClass with a maximum number of enrolments. All other details
* are set to default values.
*/
public LabClass(int maxNumberOfStudents)  //constructor name same as class; int parameter for max number of students
{
instructor = "unknown";
room = "unknown";
timeAndDay = "unknown";
students = new ArrayList<Student>();
capacity = maxNumberOfStudents;
}

/**
* Add a student to this LabClass.
*/
public void enrollStudent(Student newStudent)  //parameter is itself a class (student)
{
if(students.size() == capacity) {
System.out.println("The class is full, you cannot enrol.");
}
else {
students.add(newStudent);
}
}

/**
* Return the number of students currently enrolled in this LabClass.
*/
public int numberOfStudents()
{
return students.size();
}

/**
* Set the room number for this LabClass.
*/
public void setRoom(String roomNumber)
{
room = roomNumber;
}

/**
* Set the time for this LabClass. The parameter should define the day
* and the time of day, such as "Friday, 10am".
*/
public void setTime(String timeAndDayString)
{
timeAndDay = timeAndDayString;
}

/**
* Set the name of the instructor for this LabClass.
*/
public void setInstructor(String instructorName)
{
instructor = instructorName;
}

/**
* Print out a class list with other LabClass details to the standard
* terminal.
*/
public void printList()
{
System.out.println("Lab class " + timeAndDay);
System.out.println("Instructor: " + instructor + " room: " + room);
System.out.println("Class list:");
for(Student student : students) {
student.print();
}
System.out.println("Number of students: " + numberOfStudents());
}
}

This gives you a first impression of Java code.

Home Page