chap1_class (editedv3)

Download Report

Transcript chap1_class (editedv3)

INTRODUCTION TO CLASS &
OBJECT – PART 1
INSPIRING CREATIVE AND INNOVATIVE MINDS
Questions are KEYS to Knowledge
•
•
•
•
What is a class?
What are the constituents of a class?
How does class diagram look like?
How to define a class?
2
INSPIRING CREATIVE AND INNOVATIVE MINDS
What is a class?
• Class is a structure for creating objects.
• The constituents of a class are;
1.
2.
3.
4.
Name
Data attribute
Constructor
Method
3
INSPIRING CREATIVE AND INNOVATIVE MINDS
1st Class Component:Name
• As a standard convention, name is a
noun.
– For example: Circle, Student,
Employee.
• Start with a Capital letter.
4
2nd
Class Component: Attribute
• Variable is normally declared as scope private.
• Constant is of scope public.
• Attribute can be of the following type;
– Primitive data type like;
int,double,float,char,short,long,
byte,boolean, OR/AND.
– Class for example;
String, Circle, Employee.
5
3rd
Class Component: Constructor
• Is needed to create object using new
operator.
• A class may have more than one
constructor(known as constructor
overloading).
– Non-argument also known as default.
– Constructor with parameter.
6
Example of Default Constructor
public Circle(){
radius = 0.0;
}
If we forget to declare a constructor in a
class, Java will provide a default
constructor.
7
Example of Constructor with Parameter
public Circle(double r){
radius = r;
}
parameter
8
4th
Class Component: Methods
• Denotes behaviours of a class.
• Methods serve 3 purposes;
1. public interface to variables of scope
private .
i. Accessor
ii. Mutator
2. Manipulation of attributes to produce
output, may use a formula.
3. Display attributes and output.
9
Accessor Method
• The method does not have a parameter
but return a value stored inside a
variable, example;
public double getRadius(){
return radius;
}
10
Mutator Method
• The method receives parameter to be
assigned to a variable, but does not
return any value, example;
public void setRadius(double r){
radius = r;
}
parameter
No value is return
11
Summary
• Class is a structure for creating objects. It has
4 items;
1.
2.
3.
Name
Attribute
Constructor;
i.
ii.
4.
Default
With parameter
Methods
-
Public Interface (Mutator method is used to set value to attribute,
Accessor method is used to access value of the attribute.
12
INTRODUCTION TO CLASS &
OBJECT – PART 2
INSPIRING CREATIVE AND INNOVATIVE MINDS
What is a Class?
• Class is a structure for creating objects.
• The constituents of a class are;
1.
2.
3.
4.
Name
Data attribute
Constructor
Method; 4 types
i.
ii.
iii.
iv.
Mutator
Acessor
manipulate attributes/output producing
display
14
INSPIRING CREATIVE AND INNOVATIVE MINDS
Method to Manipulate Attributes
• This method will utilize attributes to
produce a result.
• Normally mathematical formula is used
• Example;
public double calcArea(){
return(PI*radius*radius);
}
15
Method to DISPLAY
• Attributes and output must be displayed
to the user.
• Example;
public void display(){
System.out.println("Radius of a
circle:"+radius);
System.out.println("Area of the
circle:"+calcArea());
}
16
Class Diagram
Class Name
Attribute
Constructor
Methods
Circle
-radius:double
+Circle()
+Circle(double r)
+getRadius():double
+setRadius(double r):void
+calcArea():double
+display():void
17
Class Definition Sequence
1.
2.
3.
4.
Name of Class
Attribute
Constructor
Methods
i. Accessor and Mutator
ii. Output producing Method
iii. Display Method
18
Methods
public class Circle {
Name of class
private double radius;
Attributes
public double final PI = 3.13;
public Circle(){
radius = 0.0;
}
Constructor
public Circle(double r){
radius = r;}
public void setRadius(double r){
radius = r;
}
public double getRadius(){
return radius;
}
public double calcArea(){
return(PI*radius*radius);
}
public void display(){
System.out.println("Radius of a circle:"+radius);
System.out.println("Area of the circle:"+calcArea());
}
}
19
Save as;
• When we have completed the definition of the
class, we may save it as “Circle.java”.
• We need to compile it to remove syntax error.
• When we run it, no output is generated, WHY
DO YOU THINK?
• Yes, we have not create object from the class
yet.
• HOW to create objects? – we’ll learn it in the
next session.
20
Summary
• Sequence to define a class is based on it’s
structure.
• Class Diagram consists of 3 sections;
Name
-Attribute
+Constructor
+Method
-: scope private
+: scope public
21
INTRODUCTION TO CLASS &
OBJECT – PART 3
INSPIRING CREATIVE AND INNOVATIVE MINDS
Create Object using New Operator
• We develop another class that consists
of main() method to create object;
public class TestCircle
{
public static void main(String[] args)
{
// use constructor with parameter
Circle obj1 = new Circle(2.5);
// use default constructor
Circle obj2 = new Circle();
}
}
23
Invoke Method using (.) Operator
public class TestCircle {
public static void main(String[] args){
Circle obj1 = new Circle(2.3);
Circle obj2 = new Circle();
Call method setRadius()
obj2.setRadius(5.6);
obj2.display();
Call method display()
obj1.display();
}
}
24
What Next
• Save the class as “TestCircle.java”.
• Compile using command: javac
TestCircle.java
• Run the class/bytecode using command:
Java TestCircle
Output produced;
Radius of a circle:5.6
Area of the circle:98.4704
Radius of a circle:2.3
Area of the circle:16.610599999999998 25
Summary
• Create object using new operator in a
main() method.
• Call members of class, example method
using dot(.) operator.
• We can create as many objects as we
desired.
26