Write an abstract superclass encapsulating or for a Vacation: A vacation has two
ID: 3728734 • Letter: W
Question
Write an abstract superclass encapsulating or for a Vacation: A vacation has two attributes, budget and destination. It has an abstract method returning how much the vacation is over budget. Create two non-abstract classes that extend vacation: one for an all-inclusive vacation, and another for a piecemeal vacation. All inclusive will have three attributes: brand, rating (represented by number of stars), and a price. Piecemeal needs to have a list of items (hotel, meals, airfaire, etc.) and their corresponding costs. This can be handled using parallel arrays, thus there will only be two attributes. Be sure to write appropriate getters, setters, and constructors for all 3 classes. Don't forget the abstract method. HINT: It is ok to have a constructor for an abstract class. It can be called to set up attribute values from the child class.
Write a test application for these classes. You can create this as an application for a user to determine which vacation option is right for them. Create a piecemeal and all inclusive vacation object. Be sure to appropriately ask for and load the field values. Display all information for each object and display whether the vacation is over budget (showing by how much), under budget (showing how much), or right at budget.
Explanation / Answer
public abstract class Vacation
{
protected double budget;
protected String destination;
abstract double overOrUnderBudget();
abstract double TotalCost()
}
class AllInclusiveVacation extends Vacation
{
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;
}
public double TotalCost()
{
return price;
}
public double overOrUnderBudget()
{
return budget-price;
}
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;
}
public double TotalCost()
{
totalPrice=0;
for(int i=0;i<item.length;i++)
{
totalPrice+=costs.get(i);
}
return totalPrice;
}
public double overOrUnderBudget()
{
totalPrice=0;
for(int i=0;i<item.length;i++)
{
totalPrice+=costs.get(i);
}
return budget-totalPrice;
}
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.*;
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.