CLASS AND METHODS

Object-oriented programming

 

Application sw &system sw based opp

Object-oriented programming languages: C++ JAVA C# VB

OOP à class, object, inheritance, polymorphism

 

Class

Class

-         abstract data type ADT

-         a collection of attribute (properties ), methods and events

-         represents a thing like car, house, people

 

Example
Create a class called Cecs

 

Class named Cecs

 

Attributes :

-syllabus

-exams

-lab assignments

-class roster

 

Objec methods

-create/edit a syllabus

-create exam

-Create lab assignments

-Grade exams
 

Example:
 CLASS CALLED Human

 

ATTRIBUTES

-         Head

-         Eyes

-         Arms legs

 

METHODS

-         WALK

-         EAT

-         DRINK

 

 

Object
 

Object- an instance of a class

int x; à x = variable

 

int = primitive data type

 

String s;   à  s = object     String= class

Declare an object

Classname  objectname;

Example

String s;

CECS objCecs174;

 

Create an object

Allocate the memory for the object 

Classname objectname =new ClassName(Parameters);
new JAVA keyword allocate the memory
ClassName(Parameters) - called constructor

Example
 

Cecs  objCECS174;
objCECS174 = new Cecs( );

Declare and create an object
Example
 

Cecs  objCECS174 =  new Cecs( );


Calling an object method
Object must be created before calling an object method

           

Object.method
 

Example

Human nguyen;
nguyen.sleep( );  //Error object not declared

Example

Human nguyen = new Human();;
nguyen.sleep( );  //OK 

 

Java Classes                                                  Object Methods

-         JOptionPane                                         ShowInputDialog

-         Integer                                                  parseInt

-         Double                                                 parseDouble

 

Example

num1= Integer.parseInt (Input); 

 

Programming with Turtle

 

Class World

EX

 

>World w1= new World ( );

Window pops out

>System.out.println(w1);

A 640 by 480 World with 0 turtles in it  //OUTPUT

 

Object methods of class Turtle
 

forward (int numberofstep);       MOVE FORWARD BY THE PASSING NUMBEROFSTEP

 

turnLeft( );                                TURN THE HEAD LEFT 90 DEGREES

 

turnRight ( );                             TURN THE HEAD RIGHT 90 DEGREES

 

turn ( );                                     TURN BY THE SPECIFIED ANGLE 

A NEGATIVE ANGLE WILL TURN THAT MUCH TO THE LEFT, A POSTIVE ANGLE WILL TURN
            THAT MUCH TO THE RIGHT

 

MoveTo (int x, int y);               MOVE TO A SPECIFED X AND Y LOCATION

 

hide ( )                                     STOP SHOW THE TURTLE

 

show( );                                   SHOW THE TURTLE

 

 

 

Example:
Using Turtle class to draw a square

> World w1 = new World ( );

 

> Turtle t1 = new Turtle ( w1);

A world window pops up

 

Note:
Coordinate (0,0) origin is always at the upper left hand corner

Turtle is positioned at (320,240)

 

> t1.turnRight( );

>t1.forward (30);

 

> t1.turnRight ( );

 

> t1.forward (30);

 

> t1.turnRight ();

> t1.forward (30);

 

> t1.turnRight( );

> t1.forward (30);

 

> t1.hide ( );

 

Create an object method in the class Turtle

-Open the Turtle.java program in the BookClasses folder
-Insert the code below at the end of the class

 

public void drawSquare( )

{ this.turnRight( );         //this. = the java keyword, reference to the object called the method

this. forward(30);

this.turnRight ( );

this.forward(30);

this.turnRight( );

this.forward(30);

this.turnRight( );

this.forward(30);

}

 

 

- Compile the file

- test the method drawSquare

> World w2 = new World ( );

> Turtle t2 = new Turtle (w2);

> t2.drawSquare ( );

 

Passing an argument to the method

 

public void drawSquareSize (int width)

{this.turnRight( );

  this.forward(width);
}

 

Test the method drawsquareSize

 

>World w3 = new World ( );

>Turtle t3 = new Turtle ( w3);

>int x = 40

> t3 .drawSquareSize (x);

> t3 .drawSquareSize (100);