JAVA LAB Assignment 8 by Swapnendu Pal
JAVA LAB Assignment 8 by Swapnendu Pal
ASSIGNMENT : 8
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;
double volume() {
return length * breadth * height;
}
double wholeSurfaceArea() {
return 2 * ((length * breadth) + (length * height) + (breadth * height));
}
}
double volume() {
return length * length * length;
}
double wholeSurfaceArea() {
return 6 * (length * length);
}
}
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)));
}
}
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!!!");
}
}
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.
class Num {
int num;
Num(int n) {
num = n;
}
void showNum() {
System.out.println("The number is = " + 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.
class Num2 {
int num;
Num2(int n) {
num = n;
}
void showNum() {
System.out.println("The number is = " + 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.
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.");
}
}
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");
}
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_
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);
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_