Week 4 - L10-L12
Week 4 - L10-L12
Unit No:2
Unit Name: Classes and Objects
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.
• 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.
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:
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
• 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
https://crosswordlabs.com/view/crossword-puzzle-on-oop-concepts
Lecture No: 12
Method Overloading
‘static’ Members
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
S1.b=0 StaticDemo.a=0
S2.b=0
S1.b=1
StaticDemo.a=1
S2.b=0
S1.b=0
StaticDemo.a=2
S2.b=1
• Static block gets executed exactly once, when the class is first loaded
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);
}
}
• 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;
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);
}
}
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
Methods: getData()
display()
• 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.
• 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.
• For Example :
int display(int num1,int num2); OR
int display(double num1,int num2); OR
void display(double num1,double num2);