Ganpat University: Akshi Vasaniya

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Akshi Vasaniya

Ganpat University
(Practical-7)

1. Create a class Room which has two instance variables


length and breadth and a method called area() to find area
of Room. Create another class BedRoom which inherit
Room class and has an instance variable height and a
method volume() to find volume in BedRoom. Use
necessary constructors in both class. A main() method
resides in another class.
Code –

public class Practical7 {

public static void main(String[] args) {

Room r = new Room(5, 3);


r.area();

BedRoom b = new BedRoom(5, 2.5);


b.volume(3.6);

}
}

class Room{
double length, breadth;

public Room(double l, double b) {


this.length = l;
this.breadth = b;
}
void area() {
System.out.println("Area of room = "+ length*breadth);
}
}

class BedRoom extends Room{


double length, breadth, height;

public BedRoom(double l, double b) {


super(l, b);
this.length = l;
this.breadth = b;
}

void volume(double h) {
System.out.println("Volume of BedRoom = "+length*breadth*h);
}
}

Output:

2. A super class Worker has been defined to store the details


of a worker. Define a subclass Wages to compute the
monthly wages for the worker. The details of both the
classes are given below:
Class name : Worker
Data members :
Name : to store the name of the worker
Basic : to store the basic pay in decimal

Member Methods:
Worker(....) : parameterized constructor to assign values to
the instance variables
void display() : display worker details
class name : Wages

Data members
hrs : stores the hours worked
rate : stores rate per hour
wage : stores the overall wage of the worker

Member Methods:
Wages(....) : parameterized constructor to assign values to
the instance variables of both classes
double overtime( ) : calculates and returns the overtime
amount as (hours * rate )
void display() : calculates the wage using the formula
wage=overtime amount +basic pay and displays it along
with other details .
Specify the class Worker giving details of the constructor()
and void display(). Using the concept of inheritance,
specify the class Wages giving details of the constructor(),
double overtime() and void display().
Code –
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the worker: ");
String n = sc.nextLine();
System.out.print("Enter the basic pay of the worker: ");
double p = sc.nextDouble();
System.out.print("Enter total hours/day worked by the worker:
");
int h = sc.nextInt();
System.out.print("Enter rate/hr given by company: ");
Double r = sc.nextDouble();

Wages w1 = new Wages(n, p, h, r);


System.out.println();
System.out.println("Worker Details - ");
w1.display();
}
}
class Worker{
String name;
double basic;

public Worker(String s, double x){


this.name = s;
this.basic = x;
}

void display(){
System.out.println("Name of worker: "+name);
System.out.println("Basic pay of worker: "+basic);
}
}

class Wages extends Worker{

int hr;
double rate, wage;

public Wages(String s, double x, int h, double r) {


super(s, x);
this.hr = h;
this.rate = r;
this.wage = 0;
}
double overTime(){
return (rate*hr);
}

void display(){
double o = overTime();
wage = o + super.basic;
super.display();
System.out.println("Total hours worker by the worker: "+hr);
System.out.println("OverTime amount of the worker: "+rate);
System.out.println("Wage = "+wage);
}
}

Output –
3. Create abstract class called Shape which has three sub
classes say Triangle, Rectangle, and Circle. Define one
method area() in abstract class and override this area() in
these three subclasses to calculate area for specific object
i.e. area() of Triangle subclass should calculate area of
triangle. Same for rectangle, circle.
Code –
package anotherVideoOfAnujBhaiya;

public class Demo {

public static void main(String[] args) {

Triangle triangle = new Triangle();


triangle.area();
Rectangle rectangle = new Rectangle();
rectangle.area();
Circle circle = new Circle();
circle.area();

abstract class shape{

public abstract void area();

class Triangle extends shape{

@Override
public void area() {
System.out.println("Area of triangle = (l * b)/2");
}
}

class Rectangle extends shape{

@Override
public void area() {
System.out.println("Area of rectangle = L * W");

}
}

class Circle extends shape{

@Override
public void area() {
System.out.println("Area of circle = 3.14*r*r");

}
}

Output –

4. Write a program to implement Multiple inheritance using interface.


Code –
public class Inheritances implements Demo1, Demo2, Demo3 {

public static void main(String[] args) {

Inheritances in = new Inheritances();


in.demo();

@Override
public void demo() {
System.out.println("Method of Demo Class");

interface Demo1{

void demo();

interface Demo2{

interface Demo3{

Output –
5. Write a program to create interface shape containing area() method. Now
create Rectangle, Triangle, Sphere class that implements shape interface
and override area() to calculate area of rectangle ,triangle and sphere.

Code –
public interface Shape {

void area();

class Rectangle implements Shape{


@Override
public void area() {

System.out.println("Area of rectangle = L * W");

}
}

class Triangle implements Shape{


@Override
public void area() {
System.out.println("Area of triangle = (l * b)/2");

}
}

class Sphere implements Shape{


@Override
public void area() {
System.out.println("Area of sphere = 4 * 3.14 * r * r");
}
}

6. Write a program to implement following scenario.

Code –
public class E extends D implements C{

public static void main(String[] args) {

interface A{

}
interface B{

}
interface C extends A, B{

class D{

You might also like