Topic - 3 E-Box
Topic - 3 E-Box
Topic - 3 E-Box
Single inheritance
Write a Java program to implement Single Inheritance.
[Note: Strictly adhere to the object oriented specifications given as a part of the problem
statement. Use the same class names and member variable names.
Follow the naming conventions mentioned for getters/setters]
Consider a class named Person with the following private data members.
Consider a class named Donor which extends Person class with the following private data
members.
Data Type Data Member
String bloodBankName
String donorType
String donationDate
Consider another class Main and write the main method to test the above class.
In the main( ) method, read the person and donor details from the user and call the
displayDonationDetails( ) method.
String bloodBankName=sc.next();
System.out.println("Enter Donor Type :");
String donorType=sc.next();
System.out.println("Enter Donation Date :");
String donationDate=sc.next();
Donor donor=new Donor();
donor.setName(name);
donor.setDateOfBirth(dateOfBirth);
donor.setGender(gender);
donor.setMobileNumber(mobileNumber);
donor.setBloodGroup(bloodGroup);
donor.setBloodBankName(bloodBankName);
donor.setDonorType(donorType);
donor.setDonationDate(donationDate);
donor.displayDonationDetails();
}
}
class Donor extends Person {
private String bloodBankName;
private String donorType;
private String donationDate;
public String getBloodBankName() {
return bloodBankName;
}
public void setBloodBankName(String bloodBankName) {
this.bloodBankName = bloodBankName;
}
public String getDonorType() {
return donorType;
}
public void setDonorType(String donorType) {
this.donorType = donorType;
}
public String getDonationDate() {
return donationDate;
}
public void setDonationDate(String donationDate) {
this.donationDate = donationDate;
}
public void displayDonationDetails(){
System.out.println("Donation Details :");
System.out.println("Name : " +getName());
System.out.println("Date Of Birth : " +getDateOfBirth());
System.out.println("Gender : " +getGender());
System.out.println("Mobile Number : " +getMobileNumber());
System.out.println("Blood Group : " +getBloodGroup());
System.out.println("Blood Bank Name : " +getBloodBankName());
System.out.println("Donor Type : " +getDonorType());
System.out.println("Donation Date : " +getDonationDate());
}
}
class Person {
private String name;
private String dateOfBirth;
private String gender;
private String mobileNumber;
private String bloodGroup;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
ABC Bank announced a new scheme of reward points for a transaction using an ATM card.
Each transaction using the normal card will be provided 1% of the transaction amount as a
reward point. If a transaction is made using a premium card and it is for fuel expenses,
additional 10 points will be rewarded. Write a java program to calculate the total reward points.
Consider a class HPVISACard which extends VISACard class and overrides the following
Method Description
public Double computeRewardPoints(String type, Double amount) In this method, calculate the reward points from the base
method.
Hint:
Consider the Main class with the main method and read all the transaction details in the main
method.
Card type will be either ‘VISA card’ or ‘HPVISA card’. Otherwise, display ‘Invalid data’
Calculate the reward points corresponding to the card type and transaction type and print the
reward points(upto two decimal places).
All text in bold corresponds to the input and the rest corresponds to output.
if(type.equalsIgnoreCase("fuel")){
rewardPoints +=10;
return rewardPoints;
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
VISACard visaCard;
String contd;
do{
System.out.println("Enter the transaction detail");
String input=sc.next();
String[] inputs=input.split(",");
if(inputs[2].equals("VISA card")){
visaCard=new VISACard();
double reward=visaCard.computeRewardPoints(inputs[0],Double.parseDoubl
e(inputs[1]));
}
while(contd.equalsIgnoreCase("Yes"));
}
}
return rewardPoints;
}
GST Calculation
Write a program to calculate the total amount with GST for the events. The Events are
Stage show and Exhibition. For the Stage show, GST will be 15% and for exhibition,
GST will be 5%.
[Note: Strictly adhere to the object oriented specifications given as a part of the
problem statement.
Follow the naming conventions as mentioned]
Consider class Exhibition which extends the Event class with the following private
attributes/variables.
Data Type Variable
static Integer gst = 5
Integer noOfStalls
Consider class StageEvent which extends the Event class with the following private
attributes/variables.
Data Type Variable
static Integer gst = 15
Integer noOfSeats
Override toString() method in all classes to display the event details in the format
specified in sample input and output.
All text in bold corresponds to the input and the rest corresponds to output.
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in).useDelimiter("\n");
System.out.println("Enter event name");
String name=sc.next();
System.out.println("Enter the cost per day");
double costPerDay=sc.nextDouble();
System.out.println("Enter the number of days");
int noOfDays=sc.nextInt();
System.out.println("Enter the type of event\n1.Exhibition\n2.Stage Event"
);
int type=sc.nextInt();
Event event;
if(type==1){
import java.text.DecimalFormat;
return amt1;
}
public String toString(){
return "Event Details\n"+"Name:"+name+"\n"+"Type:"+type+"\n"+"Number of s
talls:"+noOfStalls+"\n"+"Total amount:"+df.format(totalCost());
}
import java.text.DecimalFormat;
}
public double totalCost(){
amt = costPerDay*noOfDays*1.15;
return amt;
}
public void display(){
System.out.println("Event Details");
System.out.println("Name:"+name);
System.out.println("Type:"+type);
System.out.println("Number of seats:"+noOfSeats);
System.out.println("Total amount:"+amt);
}
public String toString(){
return "Event Details\n"+"Name:"+name+"\n"+"Type:"+type+"\n"+"Number of s
eats:"+getNoOfSeats()+"\n"+"Total amount:"+df.format(totalCost());
}
Abstract Class
Write a program to calculate total cost of the event based on the type of event and display
details using Abstract class and method.
Prototype for the parametrized constructor, Event(String name, String detail, String
type, String organiser)
Include appropriate getters and setters
Consider a class named Exhibition which extends Event class with the following private
attributes
Attributes Datatype
noOfStalls Integer
rentPerStall Double
Consider a class named StageEvent which extends Event class with the following
private attributes.
Attribute Datatype
noOfShows Integer
costPerShow Double
Consider a driver class called Main. In the main method, obtain input from the user and
create objects accordingly.
Input format:
Input format for Exhibition is in the CSV format
(name,detail,type,organiser,noOfStalls,rentPerStall)
Input format for StageEvent is in the CSV
format (name,detail,type,organiser,noOfShows,costPerShow)
Output format:
Print "Invalid choice" if the input is invalid to our application and terminate.
Display one digit after the decimal point for Double datatype.
Refer to sample Input and Output for formatting specifications.
[All text in bold corresponds to the input and rest corresponds to output]
import java.util.Scanner;
Event event=null;
if(Integer.parseInt(eventType.trim())==1){
System.out.println("Enter the details in CSV format");
String input=sc.next();
String[] inputs=input.split(",");
event=new Exhibition(inputs[0].trim(), inputs[1].trim(), inputs[2].tr
im(), inputs[3].trim(), Integer.parseInt(inputs[4].trim()), Double.parseDouble(in
puts[5].trim()));
System.out.println("Exhibition Details");
System.out.println("Event Name:"+event.name);
System.out.println("Detail:"+event.detail);
System.out.println("Type:"+event.type);
System.out.println("Organiser Name:"+event.organiser);
System.out.println("Total Cost:"+String.format("%.1f", event.calculat
eAmount()));
}
else if(Integer.parseInt(eventType.trim())==2){
System.out.println("Enter the details in CSV format");
String input=sc.next();
String[] inputs=input.split(",");
event=new StageEvent(inputs[0].trim(), inputs[1].trim(), inputs[2].tr
im(), inputs[3].trim(), Integer.parseInt(inputs[4].trim()), Double.parseDouble(in
puts[5].trim()));
System.out.println("Stage Event Details");
System.out.println("Event Name:"+event.name);
System.out.println("Detail:"+event.detail);
System.out.println("Type:"+event.type);
System.out.println("Organiser Name:"+event.organiser);
System.out.println("Total Cost:"+String.format("%.1f", event.calculat
eAmount()));
}
else{
System.out.println("Invalid choice");
}
}
}
}
public Integer getNoOfStalls() {
return noOfStalls;
}
public void setNoOfStalls(Integer noOfStalls) {
this.noOfStalls = noOfStalls;
}
public Double getRentPerStall() {
return rentPerStall;
}
public void setRentPerStall(Double rentPerStall) {
this.rentPerStall = rentPerStall;
}
Double calculateAmount() {
double a=getRentPerStall();
double b=getNoOfStalls();
double c=a*b;
return c;
}
Double calculateAmount() {
double z=getNoOfShows();
double y=getCostPerShow();
double x=z*y;
return x;
Overriding
Overriding is a runtime polymorphism. The inherited class has the overridden method
which has the same name as the method in the parent class. The argument number,
types, or return types should not differ in any case. The method is invoked with the
object of the specific class ( but with the reference of the parent class).
Write a program to calculate projected revenue for exhibition and stage event using
inheritance and method overriding
[Note :
Strictly adhere to the object-oriented specifications given as a part of the problem
statement.
Use the same class names and member variable names. ]
Consider a parent class Event and define the following protected attributes,
Attributes Datatype
name String
detail String
ownerName String
Declare the abstract method public abstract Double projectedRevenue() in the Event
class
Consider a child class Exhibition that extends Event that defines with the
following attribute,
Attributes Datatype
noOfStalls Integer
Include appropriate getters and setters.
Prototype for the parameterized constructors to the Exhibition class in the following
order Exhibition(String name, String detail, String ownerName, Integer
noOfStalls). Use super( ) to call and assign values in the base class constructor.
Implement the abstract method projectedRevenue() in Exhibition class
MethodName Description
public Double projectedRevenue() Calculate revenue and return the double value. Each stall will prod
Consider another child class StageEvent that extends Event that defines with the
following attribute,
Attributes Datatype
noOfShows Integer
noOfSeatsPerShow Integer
Include appropriate getters and setters.
Prototype for the parameterized constructors to the StageEvent class in the following
order StageEvent(String name, String detail, String ownerName, Integer
noOfShows, Integer noOfSeatsPerShow). Use super( ) to call and assign values in
the base class constructor.
Implement the abstract method projectedRevenue() in StageEvent class
MethodName Description
public Double projectedRevenue() Calculate revenue and return the double value. Each seat produc
Consider the class Main. It includes the method main. In the main( ) method the event
details are read from the user and the methods of the above classes are called
Refer to sample input/output for other further details and format of the output.
The double values should be formatted to 1 decimal place .
[All Texts in bold corresponds to the input and rest are output]
Sample Input/Output 1:
Sample Input/Output 2:
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the event:");
String name=sc.nextLine();
System.out.println("Enter the detail of the event:");
String detail=sc.nextLine();
System.out.println("Enter the owner name of the event:");
String owner=sc.nextLine();
System.out.println("Enter the type of the event:\n1.Exhibition\n2.StageE
vent");
int n=sc.nextInt();
switch (n)
{
case 1:
System.out.println("Enter the number of stalls:");
int stall=sc.nextInt();
Event e=new Exhibition(name,detail,owner,stall);
System.out.println("The projected revenue of the event is "+e.pro
jectedRevenue());
break;
case 2:
System.out.println("Enter the number of shows:");
int show=sc.nextInt();
System.out.println("Enter the number of seats per show:");
int seat=sc.nextInt();
Event s=new StageEvent(name,detail,owner,show,seat);
System.out.println("The projected revenue of the event is "+s.pro
jectedRevenue());
break;
}
}
}
}
public Integer getNoOfShows(){
return noOfShows;
}
public void setNoOfShows(Integer noOfShows) {
this.noOfShows = noOfShows;
}
public Integer getNoOfSeatsPerShow() {
return noOfSeatsPerShow;
}
public void setNoOfSeatsPerShow(Integer noOfSeatsPerShow) {
this.noOfSeatsPerShow = noOfSeatsPerShow;
}
double z=getNoOfShows();
double y=getNoOfSeatsPerShow();
double x=50.0;
double w=z*y;
double v=w*x;
return v;