Professional Documents
Culture Documents
Varshini M
Varshini M
Varshini M
UEN No : - 2022UG000062
1. Create a general class ThreeDObject and derive the classes Box, Cube, Cylinder and Cone
from it. The class ThreeDObject has methods wholeSurfaceArea ( ) and volume ( ). Override
these two methods in each of the derived classes to calculate the volume and whole surface
area of each type of three-dimensional object. The dimensions of the objects are to be taken
from the users and passed through the respective constructors of each derived class. Write a
main method to test these classes.
import java.util.Scanner;
@Override
double volume() {
return length * width * height;
}
@Override
double wholeSurfaceArea() {
return 2 * (length * width + length * height + width * height);
}
}
Cube(double s) {
side = s;
}
@Override
double volume() {
return side * side * side;
}
@Override
double wholeSurfaceArea() {
return 6 * side * side;
}
}
Cylinder(double r, double h) {
radius = r;
height = h;
}
@Override
double volume() {
return Math.PI * radius * radius * height;
}
@Override
double wholeSurfaceArea() {
return 2 * Math.PI * radius * (radius + height);
}
}
Cone(double r, double h) {
radius = r;
height = h;
}
@Override
double volume() {
return (Math.PI * radius * radius * height) / 3;
}
@Override
double wholeSurfaceArea() {
return Math.PI * radius * (radius + Math.sqrt(radius * radius + height * height));
}
}
scanner.close();
}
}
import java.util.ArrayList;
class Staff {
private String name;
private String address;
private String phoneNumber;
public Teacher(String name, String address, String phoneNumber, String qualification, String
subject) {
super(name, address, phoneNumber);
this.qualification = qualification;
this.subject = subject;
}
public CasualTypist(String name, String address, String phoneNumber, int speed, double
wage) {
super(name, address, phoneNumber, speed);
this.wage = wage;
}
System.out.println("\nOfficer Information:");
System.out.println("Name: " + officer.getName());
System.out.println("Address: " + officer.getAddress());
System.out.println("Phone Number: " + officer.getPhoneNumber());
System.out.println("Specialization: " + officer.getSpecialization());
3. Create a base class Distance which stores the distance between two locations in miles and
a method travelTime(). The method prints the time taken to cover the distance when the
speed is 60 miles per hour. Now in a derived class DistanceMKS, override travelTime() so
that it prints the time assuming the distance is in kilometers and the speed is 100 km per
second.
class Distance {
private double distance;
private double speed;
public Distance(double distance, double speed) {
this.distance = distance;
this.speed = speed;
}
@Override
public double timeTaken() {
// Convert distance from kilometers to miles for calculation
double distanceInMiles = getDistance() * 0.621371;
return distanceInMiles / getSpeed();
}
@Override
public void displayTimeTaken() {
System.out.println("Time taken to cover " + getDistance() + " kilometers: " + timeTaken() +
" hours");
}
}
// Distance in kilometers
DistanceMKS distanceInKilometers = new DistanceMKS(160.9344, 96.5606); // 100
kilometers at 60 mph (converted)
distanceInKilometers.displayTimeTaken();
}
}
4. Create a class called Time, which has three private instance variables – hour, min and
sec. It contains a method called add( ) which takes one Time object as parameter and print
the added value of the calling Time object and passes Time object. In the main method,
declare two Time objects and assign values using constructor and call the add() method.
class Time {
private int hours;
private int minutes;
private int seconds;
System.out.println("Time 1:");
time1.displayTime();
System.out.println("Time 2:");
time2.displayTime();
time1.add(time2);
@Override
public void displayDetails() {
System.out.println("Stock Name: " + getName());
System.out.println("Stock Symbol: " + symbol);
System.out.println("Number of Shares: " + shares);
System.out.println("Stock Value: $" + getValue());
}
}
@Override
public void displayDetails() {
System.out.println("Bond Name: " + getName());
System.out.println("Bond Issuer: " + issuer);
System.out.println("Maturity Years: " + maturityYears);
System.out.println("Bond Value: $" + getValue());
}
}
@Override
public void displayDetails() {
System.out.println("Savings Account Name: " + getName());
System.out.println("Interest Rate: " + interestRate + "%");
System.out.println("Savings Account Value: $" + getValue());
}
}
System.out.println("Stock Details:");
stock.displayDetails();
System.out.println();
System.out.println("Bond Details:");
bond.displayDetails();
System.out.println();