Lab 8 Object-Oriented Programming

oatmealbrothersSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

125 views

Eng. Alaa Hassan Awad

Page

1



Islamic University Of Gaza

Engineering Faculty

Computer Engineering Department

Java Programming I Lab (ECOM 2114)




Lab 8













25,Dec
.
,2010

Eng. Alaa Hassan Awad

Page

2


Java is an Object Oriented Language. As a language that has the Object Oriented
feature Java
supports the following fundamental concepts:



Polymorphism



Inheritance



Encapsulation



Abstraction



Classes



Objects



Instance



Method



Message Parsing

In this
lab

we will look into the concepts Classes and Objects.



Object
-

Objects have states and behaviors. Example: A
student

has
id
,
name,
birthDate

as well as behaviors
-
WritingNotes,
SubmitExam
. An
object is an instance of a class.



Class
-

A class can be defined as a template that describe the
behaviors/states that object of its type support.




Objects in Java:




If we consider the real
-
world we can find many objects around us, Cars, Dogs,
Humans etc. All these objects have a state and behavi
or.



If you compare the software object with a real world object, they have very
similar characteristics.



Software objects also have a state and behavior. A software object's state is
stored in fields and behavior is shown via methods.




Classes in Java:


A class is a
template

from

which

individual objects are created.

A sample of a class is given below
(
Exercise 7.3
):

class

Account

{

private int id=0;

private double balance =0;

private double annualInterestRate =0;

private Date dateCreated=new Date();



public

Account

(){

}


public int getId(){


return id;

}


public void setId(int d0){


id=d0;


}

public double getMonthlyInterestRate(){

Eng. Alaa Hassan Awad

Page

3



return annualInterestRate
/
12;




}




public void withDraw(double w){


balance=balance
-
w;


}

.

.

.

.

}

A class can
contain any of the following variable types.



Local variables .
variables defined inside methods, constructors or blocks are
called local variables. The variable will be declared and initialized within the
method and the variable will be destroyed when the
method has completed.



Instance variables .
Instance variables are variables within a class but outside
any method. These variables are instantiated when the class is loaded. Instance
variables can be accessed from inside any method, constructor or blocks o
f
that particular class.



Class variables .
Class variables are variables declared within a class, outside
any method, with the
static

keyword.



Constructors:




Constructors must have the same name as the class itself.



Constructors do not have a return
type

not even void.



Every class has a constructor. If we do not explicitly write a constructor for a
class the java compiler builds a
default constructor

for that class.



Each time a new object is created at least one constructor will be invoked. A
class ca
n have more than one constructor.

Example of a constructor is given below:

class
Account
{

//Default constructor

public

Account

(){

}


public
Account

(
double

b0
){

balance = b0;


// This constructor has one parameter,
b0
.


}

}



Creating an Object:

O
bject
s are

created from a class. In java the
new

key word is used to create new
objects.

There are three steps when creating an object from a class:

Eng. Alaa Hassan Awad

Page

4




Declaration .
A variable declaration with a variable name with an object type.




Instantiation .
The 'new'
key word is used to create the object.



Initialization .
The 'new' keyword is followed by a call o a constructor. This call
initializes the new object.

Example of creating an object is given below:

class
Account
{


public
Account

(
double b0
){


// This constructor has one parameter,
b0
.


balance=b0;


}


public static void main(String []args){


// Following statement would create an object
myAccount


Account

myAccount

= new
Account

( 600.0

); }}



Accessing Instance
Variables and Methods:

Instance variables and methods are accessed via created objects. To access an instance
variable the fully qualified path should be as follows:

/* First create an object */

ObjectReference = new Constructor();


/* Now call a variable
as follows */

ObjectReference.variableName;


/* Now you can call a class method as follows */

ObjectReference.MethodName();

Example:

This example explains how to access instance variables and methods of a class:

//Exercise 7.3

import java.util.Date;

class

Account{



static int
num
OfAccount

=0;


private int id=0;

private double balance =0;

private double annualInterestRate =0;

private Date dateCreated=new Date();


public Account(){


num
OfAccount
++;

}

public void setId(int d0){


id=d0;



}




public void
setBalance(double b0){


balance=b0;



}




public void setAnnualInterestRate(double a0){

Eng. Alaa Hassan Awad

Page

5


annualInterestRate=a0;


}



public int getId(){


return id;


}




public double getBalance(){


return balance;


}


public double getAnnualInterestRate(){


return

annualInterestRate;


}


public Date getDateCreated(){


return dateCreated;

}


public double getMonthlyInterestRate(){


return annualInterestRate/12;




}



public void withDraw(double w){


balance=balance
-
w;


}



public void deposit (double d){


balance=balance+d;

}

public static void main(String [] args){



Account account=new Account();

account.setId(1122);

account.setBalance(20000);

account.setAnnualInterestRate(4.5/100.0);

account.withDraw(2500);

account.deposit(3000);

System.out.println("bala
nce
\
t
\
tmonthlyInterest
\
t
\
tDateWhenCreated
\
n"+acc
ount.getBalance()+"
\
t
\
t"+account.getMonthlyInterestRate()

+"
\
t
\
t
\
t"+account.getDateCreated());

System.out.println("
\
nnum is a static variable:
\
nits value at the first
object
\
n"+account.

num
OfAccount
);


Accoun
t account1=new Account();

System.out.println("num value's at the second object
\
n"+account1.

num
OfAccount
);



}




}

If we compile and run the above program then it would produce following result:



Eng. Alaa Hassan Awad

Page

6


Note:

Static variables and methods can be used from
instance or static methods in the class.
However, instance variables and methods can only be used from instance methods,
not from static methods, since static variables and methods belong to the class as a
whole and not to particular objects.
(
check this

yo
urself
)