[go: up one dir, main page]

0% found this document useful (0 votes)
20 views31 pages

Week 4 - L10-L12

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views31 pages

Week 4 - L10-L12

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Subject Name: OOP

Unit No:2
Unit Name: Classes and Objects

Faculty Name : Mr. Dayanand Dhongade


Index

Lecture 10 – Implementation of class and object concept using Java language 3

Lecture 11 – Argument Passing Mechanism 12

Lecture 12 – Method Overloading 18

2
Module No 2: Classes and Objects

Lecture No: 10
Implementation of class and object concept
using Java language
Example: Calculate the area of Circle, Triangle, Square and Rectangle

• Aim: Using classes and method in java write a program to calculate the
area of circle, triangle, square and rectangle.

• Objects are the basic runtime entities in an object oriented system.

• A class may be thought of as a ‘data type’ and an object as ‘variable’ of


that data type.

• The data and code of the class are accessed by object of that class.

Lecture 10- Implementation of class and object concept using Java language 4
Example: Calculate the area of Circle, Triangle, Square and Rectangle

• Syntax:

class Class_Name
{
public static void main(String args[])
{
// Statements;
}
}

Lecture 10- Implementation of class and object concept using Java language 5
Example: Calculate the area of Circle, Triangle, Square and Rectangle

• Algorithm:

1. Start.

2. Declare a class named Area.

3. Define a method circle() to calculate area of circle.

4. Define a method triangle() to calculate area of triangle.

5. Define a method square() to calculate area of square.

6. Define a method rectangle() to calculate area of rectangle.

7. Define a class named Calculate.

8. Create object of Area class and invoke methods of circle, triangle, square and
rectangle methods inside main() method.

9. Stop.

Lecture 10- Implementation of class and object concept using Java language 6
Example: Calculate the area of Circle, Triangle, Square and Rectangle

• Program:

import java.util.*;
class Area
{
void circle(double r)
{
double area1 = 3.14 * r * r;
System.out.println("The area of the circle is :" + area1);
}
void triangle( double ht, double bs)
{
double area2= ht*bs*0.5;
System.out.println("Area of traingle is "+area2);
}

Lecture 10- Implementation of class and object concept using Java language 7
Example: Calculate the area of Circle, Triangle, Square and Rectangle

• Program:

void square(int s)
{
int area3=s*s;
System.out.println("Area of square is "+area3);
}
void rectangle(int lth, int wd)
{
int area4=lth*wd;
System.out.println("Area of Rectangle is "+area4);
}
}

Lecture 10- Implementation of class and object concept using Java language 8
Example: Calculate the area of Circle, Triangle, Square and Rectangle

• Program:

class Calculate{
public static void main(String args[])
{
Area ob=new Area();
Scanner sc = new Scanner(System.in);
System.out.println("Enter Radius of Circle:");
double radius = sc.nextDouble();
System.out.println("Enter Base of Triangle:");
double base = sc.nextDouble();
System.out.println("Enter Height of Triangle:");
double height = sc.nextDouble();
System.out.println("Enter Side of Square");
int side = sc.nextInt();

Lecture 10- Implementation of class and object concept using Java language 9
Example: Calculate the area of Circle, Triangle, Square and Rectangle

• Program:

System.out.println("Enter Length of Rectangle");


int length = sc.nextInt();
System.out.println("Enter Breadth of Rectangle");
int breadth = sc.nextInt();
ob.circle(radius); // Area of Circle
ob.triangle(base,height); // Area of Triangle
ob.square(side); // Area of Square
ob.rectangle(length,breadth); // Area of Rectangle
}
}

Lecture 10- Implementation of class and object concept using Java language 10
Example: Calculate the area of Circle, Triangle, Square and Rectangle

• Output:

Lecture 10- Implementation of class and object concept using Java language 11
Module No 2: Classes and Objects

Lecture No: 11
Argument Passing Mechanism
Methods with Parameters/Arguments

class Circle class MyMain


{ {
double c ,a; //area and circumference public static void main(String args[])
{
Circle c1; // creating reference
//method definition c1 = new Circle(); // creating object
void circumference( int r)
{ int radius= 5;
c=2*3.14*r;
// invoking method
System.out.println(“Circumference =“
+c);
c1.circumference(radius);
}
void area(int r) c1.area(radius);
{
a=3.14 * r * r;
System.out.println(“Area =“ +a);
}
} }
}

Lecture 11- Argument Passing Mechanism 13


Methods with Parameters/Arguments.... Impact of access specifiers

class Test { class AccessTest {


int a; // default access public static void main(String args[]) {
public int b; // public access Test ob = new Test();
private int c; // private access ob.a = 10; // OK
ob.b = 20; // OK
// methods to access c ob.c = 100; // Error!
void setc(int i) { // set c's value ob.setc(100); // OK
c = i; System.out.println("a, b, and c: " + ob.a + "
} " +ob.b + " " + ob.getc());
int getc() { // get c's value }}
return c;
}
}

Lecture 11- Argument Passing Mechanism 14


Excercise

• WAP to create class rectangle and calculate area and perimeter of it.

• WAP to create class Student. Add methods to read and display student
information

**Passing Objects as Arguments will be covered after introducing Constructors

Lecture 11- Argument Passing Mechanism 15


Activity

https://crosswordlabs.com/view/crossword-puzzle-on-oop-concepts

16 Lecture 11- Argument Passing Mechanism


Activity- Answer Key

17 Lecture 11- Argument Passing Mechanism


Module No 2: Classes and Objects

Lecture No: 12
Method Overloading
‘static’ Members

• Normally a class member must be accessed only with an object.


• When a member is declared static, it can be accessed before any
objects of its class are created, and without reference to any object.
• There can be a static method, static variable and a static block

• static keyword is used to declare static variables.


Eg: static int counter;

• When objects of class are declared, no copy of a static variable is


made.
• Instead, all instances of the class share the same static variable.

19 Lecture 12- Method Overloading


‘static’ Members –Example 1

class StaticDemo {
public static void main(String
static int a; //static variable args[]) {
int b; //instance variable StaticDemo s1=new StaticDemo();
void increment() { StaticDemo s2=new StaticDemo();
a++; s1.increment();
b++; s2.increment();
} s1.display();
void display() }
{ }
System.out.println(“a= “+a);
System.out.println(“b= “+b);
OUTPUT
}
a=2
b=1

20 Lecture 12- Method Overloading


‘static’ Members

• Initially when object S1 and S2 are created

S1.b=0 StaticDemo.a=0

S2.b=0

• When S1.increment() is called

S1.b=1
StaticDemo.a=1

S2.b=0

• When S2.increment() is called

S1.b=0
StaticDemo.a=2

S2.b=1

21 Lecture 12- Method Overloading


‘static’ Members

• Static methods can only call other static methods

• Static methods must only access static data

• Static block gets executed exactly once, when the class is first loaded

22 Lecture 12- Method Overloading


‘static’ Members- Example 2

class UseStatic {
static int a = 3; OUTPUT
Static block initialized.
static int b;
x = 42
static void meth(int x) { a=3
System.out.println("Static block initialized."); b = 12
b = a * 4;
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
public static void main(String args[]) {
meth(42);
}
}

23 Lecture 12- Method Overloading


‘static’ Members

• Outside of the class in which they are defined, static methods and variables can be used
independently of any object.

• To call a static method and variable from outside its class, class name is used .

• General form:
classname.method( );
Classname.variable1;

24 Lecture 12- Method Overloading


‘static’ Members

class StaticDemo {
static int a = 42; output :
a = 42
static int b = 99; b = 99
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String
args[]) {
StaticDemo.callme();
System.out.println("b = " +
StaticDemo.b);
}
}

25 Lecture 12- Method Overloading


Case Study on Static members

A XYZ company wants to maintain employee details like EID, name, salary and department. Company
will read and display details of employees. When a new employee is joined the company an Employee
ID (EID) is automatically incremented and assigned to him/her.

Class: Employee

Data Members: EID, name, salary , department

Methods: getData()
display()

26 Lecture 12- Method Overloading


Method Overloading

• In Java we can define number of methods in a class with the same name.
• Defining two or more methods with the same name in a class is called method overloading.
• Method overloading is one of the ways that Java implements polymorphism
• When an overloaded method is invoked, Java uses the type and/or number of arguments to
determine which version of the overloaded method to actually call.
• Compile determine which method to execute automatically.
• The return type has no effect on the signature of a method.

27 Lecture 12- Method Overloading


What is Signature of a method?

• The name of a method along with the types and sequence of the parameters is called signature
of the method
• Signature of the method should be unique.

• Signature of the method includes –


1. No of Parameters
2. Different Parameters
3. Sequence of the parameters

• For Example :
int display(int num1,int num2); OR
int display(double num1,int num2); OR
void display(double num1,double num2);

28 Lecture 12- Method Overloading


Method Overloading

class OverloadDemo { class Overload {


public static void main(String args[])
void test()
{
{ System.out.println("No parameters");
} OverloadDemo ob = new OverloadDemo();
void test(int a) double result;
{ System.out.println("a: " + a); // call all versions of test()
ob.test();
}
ob.test(10);
void test(int a, int b) ob.test(10, 20);
{ result = ob.test(123.2);
System.out.println("a and b: " + a + " " System.out.println("Result of ob.test(123.2): "
+ b); + result);
} }
}
double test(double a)
{ System.out.println("double a: " + a);
return a*a;
}
}

29 Lecture 12- Method Overloading


Excercise

• Write a program to overload the plusMethod() method to work for


both int and double.
• Write a program to overload the area() method to calculate area of
different shapes.

30 Lecture 12- Method Overloading


Thank You

You might also like