Object oriented experiment 1

braintreesmileSoftware and s/w Development

Aug 15, 2012 (5 years and 6 days ago)

231 views

Object oriented experiment
2

Objective:



Package



Reference data type



Variable scope



Overloading





Java Packages



State: property, or attribute



Behavior: function/action, or method




Java hierarchically organizes classes into packages*

o

java.lang

o

java.text

o

ja
va.util

o






Classes need to be referred using its complete name (package + class
name): for example, java.util.Calendar

o

Packages can be “imported” to avoid writing package names every
time you use a class (except java.lang)


import java.util.*;





Using Pack
age



Organizing your classes into packages

o

A class can only be in one package

o

No duplicate class definition in the same package

o

Put the package statement at the beginning

o

Packages correspond to directories in local file system




Examples:

package cis3270;

p
ackage cis3270.assignment;

package cis3270.lecture.web;





Default Package



A class without any package defined is in a “default package”



The default package is NOT the root package!

o

Classes in the default package cannot be referenced outside the
default pac
kage




Reference Data Type



Reference data type stores memory address (reference) as its value




Objects are reference data type





Object Assignment



Objects are assigned by reference





Variable Scope



Member variable

o

Something like a global variable within t
he class



Local variable

o

Method parameter

o

Method level variable

o

Block level variable



A variable is effective at its declaration level and all sub
-
levels






Method Overloading



Multiple methods share the same method name, but each
of them is with a
different parameter set (different method signature)

o

Examples:

int
method
()

int
method
(int a)

String
method
(int a, String b)

void
method
(int a, int b)

void
method
(String a, int b)


Or:

System.out.print(…)





Constructor Overloading



Like me
thods, constructors can be overloaded



This offers greater flexibility and convenience of creating objects


Summary



Object orientation is more of a way of thinking/modeling, rather than just a
programming method



Organize your classes effectively using packa
ges



Design overloaded methods and constructors effectively



Tools :

NetBeans or JCreator
or JDK


Experiment

1
-

Open NetBeans tool

2
-

File


New


select "Java Classes" as shown in the figure
:




3
-

Then press "Next", then "
Complex
" in the class name as shown in t
he figure:




4
-

Then press "Finished", then the following code will appears


public class
Complex

{




public
Complex
() {


}

}


5
-

Then complete the code to be like the following:


public class Complex {




private float re;




private fl
oat im;




private static int instNo=0;




/** Creates a new instance of Complex */


public Complex() {


this(0,0);


}




public Complex(float r, float i) {


re= r;


im=i;


instNo++;


}




public

Complex(Complex c) {


this(c.re,c.im);




}


public float getReal() {


return re;


}




public float getImaginary() {


return im;


}




public void setReal(float r) {


re=r;


}




publi
c void setImaginary(float i) {


im=i;


}




public Complex add(Complex c) {


re += c.re;


im +=c.im;




return this;


}




public Complex sub(Complex c) {


re
-
= c.re;


im
-
=c.im;




return this;


}




public static int getNoInstance() {


return instNo;


}




public String toString() {


return "Real =" + re + " Imaginary=" + im;


}




public static Complex add(Complex c1, Complex c2)
{


Complex c3 = new Complex(c1.re+c2.re ,c1.im+ c2.im);


return c3;


}




public static Complex sub(Complex c1, Complex c2) {


Complex c3 = new Complex(c1.re
-

c2.re ,c1.im
-

c2.im);


return c3;


}




public st
atic void main(String srg[]){


Complex c1 = new Complex(1,2);


Complex c2 = new Complex();


Complex c3 = new Complex(c1);


Complex c4 = new Complex(3,4);


System.out.println(c1);


System.out.println(c2);


Sy
stem.out.println(c3);


System.out.println(c4);


c2.add(c1);


Complex c5 = c1;


c1.sub(c4);


System.out.println(c3);


System.out.println(c5);




Complex c6 = Complex.add(c3,c4);


System.out.prin
tln(c6);




System.out.println("No of Obkect Created = " +
Complex.getNoInstance());








}

}

6
-

Press "F6" or open menu Build


Execute

7
-

W
rite down the result :

1

2

3

4

5

6

7

8