Você está na página 1de 10

Islamic University of Gaza

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

Eng. Mustafa J. Dalloul


Computer Programming Lab

JAVA PROGRAMMING LAB

27 November 2016

Object-oriented programming enables you to develop large-scale software and


GUIs effectively.

Object-oriented programming (OOP) involves programming using objects. An


object represents an entity in the real world that can be distinctly identified. For
example, a student, a desk, a circle, a button, and even a loan can all be viewed as
objects. An object has a unique identity, state, and behavior.

The state of an object (also known as its properties or attributes) is


represented by data fields with their current values.
The behavior of an object (also known as its actions) is defined by methods.

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

Creating a three objects from the Rectangle class:


public class TestRectangle {
public static void main(String[] args) {
Rectangle r1 = new Rectangle(5.5,27);
System.out.println("The area of Rectangle1 is: " +r1.getArea());
System.out.println("Perimeter of Rectangle1 is: " +r1.getPerimeter()+"\n");
Rectangle r2 = new Rectangle(10, 3);
System.out.println("The area of Rectangle2 is: " +r2.getArea());
System.out.println("Perimeter of Rectangle2 is: " +r2.getPerimeter()+"\n");
Rectangle r3 = new Rectangle(10, 3);
System.out.println("The area of Rectangle3 is: " +r3.getArea());
System.out.println("Perimeter of Rectangle3 is: " +r3.getPerimeter());
}
}

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
.
.
.
.

Testing the new rectangle class:


Rectangle r1 = new Rectangle(5.5, 27);
System.out.println("The number of Instances: "
+ r1.getNumberOfObjects());
Rectangle r2 = new Rectangle(10, 3);
System.out.println("The number of Instances: "
+ r2.getNumberOfObjects());
The result after running the program:

Tip: Use Class Name


Use ClassName.methodName(arguments) to invoke a static method and
ClassName.staticVariable to access a static variable. This improves readability,
because this makes the static method and data easy to spot.
Recall that PI is a constant defined in Math, and Math.PI references the constant. So
r1.getNumberOfObjects() and r2.getNumberOfObjects() are better replaced by
Rectangle.getNumberOfObjects() or Rectangle.numberOfObjects.
Data Accessing
See this graphical diagram:

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).

See this code and read the comments:


public class A {
int i = 5;
static int k = 2;
public static void main(String[] args) {
int j = i; // Wrong because i is an instance variable
m1(); // Wrong because m1() is an instance method
}
public void m1() {
// Correct since instance and static variables and methods
// can be used in an instance method
i = i + k + m2(i, k);
}

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:

C1: A nonpublic class has package-access.

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:

public class Rectangle {


private double width = 1.0;
private double height = 1.0;
private static int numberOfObjects = 0;
Rectangle(){
}
Rectangle(double w, double h){
width = w;
height = h;
numberOfObjects ++;
}
static int getNumberOfObjects(){
return numberOfObjects;
}
double getArea(){
return width*height;
}
double getPerimeter(){
return 2*(width+height);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
Creating an instances:
Rectangle r1 = new Rectangle();
r1.setWidth(50);
r1.setHeight(70);
System.out.println(r1.getArea());

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.

See this figure:


Using this to Reference Hidden Data Fields

Using this to Invoke a Constructor


public class Rectangle {
private double width = 1.0;
private double height = 1.0;
private static int numberOfObjects = 0;
Rectangle(){
this(1.0,1.0); this to invoke another constructor
}
Rectangle(double width, double height){
this.width = width;
this to invoke private variables
this.height = height; this to invoke private variables
numberOfObjects ++;
}
}

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)

Você também pode gostar