Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Lab5 Oops

The document defines an abstract Worker class with name and salary rate as attributes. It has two subclasses - FullTimeWorker and HourlyWorker that override the abstract computePay() method to calculate pay based on hours worked and salary rate. The main method creates objects of both subclasses, gets user input for name and hours worked, and prints the computed pay.

Uploaded by

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

Lab5 Oops

The document defines an abstract Worker class with name and salary rate as attributes. It has two subclasses - FullTimeWorker and HourlyWorker that override the abstract computePay() method to calculate pay based on hours worked and salary rate. The main method creates objects of both subclasses, gets user input for name and hours worked, and prints the computed pay.

Uploaded by

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

Q1

class Circle { // Save as "Circle.java"

// Private variables

private double radius;

private String color;

// Constructors (overloaded)

public Circle() { // 1st Constructor

radius = 1.0;

color = "red";

public Circle(double r) { // 2nd Constructor

radius = r;

color = "red";

public Circle(double r, String c) { // 3rd Constructor

radius = r;

color = c;

// Public methods
public double getRadius() {

return radius;

public String getColor() {

return color;

public double getArea()

{ return

radius*radius*Math.PI;

//Define Cylinder class, which is a subclass of Circle

class Cylinder extends Circle {

private double height; // Private member variable

public Cylinder() { // constructor 1

super(); // invoke superclass' constructor Circle()

height = 1.0;

public Cylinder(double radius, double height) { // Constructor 2

super(radius); // invoke superclass' constructor Circle(radius)

this.height = height;

public double getHeight() {

return height;
}

public void setHeight(double

height) { this.height = height;

public double getVolume() {

return getArea()*height; // Use Circle's getArea()

public class TestCylinder {

public static void main(String[]

args) { Cylinder cy1 = new

Cylinder();

System.out.println("Radius is " + cy1.getRadius()

+ " Height is " + cy1.getHeight()

+ " Color is " + cy1.getColor()

+ " Base area is " + cy1.getArea()

+ " Volume is " +

cy1.getVolume()); Cylinder cy2

= new Cylinder(5.0, 2.0);

System.out.println("Radius is " + cy2.getRadius()

+ " Height is " + cy2.getHeight()

+ " Color is " + cy2.getColor()

+ " Base area is " + cy2.getArea()

+ " Volume is " + cy2.getVolume());

}
}
Q2

class A { public int a =100;}

class B extends A { public int a

=80; } class C extends B

{ public int a =60; } class D

extends C { public int a =40; }

class E extends D

public int a

=10; public

void show()

int a =0;

System.out.println(a);

System.out.println(this.

a);

System.out.println(new

A().a);

System.out.println(new

B().a);

System.out.println(new

C().a);

System.out.println(supe
r.a);

class Test
{
public static void main(String args[])
{

new E().show();

A a1 = new E();

D d1 = (D)

a1;

Q3

class OverloadConstructors {

public static void main(String

args[]) { Room a=new

Room(20,30,40);

Room b=new

Room(); Room
c=new Room(10);

double vol;

vol=a.volume();

System.out.println("Volume of room a is "

+vol);

vol=b.volume();

System.out.println("Volume of room b is "

+ vol); vol=c.volume();

System.out.println("Volume of room c is " + vol);

}
class Room {

double

length,breadth,height;

Room(){

bread

lengt

h=-1;

heigh

t=-1;

Room(double l,double b,double h

length=l;

breadth=b;

heig

ht=h;
}

Room(double len)

{ length=breadth=heig

ht=len;

public double volume() {

return

length*breadth*height;

Q4.

class OverloadConstructors {

public static void main(String

args[]) { Room a=new

Room(20,30,40);

Room b=new

Room(); Room

c=new Room(10);

double vol;
vol=a.volume();

System.out.println("Volume of room a is " +

vol); vol=b.volume();

System.out.println("Volume of room b is "

+ vol); vol=c.volume();

System.out.println("Volume of room c is " + vol);

}
class Room {

double

length,breadth,height;

Room(){

lengt

h=-1;

bread

th=-

1;

heigh

t=-1;

Room(double l,double b,double h) {

length=l;

brea

dth=

b;

heig

ht=h;
}

Room(double len)

{ length=breadth=heig

ht=len;

public double volume() {

return

length*breadth*height;

Q5

abstract class Shape {

private String

color;

public Shape (String

color) { this.color =

color;

}
public String toString() {

return "Shape of color=\"" + color + "\"";

}
abstract public double getArea();

class Rectangle extends Shape {


private int length;

private int

width;

public Rectangle(String color, int length, int width)

super(color);

this.length =

length;

this.width =

width;

}
public String toString()
{

return "Rectangle of length=" + length + " and width=" + width + ", subclass of" +

super.toString();

public double getArea()

return length*width;

}
class Triangle extends Shape {
private int

base;

private int

height;

public Triangle(String color, int base, int

height) { super(color);

this.base =
base;

this.height =

height;

}
public String toString() {

return "Triangle of base=" + base + " and height=" + height + ", subclass of "

+ super.toString();

}
public double getArea() { return 0.5*base*height; }

public class TestShape

public static void main(String[]

args) {

Shape s1 = new Rectangle("red", 4, 5);

System.out.println(s1);

System.out.println("Area is " + s1.getArea());

Shape s2 = new Triangle("blue", 4, 5);

System.out.println(s2); System.out.println("Area is " + s2.getArea());

Q6
public class Country
{
private String name;
private String capital;
private long population;
public Country(String name)

this.name = name;
}

public String getName(){


return name; }

public void setName(String name) {this.name =

name;} public String getCapital() {return capital;}

public void setCapital(String capital) {this.capital =

capital;} public long getPopulation() { return

population; }

public void setPopulation(long population) {this.population = population; }

public String toString()

{
return "Country [name="+name + "capital=" + capital + ",population=" + population + "]";
}

public static void main(String args[])

Country India = new Country("India");

India.setCapital("New Delhi");

India.setPopulation(1200000000);

System.out.println(India);

}
Q7.

abstract class Shape {

// Private member variable private String color;

// Constructor

public Shape (String color) { this.color = color;

public String toString() {

return "Shape of color=\"" + color + "\"";

// All Shape subclasses must implement a method called getArea() abstract public double getArea();

class Rectangle extends Shape

private int length; private int width;

public Rectangle(String color, int length, int width)

super(color); this.length = length; this.width = width;

public String toString()

{return "Rectangle of length = " + length + " and width = " + width + ", subclass of " +
super.toString();}

public double getArea()

{return length*width; }

class Triangle extends Shape


{

private int base; private int height;

public Triangle(String color, int base, int height)

super(color); this.base = base; this.height = height;

public String toString()

{return "Triangle of base = " + base + " and height = " + height + ", subclass of "+ super.toString();}

public double getArea()

{return 0.5*base*height;}

public class TestShape {

public static void main(String[] args) { Shape s1 = new Rectangle("red", 4, 5); System.out.println(s1);

System.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle("blue", 4, 5);


System.out.println(s2); System.out.println("Area is " + s2.getArea());

Q8.

import java.util.*;

abstract class Worker

String name; double salary_rate;


Worker(String n, double sr)

name=n; salary_rate=sr;

public String getName()

{return name;}

public double getSalary_Rate()

{return salary_rate;}

abstract public double computePay();

class FullTimeWorker extends Worker

int hours_Worked; FullTimeWorker(String n,double sr,int h)

super(n,sr); hours_Worked = h;

public double computePay()

if(hours_Worked<=240)

return (hours_Worked*salary_rate);

else

return 0.0;
}

class HourlyWorker extends Worker

int hours_Worked; HourlyWorker(String n,double sr,int h)

super(n,sr); hours_Worked = h;

public double computePay()


{

if(hours_Worked<=60)

return (hours_Worked*salary_rate); else

return 0.0;

public class TestWorker

public static void main(String[] args)

String name; double salary_rate; int hours_Worked;

Scanner sc = new Scanner(System.in); System.out.print("Enter Worker Name(Full Time): "); name =


sc.next();

System.out.print("Enter Number of Hours Worked: ");

hours_Worked
= sc.nextInt();
if(hours_Worke
d>240)
System.out.println("Input hours are greater
than 240"); else
{
FullTimeWorker sc1 = new
FullTimeWorker(name,200.0,hours_Worked);
System.out.println("Salary for work hours: "+sc1.computePay());

}
System.out.println();

System.out.print("Enter Worker Name(Hourly): "); name = sc.next();

System.out.print("Enter Number of Hours Worked: ");

hours_Worked = sc.nextInt();

if(hours_Worked>60)

System.out.println("Work past 60 hours? Without pay? Give this man a full time role"); else

{
HourlyWorker sc2 = new HourlyWorker(name,120.0,hours_Worked); System.out.println("Salary for
work hours: "+sc2.computePay());

You might also like