Question 2. In below program of inheritance, class Bicycle is a base class, class MountainBike is a derived class which extends Bicycle class and class Test is a driver class to run program. Import java.util.*; // base class class Bicycle { // the Bicycle class has two fields public int gear; public int speed; // the Bicycle class has one constructor public Bicycle(int gear, int speed) { this.gear = gear;
Question 2. In below program of inheritance, class Bicycle is a base class, class MountainBike is a derived class which extends Bicycle class and class Test is a driver class to run program.
Import java.util.*;
// base class
class Bicycle {
// the Bicycle class has two fields
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int gear, int speed)
{
this.gear = gear;
this.speed = speed;
}
// the Bicycle class has three methods
public void applyBrake(int decrement)
{
speed -= decrement;
}
public void speedUp(int increment)
{
speed += increment;
}
// toString() method to print info of Bicycle
public String toString()
{
return ("No of gears are " + gear + "\n" + "speed of bicycle is " + speed);
}
}
// Create a MountainBike derived class which extends Bicycle class
//create seatHeight variable (int type)
// create constructor which holds 3 values (gear,speed, seatHeight)
// create setHeight method which holds 1 value (seatHeight)
// overriding toString() method of Bicycle to print info of gear,speed and seatHeight
// driver class
public class Test {
public static void main(String args[])
{
MountainBike mb = new MountainBike(3, 100, 25);
System.out.println(mb.toString());
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
Question 3. In below program of inheritance, class Book is a base class, class EBook is a derived class which extends Book class and class TestBook is a driver class to run program.
public class Book
{
public String author;
public float costPrice;
public float salePrice;
public String title;
public int pages;
public Book(String au, float cp, float sp, String tl, int pg){
author = au;
costPrice = cp;
salePrice = sp;
title = tl;
pages = pg;
}
//Compute net profit per book
public float netProfit()
{
return (salePrice - costPrice);
}
//Compute tax on net profit per book @ 30%
public float getTax()
{
return (0.3f*this.netProfit());
} }
// Create an EBook derived class which extends Book class
// Create downloadSite (String type) and sizeMB(int type) variables
//Create a constructor which takes parent class constructor values along with downloadSite and sizeMB values ( total 7 arguments)
// write pageSize() method which returns a float value. ( per page size is calculated as sizeMB/pages)
//write printTitle() method which prints title of book
// write getTax () method which returns float value. (Compute tax on net profit per book @ 30% + A fixed $2 ebook tax )
public class TestBook
{
public static void main(String[] args){
//Create a Book object
Book book1 = new Book("Gary Rich", 8.99f, 19.99f, "How to get rich", 100);
//Fetch Book Class specific fields from this book
System.out.println("\n1) Book Details\n 1a) Author: " + book1.author + " | cost price: "+ book1.costPrice +" | sale price:" + book1.salePrice +" | pages: "+ book1.pages +" | title: " + book1.title+"\n");
//Get Profit and Tax
System.out.println(" 1a) Author " + book1.author + " nets a per-book profit of $" + book1.netProfit() + " and a tax of $" + book1.getTax());
//Create an e-book object
EBook book2 = new EBook("Seth Devilin", 10f, 23f, "Bad habits that lead to success", 200, "Amazon", 80);
//Fetch Book Class specific fields from this ebook
System.out.println("\n2) EBook Details\n 2a) Author: " + book2.author + " | cost price: "+ book2.costPrice +" | sale price:" + book2.salePrice +" | pages: "+ book2.pages +" | title: " + book2.title+"\n");
//Get Profit and Tax
System.out.println(" 2b) Author " + book2.author + " nets a per-book profit of $" + book2.netProfit() + " and a tax of $" + book2.getTax());
//Get per page size
System.out.println("\n 2c) Book \"" + book2.title + "\" has a per-page size of " + book2.pageSize() +" MB\n");
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images