Qus 1:
/*1st ques solution*/
import java.io.*;
class Shape
{
void area()
{
System.out.println("No area for shape");
}
}
class circle extends Shape
{
float r;
circle(float r1)
{
r=r1;
}
void area()
{
float ar;
ar=r*r*3.14f;
System.out.println("Area of circle="+ar);
}
}
class rect extends Shape
{
float len,br;
rect(float l,float b)
{
len=l;
br=b;
}
void area()
{
float ar;
ar=len*br;
System.out.println("Area of rectangle="+ar);
}
}
class Dynamic
{
public static void main(String arg[])
{
Shape s1=new Shape();
circle c=new circle(5.3f);
rect r=new rect(4.5f,5.6f);
Shape s;
s=s1;
s.area();
s=c;
s.area();
s=r;
s.area();
}
}
=================
Que 2 :
/*2nd que solution*/ class Simple { int a,b; Simple(int a1,int b1) { a=a1; b=b1; } void add() { int c; c=a+b; System.out.println("Sum="+c); } void add(int a1,int b1) { int c; c=a1+b1; System.out.println("Sum="+c); } } class Overload { public static void main(String arg[]) { Simple ob=new Simple(2,5); ob.add(); ob.add(10,20); } }
================
Que 3 :
/*3rd que solution*/ class Electricity { int unit; float cost; Electricity(int u,float c) { unit=u; cost=c; } void display() { System.out.println("Unit="+unit); System.out.println("Cost="+cost); } void cal() { float t; t=unit*cost; System.out.println("Elec Bill="+t); } } class Phone extends Electricity { int unit; float cost; Phone(int u1,float c1,int u2,float c2) { super(u1,c1); unit=u2; cost=c2; } void disp() { System.out.println("Unit="+unit); System.out.println("Cost="+cost); } void calculate() { super.cal(); System.out.println("Phone Bill="+(unit*cost)); } } class SuperDemo { public static void main(String arg[]) { Phone ph=new Phone(5,3.5f,3,5.7f); ph.display(); ph.disp(); ph.calculate(); } }
========
Que 4 ;
/*4th que solution*/ class Domestic { int unit; float cost; Domestic(int u,float c) { unit=u; cost=c; } void display() { System.out.println("Unit="+unit); System.out.println("Cost="+cost); } void calculate() { float t; t=unit*cost; System.out.println("Elec Bill="+t); } } class Commercial extends Domestic { int unit; float cost; Commercial(int u1,float c1,int u2,float c2) { super(u1,c1); unit=u2; cost=c2; } void display() { super.display(); System.out.println("Unit="+unit); System.out.println("Cost="+cost); } void calculate() { super.calculate(); System.out.println("Commercial Bill="+(unit*cost)); } } class OverrideDemo { public static void main(String arg[]) { Commercial ob=new Commercial(5,3.5f,3,5.7f); ob.display(); ob.calculate(); } }
======
Que 5:
/*5th que solution*/ import java.io.*; abstract class Bank { String name; int ano; float bal; void read() { DataInputStream sd=new DataInputStream(System.in); try { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter Name:"); name=br.readLine(); System.out.println("Enter account no:"); ano=Integer.parseInt(br.readLine()); System.out.println("Enter Balance:"); bal=Float.parseFloat(br.readLine()); } catch(Exception e) {} } void deposit() { float d; try { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter Amount:"); d=Float.parseFloat(br.readLine()); bal=bal+d; System.out.println("Balance="+bal); } catch(Exception e) {} } abstract void withdraw(float amount); } class Bankaccess extends Bank { void withdraw(float amount) { try { if(bal-1000<amount) System.out.println("Cant withdraw"); else { bal=bal-amount; System.out.println("Balance="+bal); } } catch(Exception e) {} } } class MainDemo1 { public static void main(String arg[])throws IOException { Bankaccess ob=new Bankaccess(); String ch; float w; ob.read(); BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.println("Want to withdraw or deposit...(w/d)"); ch=br.readLine(); if(ch.equals("w")) { System.out.println("Enter Amount:"); w=Float.parseFloat(br.readLine()); ob.withdraw(w); } else if(ch.equals("d")) ob.deposit(); else System.out.println("Wrong Entry"); } }
/*1st ques solution*/
import java.io.*;
class Shape
{
void area()
{
System.out.println("No area for shape");
}
}
class circle extends Shape
{
float r;
circle(float r1)
{
r=r1;
}
void area()
{
float ar;
ar=r*r*3.14f;
System.out.println("Area of circle="+ar);
}
}
class rect extends Shape
{
float len,br;
rect(float l,float b)
{
len=l;
br=b;
}
void area()
{
float ar;
ar=len*br;
System.out.println("Area of rectangle="+ar);
}
}
class Dynamic
{
public static void main(String arg[])
{
Shape s1=new Shape();
circle c=new circle(5.3f);
rect r=new rect(4.5f,5.6f);
Shape s;
s=s1;
s.area();
s=c;
s.area();
s=r;
s.area();
}
}
=================
Que 2 :
/*2nd que solution*/ class Simple { int a,b; Simple(int a1,int b1) { a=a1; b=b1; } void add() { int c; c=a+b; System.out.println("Sum="+c); } void add(int a1,int b1) { int c; c=a1+b1; System.out.println("Sum="+c); } } class Overload { public static void main(String arg[]) { Simple ob=new Simple(2,5); ob.add(); ob.add(10,20); } }
================
Que 3 :
/*3rd que solution*/ class Electricity { int unit; float cost; Electricity(int u,float c) { unit=u; cost=c; } void display() { System.out.println("Unit="+unit); System.out.println("Cost="+cost); } void cal() { float t; t=unit*cost; System.out.println("Elec Bill="+t); } } class Phone extends Electricity { int unit; float cost; Phone(int u1,float c1,int u2,float c2) { super(u1,c1); unit=u2; cost=c2; } void disp() { System.out.println("Unit="+unit); System.out.println("Cost="+cost); } void calculate() { super.cal(); System.out.println("Phone Bill="+(unit*cost)); } } class SuperDemo { public static void main(String arg[]) { Phone ph=new Phone(5,3.5f,3,5.7f); ph.display(); ph.disp(); ph.calculate(); } }
========
Que 4 ;
/*4th que solution*/ class Domestic { int unit; float cost; Domestic(int u,float c) { unit=u; cost=c; } void display() { System.out.println("Unit="+unit); System.out.println("Cost="+cost); } void calculate() { float t; t=unit*cost; System.out.println("Elec Bill="+t); } } class Commercial extends Domestic { int unit; float cost; Commercial(int u1,float c1,int u2,float c2) { super(u1,c1); unit=u2; cost=c2; } void display() { super.display(); System.out.println("Unit="+unit); System.out.println("Cost="+cost); } void calculate() { super.calculate(); System.out.println("Commercial Bill="+(unit*cost)); } } class OverrideDemo { public static void main(String arg[]) { Commercial ob=new Commercial(5,3.5f,3,5.7f); ob.display(); ob.calculate(); } }
======
Que 5:
/*5th que solution*/ import java.io.*; abstract class Bank { String name; int ano; float bal; void read() { DataInputStream sd=new DataInputStream(System.in); try { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter Name:"); name=br.readLine(); System.out.println("Enter account no:"); ano=Integer.parseInt(br.readLine()); System.out.println("Enter Balance:"); bal=Float.parseFloat(br.readLine()); } catch(Exception e) {} } void deposit() { float d; try { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter Amount:"); d=Float.parseFloat(br.readLine()); bal=bal+d; System.out.println("Balance="+bal); } catch(Exception e) {} } abstract void withdraw(float amount); } class Bankaccess extends Bank { void withdraw(float amount) { try { if(bal-1000<amount) System.out.println("Cant withdraw"); else { bal=bal-amount; System.out.println("Balance="+bal); } } catch(Exception e) {} } } class MainDemo1 { public static void main(String arg[])throws IOException { Bankaccess ob=new Bankaccess(); String ch; float w; ob.read(); BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.println("Want to withdraw or deposit...(w/d)"); ch=br.readLine(); if(ch.equals("w")) { System.out.println("Enter Amount:"); w=Float.parseFloat(br.readLine()); ob.withdraw(w); } else if(ch.equals("d")) ob.deposit(); else System.out.println("Wrong Entry"); } }
No comments :
Post a Comment