0% found this document useful (0 votes)
112 views9 pages

JAVA LAB Assignment 8 by Swapnendu Pal

The document contains examples of method overriding and inheritance in Java. It defines base classes like Vehicle, ThreeDObject, Num and subclasses that inherit from the base classes and override methods. Main methods are included to test the classes by creating objects of the subclasses.

Uploaded by

babinrick23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views9 pages

JAVA LAB Assignment 8 by Swapnendu Pal

The document contains examples of method overriding and inheritance in Java. It defines base classes like Vehicle, ThreeDObject, Num and subclasses that inherit from the base classes and override methods. Main methods are included to test the classes by creating objects of the subclasses.

Uploaded by

babinrick23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name : Swapnendu Pal

Stream : BCA (3rd Semester)


University Roll : 31001220008
College Roll : 36

ASSIGNMENT : 8

1.Write a Java program to show method overriding.

class A{
int i,j;
A(int a, int b){
i=a;
j=b;
}
void show(){
System.out.println(" i and j : "+i+" "+j);
}
}
class B extends A{
int k;
B(int a, int b, int c){
super(a,b);
k=c;
}
void show(){
System.out.println(" k : "+k);
}
}
public class Override{
public static void main(String args[]){
B subOb = new B(5,6,7);
subOb.show();
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_

2.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 objects. 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;

abstract class ThreeDObject {


double length, breadth, height, radius;
final double pi = 3.14;

ThreeDObject(double l, double b, double h) { //for box1


length = l;
breadth = b;
height = h;
}

ThreeDObject(double l) { //for cube


length = l;

ThreeDObject(double h, double r) { //for cylinder & cone


height = h;
radius = r;
}

abstract double wholeSurfaceArea();

abstract double volume();


}

class box1 extends ThreeDObject {


box1(double l, double b, double h) {
super(l, b, h);
}

double volume() {
return length * breadth * height;
}

double wholeSurfaceArea() {
return 2 * ((length * breadth) + (length * height) + (breadth * height));
}
}

class cube extends ThreeDObject {


cube(double length) {
super(length);
}

double volume() {
return length * length * length;
}

double wholeSurfaceArea() {
return 6 * (length * length);
}
}

class cylinder extends ThreeDObject {

cylinder(double h, double r) {
super(h, r);
}

double volume() {
return pi * (radius * radius) * height;
}

double wholeSurfaceArea() {
return 2 * pi * radius * (height + radius);
}
}
class cone extends ThreeDObject {
cone(double h, double r) {
super(h, r);
}

double volume() {
return pi * (radius * radius) * (height / 3.0);
}

double wholeSurfaceArea() {
return pi * radius * (radius + Math.sqrt((height * height) + (radius *
radius)));
}
}

public class Abstraction {


public static void main(String[] args) {

Scanner s = new Scanner(System.in);


System.out.print("Enter the length : ");
double length = s.nextDouble();
System.out.print("Enter the breadth : ");
double breadth = s.nextDouble();
System.out.print("Enter the height : ");
double height = s.nextDouble();
System.out.print("Enter the radius : ");
double radius = s.nextDouble();

box1 box1 = new box1(length, breadth, height);


cube cube = new cube(length);
cylinder cylinder = new cylinder(height, radius);
cone cone = new cone(height, radius);
ThreeDObject obj;
//for box1
obj = box1;
System.out.println("Volume of box1: " + String.format("%.2f", obj.volume())
+ "\nWhole Surface Area of box1: " + String.format("%.2f",
obj.wholeSurfaceArea()));
//for cube
obj = cube;
System.out.println("Volume of cube: " + String.format("%.2f", obj.volume())
+ "\nWhole Surface Area of cube: " + String.format("%.2f",
obj.wholeSurfaceArea()));
//for cylinder
obj = cylinder;
System.out.println("Volume of cylinder: " + String.format("%.2f",
obj.volume()) + "\nWhole Surface Area of cylinder: " + String.format("%.2f",
obj.wholeSurfaceArea()));
//for cone
obj = cone;
System.out.println("Volume of cone " + String.format("%.2f", obj.volume())
+ "\nWhole Surface Area of cone: " + String.format("%.2f",
obj.wholeSurfaceArea()));
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_
3.Write a program to create a class names Vehicle having protected instance
variables regnNumber, speed, color, ownerName and a method showData() to show “This
is a vehicle class”. Inherit the Vehicle into subclasses named Bus and Car having
individual private instance variables routeNumber in Bus and manufacturerName in
Car and both of them having showData() method showing all details of Bus and Car
respectively with content of super class’s showData() method.

public class Vehicle_demo {


public static void main(String[] args) {
Bus objBus = new Bus(20211231, 55, "Blue", "Legend Killer", "21/1");
objBus.showData();
System.out.println();
Car objCar = new Car(20220807, 95, "Yellow", "Swapnendu Pal", "XUV");
objCar.showData();
}
}

class Vehicle {
protected
long regnNumber;
double speed;
String color;
String ownerName;
Vehicle(long a, double b, String c, String d) {
regnNumber = a;
speed = b;
color = c;
ownerName = d;
}
void showData() {
System.out.println("This is a vehicle class!!!");
}
}

class Bus extends Vehicle {


private String routeNumber;
Bus(long a, double b, String c, String d, String e) {
super(a, b, c, d);
routeNumber = e;
}
void showData() {
super.showData();
System.out.println("This is BUS Class!!");
System.out.println("Registration Number : " + regnNumber);
System.out.println("Speed : " + speed + " km/h");
System.out.println("Color : " + color);
System.out.println("Owner Name : " + ownerName);
System.out.println("Route Number : " + routeNumber);
}
}

class Car extends Vehicle {


private String manufacturerName;
Car(long a, double b, String c, String d, String e) {
super(a, b, c, d);
manufacturerName = e;
}
void showData() {
super.showData();
System.out.println("This is CAR Class!!");
System.out.println("Registration Number : " + regnNumber);
System.out.println("Speed : " + speed + " km/h");
System.out.println("Color : " + color);
System.out.println("Owner Name : " + ownerName);
System.out.println("Manufacturer Name : " + manufacturerName);
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_

4.Write a Java program which creates a base class Num and contains an integer
number along with a method shownum() which displays the number. Now create a
derived class HexNum which inherits Num and overrides shownum() which displays the
hexadecimal value of the number. Demonstrate the working of the classes.

public class Num_demo_10 {


public static void main(String[] args) {
Num objNum = new Num(10);
HexNum objHexNum = new HexNum(289);
objNum.showNum();;
objHexNum.showNum();
}
}

class Num {
int num;
Num(int n) {
num = n;
}
void showNum() {
System.out.println("The number is = " + num);
}
}

class HexNum extends Num {


HexNum(int n) {
super(n);
}
void showNum() {
System.out.println("The Hexadecimal of " + num + " is = " +
Integer.toHexString(num));
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_

5.Write a Java program which creates a base class Num and contains an integer
number along with a method shownum() which displays the number. Now create a
derived class OctNum which inherits Num and overrides shownum() which displays the
octal value of the number. Demonstrate the working of the classes.

public class Num_demo_11 {


public static void main(String[] args) {
Num2 objNum2 = new Num2(10);
OctNum objOctNum = new OctNum(246);
objNum2.showNum();;
objOctNum.showNum();
}
}

class Num2 {
int num;
Num2(int n) {
num = n;
}
void showNum() {
System.out.println("The number is = " + num);
}
}

class OctNum extends Num {


OctNum(int n) {
super(n);
}
void showNum() {
System.out.println("The Octal of " + num + " is = " +
Integer.toOctalString(num));
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_

6.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. Demonstrate the working of the
classes.

public class Distance_demo {


public static void main(String[] args) {
Distance objDistance = new Distance(150);
DistanceMKS objDistanceMKS = new DistanceMKS(69.08);
objDistance.travelTime();
objDistanceMKS.travelTime();
}
}

class Distance {
double dist;
Distance(double a) {
dist = a;
}
void travelTime() {
System.out.println("Time taken to cover " + dist + " miles when speed is 60
miles/hour is " + (dist/60) + " hours.");
}
}

class DistanceMKS extends Distance {


double distk;
DistanceMKS(double a) {
super(a);
distk = (a*1.609);
}
void travelTime() {
System.out.println("Time taken to cover " + distk + " kilometers when speed
is 100 kilometers/second is " + (dist/100) + " seconds.");
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_

7.Create a base class called "vehicle" that stores number of wheels and speed.
Create the following derived classes –
"car" that inherits "vehicle" and also stores number of passengers.
"truck" that inherits "vehicle" and also stores the load limit.
Write a main function to create objects of these two derived classes and display
all the information about "car" and "truck". Also compare the speed of these two
vehicles - car and truck and display which one is faster.

class vehicle{
int wheel;
double speed;
vehicle(int wh,double sp){
wheel=wh;
speed=sp;
}
void show(){
System.out.println("Record");
}
}
class car extends vehicle{
int seat;
car(int wh,double sp,int s){
super(wh,sp);
seat=s;
}
void show(){
System.out.println("Number of Wheel of the Car: "+wheel);
System.out.println("Max Speed of the Car: "+speed+" km/h");
System.out.println("Number of Passengers of the Car: "+seat);
}
}
class truck extends vehicle{
double load;
truck(int wh,double sp,double l){
super(wh,sp);
load=l;
}
void show(){
System.out.println("Number of Wheel of the Truck: "+wheel);
System.out.println("Max Speed of the Truck: "+speed+" km/h");
System.out.println("Loading Capacity of the Truck: "+load+" kg");
}
}
public class Speed{
public static void main(String[] args) {
System.out.println("\n-----Car Details-----");
car c=new car(4,170,7);
c.show();
System.out.println("\n-----Truck Details-----");
truck tr=new truck(16,100,3000);
tr.show();
if(tr.speed>c.speed){
System.out.println("\nTruck is Faster "+tr.speed+" km/h");
}
else{
System.out.println("\nCar is Faster "+c.speed+" km/h");
}
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_

8.Write a Java program to explain "multilevel inheritance."

class Box{
private double width;
private double height;
private double depth;
Box(Box ob){
width=ob.width;
height=ob.height;
depth=ob.depth;
}
Box(double w,double h,double d){
width=w;
height=h;
depth=d;
}
Box(){
width=-1;
height=-1;
depth=-1;
}
Box(double len){
width=height=depth=len;
}
double volume(){
return width*height*depth;
}
}
class BoxWeight extends Box{
double weight;
BoxWeight(double w, double h, double d, double m){
super(w,h,d);
weight=m;
}
BoxWeight(){
super();
weight=-1;
}
BoxWeight(double len, double m){
super(len);
weight=m;
}
}
class Shipment extends BoxWeight{
double cost;
Shipment(double w, double h, double d, double m, double c){
super(w,h,d,m);
cost=c;
}
Shipment(){
super();
cost=-1;
}
Shipment(double len, double m, double c){
super(len,m);
cost=c;
}
}
public class Multi_Hi{
public static void main(String args[]){
Shipment shipment1=new Shipment(10,20,15,10,3.41);
Shipment shipment2=new Shipment(2,3,4,0.76,1.28);
double vol;
vol=shipment1.volume();
System.out.println("Volume of shipment1 is "+vol);
System.out.println("Volume of shipment1 is "+shipment1.weight);
System.out.println("Shipping cost: $"+shipment1.cost);
System.out.println();
vol=shipment2.volume();
System.out.println("Volume of shipment2 is "+vol);
System.out.println("Volume of shipment2 is "+shipment2.weight);
System.out.println("Shipping cost: $"+shipment2.cost);
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_

You might also like