[go: up one dir, main page]

0% found this document useful (0 votes)
143 views10 pages

area of Rectangle and Circle Using Method Overloading

The document contains code snippets demonstrating various Java concepts like method overloading, constructors, inheritance, interfaces, exceptions, and access modifiers. Specifically: 1) It shows calculating area of circle and rectangle using method overloading. 2) Calculating simple interest by passing an object as argument to a method. 3) Creating a student class with constructor and methods to print student marks and grades. 4) Summing numbers by passing them to a constructor and using this reference. 5) Handling exceptions while inserting into an array and dividing by zero. 6) Defining a custom exception to find largest of two numbers. 7) Using an interface to calculate area of a circle. 8

Uploaded by

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

area of Rectangle and Circle Using Method Overloading

The document contains code snippets demonstrating various Java concepts like method overloading, constructors, inheritance, interfaces, exceptions, and access modifiers. Specifically: 1) It shows calculating area of circle and rectangle using method overloading. 2) Calculating simple interest by passing an object as argument to a method. 3) Creating a student class with constructor and methods to print student marks and grades. 4) Summing numbers by passing them to a constructor and using this reference. 5) Handling exceptions while inserting into an array and dividing by zero. 6) Defining a custom exception to find largest of two numbers. 7) Using an interface to calculate area of a circle. 8

Uploaded by

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

//AREA OF RECTANGLE AND CIRCLE USING METHOD OVERLOADING

class a
{
void area(int r)
{
System.out.println("AREA OF A CIRCLE=" +(3.14*r*r));
}
void area(int l,int b)
{
System.out.println("AREA OF A RECTANGLE=" +l*b);
}
}
class mover
{
public static void main(String arg[])
{
a x=new a();
x.area(3);
x.area(3,3);
}
}
OUTPUT:

AREA OF A CIRCLE=28.26

AREA OF A RECTANGLE=9
//SIMPLE INTEREST CALCULATION USING OBJECT AS AN ARGUMENT

class obap
{
double p,n,r;
obap(double x,double y,double z)
{
p=x;
n=y;
r=z;
}
double ca(obap a)
{
return(a.p*a.r*a.n/100);
}
}
class obasp
{
public static void main(String arg[])
{
obap m=new obap(1500,5,15);
System.out.println("SIMPLE INTEREST="+ m.ca(m));
}
}
OUTPUT:

SIMPLE INTEREST=1125.0
//STUDENT MARKLIST USING CONSTRUCTOR AND METHODS

class student
{
String n;
static int r;
int rn,m1,m2,m3;
student(String nm)
{
r++;
n=nm;
rn=r;
}
void calc(int n1,int n2,int n3,String ad)
{
int r=0;
System.out.println("NAME="+ n);
System.out.println("ROLL NO.="+ rn);
m1=n1;
m2=n2;
m3=n3;
if(m1<40 || m2<40 || m3<40)
{
r=1;
}
int t=m1+m2+m3;
double avg=t/3;
System.out.println("SUB1 MARK="+ m1);
System.out.println("SUB2 MARK="+ m2);
System.out.println("SUB3 MARK="+ m3);
System.out.println("TOTAL="+ t);
System.out.println("AVG.="+ avg);
if(avg>=80 && r==0)
{
System.out.println("GRADE=EXCELLENT");
}
else if(avg>=65 && r==0)
{
System.out.println("GRADE=GOOD");
}
else if(avg>=50 && r==0)
{
System.out.println("GRADE=PASS");
}
else
{
System.out.println("GRADE=FAIL");
}
System.out.println(ad);
System.out.println("\n");
}
static void nofs()
{
System.out.println("NUMBER OF STUDENTS ARE "+ r);
}
}
class std
{
public static void main(String z[])
{
student s1=new student("AAA");
student s2=new student("BBB");
s1.calc(45,75,54,"1/45 NEHRU STREET,COVAI");
s2.calc(39,90,87,"4/85 MGR NAGAR,PALANI");
s1.nofs();
}
}

OUTPUT:

NAME=AAA
ROLL NO.=1
SUB1 MARK=45
SUB2 MARK=75
SUB3 MARK=54
TOTAL=174
AVG.=58.0
GRADE=PASS
1/45 NEHRU STREET,COVAI

NAME=BBB
ROLL NO.=2
SUB1 MARK=39
SUB2 MARK=90
SUB3 MARK=87
TOTAL=216
AVG.=72.0
GRADE=FAIL
4/85 MGR NAGAR,PALANI

NUMBER OF STUDENTS ARE 2


//SUM OF NUMBERS USING THIS REFERENCE

class thvar
{
int a,b,c;
thvar(int a,int b,int c)
{
this.a=a;
this.b=b;
this.c=c;
}
void putdet()
{
System.out.println("SUM=" +(a+b+c));
}
}
class ths
{
public static void main(String k[])
{
thvar i=new thvar(2,5,6);
i.putdet();
}
}

OUTPUT:

SUM=13
//INSERT NUMBERS IN ARRAY USING EXCEPTION HANDLING

import java.lang.*;
class exp
{
public static void main(String arg[])
{
int a[]={3,4},b=2,c=0,d;
try
{
try
{
a[5]=3;
System.out.println("IT IS ERROR");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("NO MEMORY SPACE.");
}
finally
{
System.out.println("IT CAUSE ERROR");
}
d=b/c;
}
catch(ArithmeticException ae)
{
System.out.println("INFINITE VALUE");
}
catch(ArrayIndexOutOfBoundsException ao)
{
System.out.println("NO MEMORY SPACE");
}

}
}

OUTPUT:

NO MEMORY SPACE.

IT CAUSE ERROR

INFINIT VALUE
//USING USER DEFINED EXCEPTION FIND THE LARGEST NUMBER

import java.lang.*;
class NegException extends Exception
{
NegException(String message)
{
super(message);
}
}
class ude
{
public static void main(String arg[])
{
int a=2,b=4;
try
{
if(a<b)
{
throw new NegException("B IS LARGEST");
}
else
{
throw new NegException("A IS LARGEST");
}
}
catch(NegException n)
{
System.out.println(n.getMessage());
}
}
}

OUTPUT:

B IS LARGEST
//AREA OF A CIRCLE USING INTERFACE

interface a
{
final double pi=3.14;
int r=3;
}
interface b extends a
{
double area();
}
class ab implements b
{
public double area()
{
return(pi*r*r);
}
}
class inf
{
public static void main(String arg[])
{
ab x=new ab();
double z=x.area();
System.out.println("AREA=" +z);
}
}

OUTPUT:

AREA=28.259999999999998
//VOLUME AND AREA OF A RECTANGLE USING SUPER REFERENCE

class a
{
int l,b;
a(int i,int j)
{
l=i;
b=j;
}
int area()
{
return(l*b);
}
}
class b extends a
{
int h;
b(int l,int b,int z)
{
super(l,b);
h=z;
}
int vol()
{
return(l*b*h);
}
}
class av
{
public static void main(String arg[])
{
a m=new a(10,12);
b n=new b(10,12,3);
int p=m.area();
int q=n.vol();
System.out.println("AREA=" +p);
System.out.println("VOLUME=" +q);

}
}

OUTPUT:

AREA=120
VOLUME=360
//STORE MANAGEMENT USING ACCESS MODIFIERS

class one
{
public String iname;
int sn,n;
protected double p;
private double pr;
one(String nm,int s,int r,double pz,double pt)
{
iname=nm;
sn=s;
n=r;
p=pz;
pr=pt;
}
void show()
{
System.out.println("ITEM NAME=" +iname);
System.out.println("S.NO.=" +sn);
System.out.println("NO. OF. ITEMS=" +n);
System.out.println("PRICE=" +p);
System.out.println("TOTAL=" +(p*n));
System.out.println("PROFIT(FOR OWNER)=" +pr);
}
}
class afs
{
public static void main(String arg[])
{
one x=new one("FAN",127,5,2450,415);
x.show();
}
}

OUTPUT:

ITEM NAME=FAN
S.NO=127
NO. OF. ITEMS=5
PRICE=2450.0
TOTAL=12250.0
PROFIT(FOR OWNER)=415.0

You might also like