0% found this document useful (0 votes)
43 views

Java Practical File

The document implements inheritance and polymorphism concepts in Java. It defines an Account superclass with deposit and withdraw methods. SavingAccount and CurrentAccount subclasses extend Account and override the withdraw method. The main method creates instances of CurrentAccount and SavingAccount, calls their methods to deposit and withdraw money, and prints their details.

Uploaded by

Umang Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Java Practical File

The document implements inheritance and polymorphism concepts in Java. It defines an Account superclass with deposit and withdraw methods. SavingAccount and CurrentAccount subclasses extend Account and override the withdraw method. The main method creates instances of CurrentAccount and SavingAccount, calls their methods to deposit and withdraw money, and prints their details.

Uploaded by

Umang Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Ques 1: Implement Box class and BoxWeight class using constructor overloading.

class Box{
double length;
double breadth;
double height;
Box(double length, double breadth, double height) {
this.length=length;
this.breadth=breadth;
this.height=height;
}
Box() {}
public void showDetails() {
String box="\nLength of box: "+length+" m";
box+="\nBreadth of box: "+breadth+" m";
box+="\nHeight of box: "+height+" m";
System.out.println(box);
}
public double volume() {
return length*breadth*height;
}
}
class BoxWeight extends Box{
double weight;
BoxWeight(double length, double breadth, double height, double weight) {
super(length, breadth, height);
this.weight=weight;
}
public void showDetails() {
String boxWeight="\nLength of box: "+length+" m";
boxWeight+="\nBreadth of box: "+breadth+" m";
boxWeight+="\nHeight of box: "+height+" m";
boxWeight+="\nWeight of box: "+weight+" Kg";
System.out.println(boxWeight); }
}
public class BoxDemo {
public static void main(String[] args) {
Box b1=new Box();
b1.showDetails();
System.out.println("Volume of box: "+b1.volume());
b1=new Box(5,8,3);
b1.showDetails();
System.out.println("Volume of box: "+b1.volume());
b1=new BoxWeight(15,49,33,8);
b1.showDetails();

}
}
OUTPUT:
Ques 2: Implement Inheritance using Account class
class Account{

protected String accountNo;

protected String holderName;

protected double amount;

public Account(String accountNo, String holderName, double amount) {

this.accountNo=accountNo;

this.holderName=holderName;

this.amount=amount;

public void deposit(double amount) {

this.amount+=amount;

System.out.println("Amount Added to your account successfully.");

System.out.println("Your New Balance is : "+this.amount+" INR");

class SavingAccount extends Account{

double interestRate;

final double minAmount=500.0;

final int maxTransaction=30;

public SavingAccount(String accountNo,String holderName,double amount,double


interestRate){

super(accountNo, holderName, amount);

this.interestRate = interestRate;

public void withdraw(double amount) {

if(this.amount-amount>minAmount) {

this.amount-=amount;

System.out.println("Withdraw succesfully.");
System.out.println("Your New Balance is : "+this.amount+" INR");

else

System.out.println("Sorry! You have not sufficient Amount in your account.");


}

@Override

public String toString() {

String crntAcc="Account no : "+accountNo;

crntAcc+="\nAccount Holder : "+holderName;

crntAcc+="\nAmount : "+amount+" INR";

crntAcc+="\nInterest Rate : "+interestRate+" %";

crntAcc+="\nMaximum Transaction : "+maxTransaction;

return crntAcc;

class CurrentAccount extends Account{

double interestRate;

final double minAmount=-50000;

public CurrentAccount(String accountNo,String holderName,double amt,double


interestRate){

super(accountNo, holderName, amt);

this.interestRate = interestRate;

@Override

public String toString() {

String savAcc="Account no : "+accountNo;

savAcc+="\nAccount Holder : "+holderName;

savAcc+="\nAmount : "+amount+" INR";

savAcc+="\nInterest Rate : "+interestRate+" %";


savAcc+="\nMaximum Transaction : INFINITE ";

return savAcc;

public void withdraw(double amount) {

if(this.amount-amount>minAmount) {

this.amount-=amount;

System.out.println("Withdraw succesfully.");

System.out.println("Your New Balance is : "+this.amount+" INR");

else

System.out.println("Sorry! You have not sufficient Amount in your account.");

public class BankAccount {

public static void main(String[] args) {

CurrentAccount crnt=new CurrentAccount("CUR-1340256","Umang


jain",1356.26,5);

System.out.println(crnt);

crnt.deposit(132.56);

crnt.withdraw(400);

System.out.println();

SavingAccount savAcc=new SavingAccount("SAV-


4562193","Samsung",4365.259,1.2);

System.out.println(savAcc);

savAcc.withdraw(4000);

}
OUTPUT:
Ques 3: Implement abstract class.
import java.util.Scanner;

abstract class Figure{


double dim1;
double dim2;
public Figure(double dim1, double dim2) {
this.dim1 = dim1;
this.dim2 = dim2;
}
abstract public double area();
}
class Rectangle extends Figure{
public Rectangle(double dim1, double dim2) {
super(dim1, dim2);
}
public double area() {
return dim1*dim2;
}
}
class Triangle extends Figure{
public Triangle(double dim1, double dim2) {
super(dim1, dim2);
}
public double area() {
return (dim1*dim2)/2;
}
}
public class Area {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("\nEnter length of Rectangle: ");
double length = input.nextDouble();
System.out.println("Enter breadth of Rectangle: ");
double breadth = input.nextDouble();
Rectangle rec = new Rectangle(length,breadth);

System.out.println("Area of rectangle: "+rec.area());


System.out.println("\nEnter base of triangle: ");
double base = input.nextDouble();
System.out.println("Enter height of triangle: ");
double height = input.nextDouble();
Triangle tri = new Triangle(base,height);
System.out.println("Area of triangle: "+tri.area());
input.close();
}
}
OUTPUT:
Ques 4: Implement Inheritance.
import java.util.Scanner;
class Circle{
double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI*Math.pow(radius, 2);
}
}
class Cylinder extends Circle{
double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double volume() {
return height*area();
}
}
public class Volume {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("\nEnter the radius of circle: ");
double r = input.nextDouble();
Circle area = new Circle(r);
System.out.println("Area of circle: "+area.area());
System.out.println("\nEnter the height of cylinder: ");
double h = input.nextDouble();
Cylinder vol = new Cylinder(r,h);
System.out.println("Volume of cylinder: "+vol.volume());
input.close();
}
}
OUTPUT:

You might also like