Escolar Documentos
Profissional Documentos
Cultura Documentos
Faculty of Engineering
Computer Engineering Dept.
Computer Programming Lab (ECOM 2114)
ABSTRACT
In this Lab you will learn how to describe objects and
classes and how to define classes and create objects
27 November 2016
A Java class uses variables to define data fields and methods to define actions.
Next Figure shows an example of defining the class for Rectangle objects:
Class Template
Class Name: Rectangle
Data Fields:
width is ______
Height is _____
Methods:
getArea
getPerimeter
setWidth
setHeight
Rectangle Object 1
Data Fields:
width is 5
Height is 2
Rectangle Object 2
Data Fields:
width is 2.5
Height is 1
Objects of
Rectangle
Class
Rectangle Object 3
Data Fields:
width is 20
Height is 7
Example 1: Define a Rectangle class as a described states and behaviors from the
previous figure, then create a three objects and call the methods from the
Rectangle class.
public class Rectangle {
double width = 1.0;
double height = 1.0;
Data Fields
Rectangle(){
}
Rectangle(double w, double h){
width = w;
height = h;
}
double getArea(){
return width*height;
}
double getPerimeter(){
return 2*(width+height);
}
Constructor
Methods
Constructors
A constructor is invoked to create an object using the new operator. Constructors
are a special kind of method. They have three peculiarities:
A constructor must have the same name as the class itself.
Constructors do not have a return typenot even void.
Constructors are invoked using the new operator when an object is created.
Constructors play the role of initializing objects.
A static variable is shared by all objects of the class. A static method cannot access
instance members of the class.
The data field width and height in the rectangle class is known as an instance
variable. An instance variable is tied to a specific instance of the class; it is not shared
among objects of the same class.
If you want all the instances of a class to share data, use static variables, also known
as class variables. Static variables store values for the variables in a common
memory location.
Lets to modify the rectangle class and adding the numberOfObjects which is a static
variable thats count the number of created objects from the rectangle class:
public class Rectangle {
double width = 1.0;
double height = 1.0;
static int numberOfObjects = 0; // we are added this variable
Rectangle(){
}
Rectangle(double w, double h){
width = w;
height = h;
numberOfObjects ++; // at every object this will be
incremented by one.
}
static int getNumberOfObjects(){
return numberOfObjects;
}//static method to access the variable
.
.
.
.
This mean thats an instance method can invoke and access another instance
(method & data field) and static (method & data filed).
On other hand, a static method can only invoke and access a static (method & data
field).
Visibility modifiers can be used to specify the visibility of a class and its
members:
- Default: The default modifier is accessible only within package
- Public: A public makes a modifier for classes, methods, and data fields
accessible from any other classes
- Private: by adding a private, this makes methods and data fields
accessible only from within its own class
- Protected: Later in java II .
See these examples:
Example 1:
Example 2:
Caution
The private modifier applies only to the members of a class. The public modifier
can apply to a class or members of a class. Using the modifiers public and private
on local variables would cause a compile error.
Making data fields private protects data and makes the class easy to maintain.
Return to the Rectangle class, we see that we can access the variables width
and height directly by writing r1.width and r1.height!
To prevent direct modifications of data fields, you should declare the data
fields private, using the private modifier. This is known as data field
encapsulation.
public class Rectangle {
private double width = 1.0;
private double height = 1.0;
private static int numberOfObjects = 0;
.
.
.
Now, How we can set the values of width and height from the rectangle
objects instances?
Okay, we need to create a public method thats make a variables accessible
for reading and writing (Setters and Getters).
See the full modified rectangle class:
this
The keyword this refers to the object itself. It can also be used inside a
constructor to invoke another constructor of the same class.
Design a class named Location for locating a maximal value and its location in
a two-dimensional array. The class contains public data fields row, column, and
maxValue that store the maximal value and its indices in a two-dimensional array
with row and column as int types and maxValue as a double type.
Write the following method that returns the location of the largest element in a
two-dimensional array:
public static Location locateLargest(double[][] a)
Sample Run:
Enter the number of rows and columns in the array: 3 4
Enter the array:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is 45 at (1, 2)