Java Classes Methods Objects. Classes Classes We have been using classes ever since we started...

34
Java Classes Methods Objects

Transcript of Java Classes Methods Objects. Classes Classes We have been using classes ever since we started...

Page 1: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

JavaClassesMethods Objects

Page 2: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Classes

Page 3: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

ClassesWe have been using classes ever

since we started programming in Java

Whenever we use the keyword class we would be declaring a class

Every program must have a main class in order to work, it is the class that the JVM uses to execute the program

Page 4: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Why are classes used?Classes are used to started off a

program

Classes in Java are also used to create objects (we will talk about objects later on)

A class specifies1. The data it contains2. The code that uses the data

Page 5: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Classes as Building BlocksA classes are the building blocks

to programs

For example and architect draws a plan of the house before it is build, the class is the plan

Page 6: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Advantage of Classes Classes provide reusability in programs

This means that a class can be used over and over again

For example we have a ready made class called the ‘Keyboard.class’, we use this over and over again when we want the program to read an input from the keyboad

Page 7: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

ActivityCreate a program and name the

class myAddingClass

The code within the class should be able to add two numbers

The output should be the result of the addiion

Page 8: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Methods

Page 9: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

What is a method?Programs are used to solve

complex problems and are MUCH longer than the ones we create at school

These programs are split up into a number of sections to make it easier to code and to read

These sections are called methods

Page 10: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

…A method holds its own specific

task which is then joined to other methods to form a complete process

A method contains the actions that the program will actually perform

So first we create a class and within the class we have a number of methods

Page 11: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Example Lets say we have a program to hold

student records

The program could have the following methods

1. A method to calculate if a mark is a pass or fail

2. A method to calculate the average marks

3. A method to draw up a graph of the grades

Page 12: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Why do we use Methods?Splitting a problem into different

methods makes it much easier to program

Methods avoids having to have double code as if we need to use a method twice you would just need to call it

Page 13: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Our First Program

As we can see we have one class called FirstProgram

We have one methods which is the main method

class FirstProgrsam{ public static void main (String args[]) {

System.out.println(“hello World”);

}}

Page 14: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

How do we Identify Methods?We know that a block of code is a

method as we would see the following syntax

The parameter list would be what the method will be using or a statement of when the method should start

method name (parameter list) { // body of the method}

Page 15: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Calling Methods In Java when we say we are calling a

method we mean that we are asking the program to perform the tasks of a method

This is done to save duplicate codes in our program

This makes it easier to write and understand codes

Page 16: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

//using a methods //calling methodsClass callingMethods{

//method findAreastatic int findArea (int lnegth, int width) {

return length * width;}

public static void main (String args[]){//calling method findArea

System.out,println(“Area = “ +findArea(5,3);

System.out,println(“Area = “ +findArea(5,4);

System.out,println(“Area = “ +findArea(5,5);

}}

Page 17: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Static MethodsThe method in the previous

program was a static method

They do not make use of instance variables

Static methods take the parameters and compute/calculate something

Page 18: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Public Methods A public method is a method that

could be used by other classes

In other words another class within the program would be able to use the public method by simply calling them

Page 19: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Void MethodsA void method is basically a

method that contains the keyword ‘void’

Void methods perform an actions but do not output any sort of result

Page 20: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Instance MethodsThis is the default type of method

Instance methods use instance variables (a set variable)

These methods are associated with objects of the program

Page 21: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Objects

Page 22: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

What is an Object?In life we have objects all around us

such as tables, chairs, desks ect…

Every objects has its attributes such as a table has four legs and a straight table top. All tables have this in common – common attributes

Objects also have a behaviour for examples dogs bark, eat and breath. All dogs have these behaviours in common

Page 23: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Object Oriented ProgrammingJava is an object oriented programming

language, programs in java also take into account different objects with attributes and behaviours

For example if we want to draw a triangle on Java we would give it the attributes of the objects – size and colour

We could also give it a behaviour and make it move around the screen

Page 24: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Object characteristics Objects have two main

characteristics;

1. Attributes = how the object looks and what it is - set as an instance variable in Java

2. Behaviour = what action it would do – set as a method in Java

Page 25: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Creating a Program We will be using an architects

plan to find out the following certain attributes of different rooms

So a common object we have is a room, so we would need to create an object room using a class.

Page 26: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

//create a type room by listing the attributes of a roomclass Room{

double width;double height;double length;

}class HomeDemo1{

public static void main (String args[]){//creating an object of type room//object is a room with the name bedroom1 Room bedroom1 = new Room();double volume;//give bedroom1 its attributesbedroom1.width = 4;bedroom1.height = 3;bedroom1.length = 5;

//find the volume of the roomvolume = bedroom1.width * bedroom1.height

*bedroom1.length;System.out.println("Volume of Bedroom1 = " +

volume);}

}

Page 27: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Outputs What is the output of the program?

Volume of Bedroom1 = 60.0

Which is the class that is creating the data type for the room?

Class Room {double width;double height;double length

}

Page 28: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Creating two ObjectsIn the next program we will be creating

two objects

The two objects in this program will be;1. Bedroom1 (width 5, height 3, length 5)2. A Kitchen (width 3, height 3, length 4)

Create a NEW program to output the volume of both bedroom1 and the kitchen

Page 29: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

We need a Method As we can see in out second

program the following code is repeated twice;

When we have repeated code we realise that we need to create a method, this would eliminate the repeated code

volume = bedroom1.width * bedroom1.height * bedroom1.length;

Page 30: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Our new MethodOur new method would need to

work out the volumeWe will name this method

‘getVolume’ to understand what the method will be doing

In the class Room write the following method

//method to calculate the volume double getVolume(){

volume = width * height * length;return volume;

}

STEP ONE

Page 31: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Eliminating CodeRemove the following lines of

code from your program;

STEP TWO

volume = bedroom1.width * bedroom1.height * bedroom1.length;

volume = kitchen.width * kitchen.height * kitchen.length;

Double volume; 1

2

3

Page 32: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Replacing CodeReplace the following code;

With;

STEP THREE

System.out.println("Volume of Bedroom1 = " + volume);

System.out.println("Volume of kitchen = " + volume);

System.out.println("Volume of Bedroom1 = " + bedroom1.getVolume());

System.out.println("Volume of Kitchen = " + kitchen.getVolume());

Page 33: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

Add a Living RoomAdd a living room to your

program with the following attributes;

1. Width = 52. Height = 33. Length = 6

Page 34: Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.

AreaCreate a method called getArea

This method should return the area

So now your program should 1. Hold three objects2. Two methods 3. Output the Volume of all objects4. Output the Area of all objects

Area = ((width * height) * 2) + ((length * height * 2) + (width * length));