LAB04-Department of Computer Science - Copy
LAB04-Department of Computer Science - Copy
Objectives:
So far, all the classes we have been written are applications containing the main method.
Another type of class is that which is written not necessarily to be executed as an application, but to be used in
creating objects. Such type of classes are designed such that they describe the state and methods for a set of
objects. This is achieved by using instant variables, instant methods and constructors.
Instant Variables: These are variables declared without using thestatic keyword. Such variables are only
accessed through an object of the class and each object has its own cop y of the variables – hence the name
instant. Instant variables are used to store the state of an object and are usually declared as
private so that they
cannot be accessed directly by other objects.
Instant methods: These are methods declared without using thestatic keyword. They are normally used to
describe the behavior of objects of a particular class.Instant methods are normally declared aspublic so that
they could be accessed by other objects.
Constructor: These are used to initialize the fields (variables) of an object at the time the object is being created.
Constructors must have the same name as the class, they have no return type and they are implicitly public.
Example 1: The following shows two classes. A class, Box, which specify the fields, a constructor and methods of
a box object, and a class, BoxDemo, which creates two Box objects and called their methods.
public class Box {
Example 2: The following example shows the difference between static and non-static variables.Notice that
both the class Circle and the class CircleDemo are stored in the same file.In this case, only one of the classes
should be declared as public – the one containing the main method.The file should also have the same name as
this public class.
// Demonstrating the use of static variables.
class Circle {
private static int numberOfCircles = 0; //a static variable
It is a good programming principle to always declare instance variables as private. However, it is O.K. to allow
indirect access to these variables by providing public methods. Some of these method only access but do not
change the variables, hence they are calledaccesor methods. Those that do not only access, but also mak e
changes to these variables are calledmutator methods.
// Accessor methods
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
// Mutator methods
public void setLength(double newLength) {
length = newLength;
}
public void setRectangleWidth(float newWidth) {
width = newWidth;
}
}
4. Assignment.
1. Open the folder lab04 , Study, compile, and then execute each of the three demo
applications:
· BoxDemo
· CircleDemo
· RectangleDemo
· a method deposit.
· a method withdraw
· a method getBalance.
3. Write two java classes, Student and StudentDemo in separate files. The class
Student should contain the following:
· three set method, one for each of quiz1, quiz2 and quiz3
· a method printDetails to print the details of a student object in the following format: