Write an abstract super class encapsulating a vacation: A vacation has two attri
ID: 3563716 • Letter: W
Question
Write an abstract super class encapsulating a vacation: A vacation has two attributes: a budget and a destination. It has an abstract method returning how much the vacation is over or under budget. This class has two non-abstract subclasses: one encapsulating an all-inclusive vacation, and the other encapsulating a vacation bought piece-meal. An all-inclusive vacation has three additional attributes: a brand (for instance ClubMed); a rating, expressed as a number of stars; and a price. A piecemeal vacation has two additional attributes: a set of items (hotel, meal, airfare), and a set of corresponding costs. You also need to include a client class to test these two classes using either an ArrayList or an array of objects to populate the array, print the state of each element and the total costs for all vacations. Also include and test an abstract calculate cost method that returns the total cost of each vacation.
Explanation / Answer
Program Code:
//abstract class Vacation
public abstract class Vacation
{
//protected instance variables
protected double budget;
protected String destination;
//abstract method
abstract double overOrUnderBudget();
abstract double TotalCost()
}
//Encapsulating the AllInclusiveVacation class
class AllInclusiveVacation extends Vacation
{
//declare the variables
protected String brand;
protected int rating;
protected double price;
AllInclusiveVacation(String dest, double bud, String b, int r, double p)
{
destination=dest;
budget=bud;
brand=b;
rating=r;
price=p;
}
//TotalCost method that returns total vacation value
public double TotalCost()
{
return price;
}
//define the overOrUnderBudget method that returns
//a double value
public double overOrUnderBudget()
{
return budget-price;
}
//toString() method that returns a string value
public String toString()
{
String s="";
s+="Destination: "+destination+" Budget: "+budget;
s+=" Brand: "+brand+" Rating: "+rating+" Price: "+price;
s+=" Budget Left: "+overOrUnderBudget();
return s;
}
}
import java.util.ArrayList;
class PiecemealVacation extends Vacation
{
protected double totalPrice;
public String[] item={"Hotel", "Meals", "Airfair"};
private ArrayList<Double> costs;
PiecemealVacation(String dest, double bud, ArrayList<Double> cost)
{
destination=dest;
budget=bud;
costs=cost;
}
//TotalCost method
public double TotalCost()
{
totalPrice=0;
for(int i=0;i<item.length;i++)
{
totalPrice+=costs.get(i);
}
return totalPrice;
}
//define the overOrUnderBudget method that returns
//a double value
public double overOrUnderBudget()
{
totalPrice=0;
for(int i=0;i<item.length;i++)
{
totalPrice+=costs.get(i);
}
return budget-totalPrice;
}
//toString() method that returns a string value
public String toString()
{
String s="";
s+="Destination: "+destination+" Budget: "+budget;
for(int i=0;i<item.length;i++)
s+=" Item: "+item[i]+" Cost: "+costs.get(i);
System.out.println(overOrUnderBudget());
s+=" Budget Left: "+overOrUnderBudget();
return s;
}
}
import java.util.*;
// A client class to test these two classes.
class VacationClient
{
public static void main(String args[])
{
ArrayList<Object> arrayObj=new ArrayList<Object>();
Scanner input=new Scanner(System.in);
double budget;
String[] item={"Hotel", "Meals", "Airfair"};
String destination;
String brand;
int rating;
double price;
String choice="yes";
int ch;
ArrayList<Double> overallPrices=new ArrayList<Double>();
ArrayList<Double> cost=new ArrayList<Double>();
do
{
System.out.println("*****Welcome to Vacation Season Budget*****");
System.out.println("1.AllInclusiveVacation");
System.out.println("2.PiecemealVacation");
System.out.println("3.Quit");
System.out.println("Enter your choice: ");
ch=input.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter your destination: ");
destination=input.next();
System.out.println("Enter your Budget: ");
budget=input.nextDouble();
System.out.println("Enter Brand name: ");
brand=input.next();
System.out.println("Enter Rating: ");
rating=input.nextInt();
System.out.println("Enter Price: ");
price=input.nextDouble();
AllInclusiveVacation alv=new AllInclusiveVacation(destination, budget, brand, rating, price);
arrayObj.add(alv);
overallPrices.add(alv.TotalCost());
break;
case 2:
System.out.println("Enter your destination: ");
destination=input.next();
System.out.println("Enter your Budget: ");
budget=input.nextDouble();
for(int i=0;i<item.length;i++)
{
System.out.println("Enter "+item[i]+" Cost: ");
cost.add(input.nextDouble());
}
PiecemealVacation pv=new PiecemealVacation(destination, budget, cost);
arrayObj.add(pv);
overallPrices.add(pv.TotalCost());
break;
case 3:
choice="NO";
break;
default:
System.out.println("Thank you! Select the correct option.");
}
}while(choice.equalsIgnoreCase("Yes"));
System.out.println("Overall Vacation spent is: ");
for(int i=0;i<arrayObj.size();i++)
System.out.println(arrayObj.get(i).toString());
System.out.println("Total amount spent is: "+ computeTotal(overallPrices));
}
public static double computeTotal(ArrayList<Double> oallPrices)
{
double total=0;
for(int i=0;i<oallPrices.size();i++)
{
total+=oallPrices.get(i);
}
return total;
}
}
---------------------------------------------------------------------------------------------------
Sample output:
*****Welcome to Vacation Season Budget*****
1.AllInclusiveVacation
2.PiecemealVacation
3.Quit
Enter your choice:
1
Enter your destination:
Hawai
Enter your Budget:
5000
Enter Brand name:
KingJames
Enter Rating:
4
Enter Price:
4500
*****Welcome to Vacation Season Budget*****
1.AllInclusiveVacation
2.PiecemealVacation
3.Quit
Enter your choice:
1
Enter your destination:
Japan
Enter your Budget:
5000
Enter Brand name:
KewiCho
Enter Rating:
5
Enter Price:
4500
*****Welcome to Vacation Season Budget*****
1.AllInclusiveVacation
2.PiecemealVacation
3.Quit
Enter your choice:
2
Enter your destination:
Tokyo
Enter your Budget:
6000
Enter Hotel Cost:
2000
Enter Meals Cost:
1500
Enter Airfair Cost:
2000
*****Welcome to Vacation Season Budget*****
1.AllInclusiveVacation
2.PiecemealVacation
3.Quit
Enter your choice:
2
Enter your destination:
Canada
Enter your Budget:
7000
Enter Hotel Cost:
3000
Enter Meals Cost:
1000
Enter Airfair Cost:
2500
*****Welcome to Vacation Season Budget*****
1.AllInclusiveVacation
2.PiecemealVacation
3.Quit
Enter your choice:
3
Overall Vacation spent is:
Destination: Hawai
Budget: 5000.0
Brand: KingJames Rating: 4 Price: 4500.0
Budget Left: 500.0
Destination: Japan
Budget: 5000.0
Brand: KewiCho Rating: 5 Price: 4500.0
Budget Left: 500.0
Destination: Tokyo
Budget: 6000.0
Item: Hotel Cost: 2000.0
Item: Meals Cost: 1500.0
Item: Airfair Cost: 2000.0
Budget Left: 500.0
Destination: Canada
Budget: 7000.0
Item: Hotel Cost: 2000.0
Item: Meals Cost: 1500.0
Item: Airfair Cost: 2000.0
Budget Left: 1500.0
Total amount spent is: 20000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.