Assignment No:5
( Topic : Inheritance, Abstract Class )
Write a program that creates a class Account that have members customer
name, account number. Account has constructor to initialize its members and
method display() to show the result. Create a child class Savings_Account
that is derived from Account class. Savings_Account have members min_bal
and saving_bal. Use show() to display its details. Then create another child
class Account_details from Savings_Account class have members deposit,
withdrawl and a method show1() to show its details. Child classes have
constructors of their own. Create a driver class that creates a record of
customer and display all its details.
class Account {
String customerName;
String accountNumber;
Account(String customerName, String accountNumber) {
[Link] = customerName;
[Link] = accountNumber;
}
void display() {
[Link]("Customer Name: " + customerName);
[Link]("Account Number: " + accountNumber);
}
}
class Savings_Account extends Account {
double min_bal;
double saving_bal;
Savings_Account(String customerName, String accountNumber, double min_bal, double
saving_bal) {
super(customerName, accountNumber);
this.min_bal = min_bal;
this.saving_bal = saving_bal;
}
void show() {
display();
[Link]("Minimum Balance: " + min_bal);
[Link]("Saving Balance: " + saving_bal);
}
24BCSA92
}
class Account_details extends Savings_Account {
double deposit;
double withdrawal;
Account_details(String customerName, String accountNumber, double min_bal, double
saving_bal, double deposit, double withdrawal) {
super(customerName, accountNumber, min_bal, saving_bal);
[Link] = deposit;
[Link] = withdrawal;
}
void show1() {
show();
[Link]("Deposit: " + deposit);
[Link]("Withdrawal: " + withdrawal);
}
}
public class Driver {
public static void main(String[] args) {
Account_details cust = new Account_details("John Doe", "123456789", 500, 1500, 300,
100);
cust.show1();
}
}
OUPUT:
Customer Name: John Doe
Account Number: 123456789
Minimum Balance: 500.0
Saving Balance: 1500.0
Deposit: 300.0
Withdrawal: 100.0
24BCSA92
Create a class Figure with instance members dim1 and dim2. Use constructor
and area() that returns the area of figure. Create a derived class Rectangle
derived from Figure and area() that returns the area of rectangle. Create
another derived class Triangle and Square that has area() which returns area
of Triangle and Square respectively. Derived class have appropriate
constructor. Using method overriding concept test the functionalities of
derived class by creating objects and super class memory references in Driver
class.
class Figure {
double dim1, dim2;
Figure(double dim1, double dim2) {
this.dim1 = dim1;
this.dim2 = dim2;
}
double area() {
return 0;
}
}
class Rectangle extends Figure {
Rectangle(double dim1, double dim2) {
super(dim1, dim2);
}
@Override
double area() {
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double dim1, double dim2) {
super(dim1, dim2);
}
@Override
double area() {
return 0.5 * dim1 * dim2;
}
}
class Square extends Figure {
Square(double side) {
24BCSA92
super(side, side);
}
@Override
double area() {
return dim1 * dim2;
}
}
public class Driver {
public static void main(String[] args) {
Figure f;
f = new Rectangle(5, 10);
[Link]("Area of Rectangle: " + [Link]());
f = new Triangle(5, 10);
[Link]("Area of Triangle: " + [Link]());
f = new Square(7);
[Link]("Area of Square: " + [Link]());
}
}
OUPUT:
Area of Rectangle: 50.0
Area of Triangle: 25.0
Area of Square: 49.0
24BCSA92
Write a program to create a class named Shape. It should contain two
methods, draw() and erase() that prints “Drawing Shape” and “Erasing
Shape” respectively. For this class, create three sub classes, Circle, Triangle
and Squareand each class should override the parent class functions - draw ()
and erase (). The draw() method should print “Drawing Circle”, “Drawing
Triangle” and “Drawing Square” respectively. The erase() method should
print “Erasing Circle”, “Erasing Triangle” and “Erasing Square” respectively.
Create objects of Circle, Triangle and Square, assign each to Shape
variable(reference) and call draw() and erase() method using each object.
class Shape {
void draw() {
[Link]("Drawing Shape");
}
void erase() {
[Link]("Erasing Shape");
}
}
class Circle extends Shape {
@Override
void draw() {
[Link]("Drawing Circle");
}
@Override
void erase() {
[Link]("Erasing Circle");
}
}
class Triangle extends Shape {
@Override
void draw() {
[Link]("Drawing Triangle");
}
@Override
void erase() {
[Link]("Erasing Triangle");
}
}
class Square extends Shape {
@Override
void draw() {
[Link]("Drawing Square");
24BCSA92
}
@Override
void erase() {
[Link]("Erasing Square");
}
}
public class DriverShape {
public static void main(String[] args) {
Shape s;
s = new Circle();
[Link]();
[Link]();
s = new Triangle();
[Link]();
[Link]();
s = new Square();
[Link]();
[Link]();
}
}
OUTPUT:
Drawing Circle
Erasing Circle
Drawing Triangle
Erasing Triangle
Drawing Square
Erasing Square
24BCSA92
Define an abstract class named “Figure”, having data members dim1 and
dim2. Extend this class to create two concrete classes named Rectangle and
Triangle. Override the getArea() method in the sub classes. Invoke the
getArea() method in the main method of another Driver class through the
abstract class reference variable.
abstract class Figure {
double dim1, dim2;
Figure(double dim1, double dim2) {
this.dim1 = dim1;
this.dim2 = dim2;
}
abstract double getArea();
}
class Rectangle extends Figure {
Rectangle(double dim1, double dim2) {
super(dim1, dim2);
}
@Override
double getArea() { OUTPUT:
return dim1 * dim2; Rectangle Area: 200.0
} Triangle Area: 100.0
}
class Triangle extends Figure {
Triangle(double dim1, double dim2) {
super(dim1, dim2);
}
@Override
double getArea() {
return 0.5 * dim1 * dim2;
}
}
public class DriverFigure {
public static void main(String[] args) {
Figure fig;
fig = new Rectangle(10, 20);
[Link]("Rectangle Area: " + [Link]());
fig = new Triangle(10, 20);
[Link]("Triangle Area: " + [Link]());
}
}
24BCSA92
Create a class Point2D with the data member x and y coordinate. Use default
and parameterised constructor to set the coordinate values and display() to
show the coordinates. Create a subclass called Point3D which is derived from
the superclass Point2D with data members z coordinate and has constructor
to initialize the input and show() method to display the coordinates. Test the
methods of both the classes by creating objects in the main method of driver
class.
class Point2D {
protected int x, y;
Point2D() {
this.x = 0;
this.y = 0; OUPUT:
} Point2D Coordinates: (0, 0)
Point2D(int x, int y) { Point2D Coordinates: (5, 10)
this.x = x; Point3D Coordinates: (7, 14, 21)
this.y = y;
}
void display() {
[Link]("Point2D Coordinates: (" + x + ", " + y + ")");
}
}
class Point3D extends Point2D {
private int z;
Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
void show() {
[Link]("Point3D Coordinates: (" + x + ", " + y + ", " + z + ")");
}
}
public class DriverPoint {
public static void main(String[] args) {
Point2D p1 = new Point2D();
[Link]();
Point2D p2 = new Point2D(5, 10);
[Link]();
Point3D p3 = new Point3D(7, 14, 21);
[Link]();
}
}
24BCSA92
Create the classes as given in the below figure. Display the interest rate in the
following format:
SBI Rate of Interest : 8
ICICI Rate of Interest : 7
AXIS Rate of Interest : 9
class Bank {
float getRateOfInterest() {
return 0;
}
}
class SBI extends Bank {
@Override
float getRateOfInterest() {
return 8;
}
}
class ICICI extends Bank {
@Override
float getRateOfInterest() {
return 7;
}
}
class AXIS extends Bank {
@Override
float getRateOfInterest() {
return 9;
}
}
public class DriverBank {
public static void main(String[] args) {
Bank b;
b = new SBI();
[Link]("SBI Rate of Interest : " + b. getRateOfInterest());
24BCSA92
b = new ICICI();
[Link]("ICICI Rate of Interest : " + b. getRateOfInterest());
b = new AXIS();
[Link]("AXIS Rate of Interest : " + b. getRateOfInterest());
}
}
OUPUT:
SBI Rate of Interest : 8
ICICI Rate of Interest : 7
AXIS Rate of Interest : 9
Create a class Person that has data member name. Use constructor to
initialize name and display() to display name. Create a derived class
Employee from Person class having private members empid. Using
constructor initialize empid and have method display() to display empid.
Create another derivedclass HourlyEmployee from Employee with private
members hourlyRate and hoursWorked. Use constructor to initialize input
and methods getGrossPay() that computes and returns the gross pay of the
employee and display() to display the hourlyRate, hoursWorked and gross
pay. Create a driver class to test the functionalities of the above classes and
display output in the following format
Name : John Smith
EmpID : 7569
Hourly Rate : 100
Hours worked : 2000
Gross pay : 200000
class Person {
private String name;
Person(String name) {
[Link] = name;
}
void display() {
[Link]("Name : " + name);
}
}
class Employee extends Person {
private int empid;
Employee(String name, int empid) {
24BCSA92
super(name);
[Link] = empid;
}
void display() {
[Link]();
[Link]("EmpID : " + empid);
}
}
class HourlyEmployee extends Employee {
private int hourlyRate;
private int hoursWorked;
HourlyEmployee(String name, int empid, int hourlyRate, int hoursWorked) {
super(name, empid);
[Link] = hourlyRate;
[Link] = hoursWorked;
}
int getGrossPay() {
return hourlyRate * hoursWorked;
}
void display() {
[Link]();
[Link]("Hourly Rate : " + hourlyRate);
[Link]("Hours worked : " + hoursWorked);
[Link]("Gross pay : " + getGrossPay());
}
}
public class DriverPerson {
public static void main(String[] args) {
HourlyEmployee he = new HourlyEmployee("John Smith", 7569, 100, 2000);
[Link]();
}
}
OUTPUT:
Name : John Smith
EmpID : 7569
Hourly Rate : 100
Hours worked : 2000
Gross pay : 200000
NAME:-D ASHISH
SEC:-F1
ROLL NO.:-12
SIC:-24BCSA92
24BCSA92