Objects and Classes

carenextΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

59 εμφανίσεις



Chapter 8

About the Midterm Exam..


Exam on March 12 Monday (Tentatively)


Review on March 7 Wednesday


Cover from Chapter 6


Grades will be out before spring break.

Object
-
Oriented
P
rogramming
(OOP
)


What we did was: (
Procedural Programming
)



a logical procedure that takes input data, processes it,
and produces output
data.



Object
-
oriented
programming
(OOP) is a
programming language model organized
around


"
objects" rather than "actions"


data
rather than logic
.


What is an object?


An entity in the real world:


a student, a desk, a circle, a button, a window…



How do we describe an object? (a student, for example)


State of an object (properties or attributes)


name, ID, year, address…


Behavior of an object (actions)


drop a class, add a class…

Some more examples of objects


Thinking of
a circle
as an object:


Properties:
radius

(or
diameter
)


Actions:
calculate
the area
of the
circle,
calculate
the circumference
of the circle …



Classes


A class is a template that
defines
objects of the same
type.



Class Name:
Circle


Properties
:

radius

is _______


Action
s
:

get
Area


Circle Object 1


Properties
:

radius is

10


Circle Object 2


Properties
:

radius is

25



Circle Object 3


Properties
:

radius is

125



A class template



Three object
s of
the Circle class

Why OOP?


The definition of a class is reusable.


It is possible to define subclasses to inherit some or all
of the main class characteristics.


Person


Student, Teacher


The concept of data classes allows a programmer to
create
any new data type that is not already defined in
the language itself.


OOP Languages


Simula

(1967)
was the first object
-
oriented
programming language.


Java
, Python, C++, Visual Basic .NET and Ruby are the
most popular OOP languages today.


How to describe an object?


A

circle is an object.


Properties: radius (or diameter)


data field (variable)


double

radius = 5;


Actions: get the area of the circle


method


double

getArea
(){




double

area = 3.14 * radius * radius;




return area;


}

How to describe an object?


An object is a bundle of
variables

and related
methods
.



Class Name:
Circle


Data Fields:

radius
is _______


Methods:

get
Area


Circle Object 1


Data Fields:

radius is
10


Circle Object 2


Data Fields:

radius is
25



Circle Object 3


Data Fields:

radius is
125



A class template



Three object
s of
the Circle class


Let’s try to define this class in Java!

How to create an object?


an object = an instance


You can create many objects (instances) of a class in
the main class.



Declaring an object


Syntax:


ClassName

objectName
;


Example:


Circle circle
1
;


Circle
circle
2
;


Circle
circle
3
;


int
[] scores;

Creating an object


circle
1

=
new

Circle();


scores =
new
int
[20];



You can also combine the declaration and creation of an
object.



int
[] scores =
new
int
[20];


Circle circle1 =
new

Circle();


Circle circle2 =
new

Circle();


Circle circle3 =
new

Circle();


Scanner input =
new

Scanner(System.in);


Accessing an Object’s Data and
Methods


dot operator (.)


objectName.dataField


objectName.method
()



For example:


circle2.radius


circle3.radius


circle1.getArea();


circle2.getArea();


What does “static” mean?


“Static” means that variable or method marked as such is
available at class level.


You don’t need to create an instance (object) to access it.


“Static” means there is only one!


E.g., public static void
printMessage
(){




System.out.println
(“This is a circle object.”);


}



radius

and
getArea
(
) are at instance level.


We will talk about static more..


Let’s try to create a few Circle objects!



Two ways:


In another file (same package!)


In the same file


You can also give a default value to the data fields.


You can always overwrite them in the main class.

Exercise 1: The Rectangle Class


Follow the example of the
Circle

class, design a class
named
Rectangle

to represent a rectangle.


The class contains:


Two double data fields named
width

and
heigh
t, The
default values are
1

for both.


A method named
getArea
().


A method named
getPerimeter
().

Constructors


Circle circle
1

=
new

Circle();


What is
Circle
()??


Constructor is a special type of method, that is
invoked to create a new object.



By default: (if no constructor is provided)


Circle()
{


}


Constructors


Circle() {


}



A constructor must have the same name as the class.


Constructors do not have a return type (not even void).


Constructors are invoked using the
new

operator
when an object is created.


Constructors play the role of initializing objects.

Constructors


Circle(){



radius
= 1;


}



Constructors


You can overload constructors.


Multiple constructors can have the same name but different
signatures.



Circle(){



radius
= 1;


}



Circle(
double r){



radius
= r;


}

Add to Exercise 1..


A no
-
arg

constructor that creates a rectangle with
width of 1 and height of 1.


A constructor that creates a rectangle with the
specified width and height.



In your TestRectangle.java, create two Rectangle
objects by invoking the two different constructors.

Primitive
Data Types
vs.
Object Types


1

Primitive type

int i = 1 i

Object type

Circle c

c

reference

Created using new Circle()




c: Circle


radius = 1

When Copying Variables..



i



Primitive type assignment i = j



Before:



1



j


2


i



After:



2



j


2



c1



Object type assignment c1 = c2



Before:






c2






c1



After:



c2



c1: Circle

radius = 5


C
2
: Circle

radius = 9








c1: Circle

radius = 5


C
2
: Circle

radius = 9


Static variables


Instance variable


rectangle2.width = 5;


rectangle2.height = 10;


class variable (static variable)


static
int

numberOfRectangles
;


Static methods


Instance method


rectangle2.getArea()


static method


static
int

getNumberOfRectangles
()

Static constants


final static double
PI = 3.14;

Visibility Modifiers


By default, the class, variable, or method can be

accessed by any class in the same package.



public


The class, data, or method is visible to any class in
any package.


private



The data or methods can be accessed only
from
within its own class.



public class C1 {


public in
t x;


int y;


private int z;




public void m1() {


}


void m2() {


}


private void m3() {


}

}

public class C2 {


void aMethod() {


C1 o = new C1();


o.x;


o.y;


o.z;




o.m1();


o.m2();

o.m3();


}

}

package p1;

package p2;

public class C3 {


void aMethod() {


C1 o = new C1();


o.x;


o.y;


o.z;




o.m1();


o.m2();


o.m3();


}

}


class C1 {


...

}

public class C2 {


C1

}

package p1;

package p2;

public class C3 {


C1;


C2;

}

Data fields should be private.


This is not good:


double

radius;
//In Circle.java


circle2.radius = 5;
//In TestCircle.java


Why?


Data is not protected.


class becomes difficult to maintain.


Data fields should be private
.


private double
radius;

Then how can we give a value to
radius of circle1?


circle1.radius = 5 does NOT work any more!


The
get

and
set

methods are used to read and modify
private properties
.


getRadius
()
: to read


setRadius
(double r)
: to write



Add to exercise 1..


In your Rectangle.java, make
width

and
height
private.


Add
get

and set methods for the two private variables.



In TestRectangle.java, create another two rectangles.
Set one rectangle to have
width

of 10,
height

of 5.



Set another rectangle to have width of 25, height of 10;



Print their width, height, area and perimeter.

Exercise 2: The Student class


Design a class named
Student

that contains:


A private
String
data field named
name
;


A private
int

data field named
id
;


A private
String

data field named
major
;


A constructor that creates a student with the specified
id, name and major.


The get and set methods for name, id

and major.


Array of Objects


int
[] scores =
new
int
[10];


Circle[] circles = new Circle[10];



Passing Objects to Methods


public static void
printRadius
(Circle c)

Use classes from the Java library


The Date Class



import
java.util.Date


toString
() returns the date and time as a string

Use classes from the Java library


GUI Components



The
JFrame
,
JButton
,
JTextfield
,
JPanel

class


import
java.swing
.*;