CS2511 Chapter 10 Individual Lab Assignment Write an abstract super class encaps
ID: 3823981 • Letter: C
Question
CS2511 Chapter 10 Individual Lab Assignment
Write an abstract super class encapsulating a vacation: A vacation has one attribute: a destination. It has an overloaded constructor a toString() and an abstract calculate cost method that returns the total cost of each vacation. 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. In addition an all-inclusive vacation has an overloaded constructor, a toString() and a concrete calculate cost method that returns the total cost of the all-inclusive vacation vacation ( price).
A piecemeal vacation has three additional attributes: hotel, meal, airfare as corresponding costs. In addition an all-inclusive vacation has an overloaded constructor, a toString() and a concrete calculate cost method that returns the total cost of the all-inclusive vacation ( sum of hotel, meal and airfare).
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 each vacation element. Classes need to be in java coded.
Explanation / Answer
import java.util.*;
abstract class Vacation
{
private String destination;
public Vacation(String destination)
{
this.destination = destination;
}
public String toString()
{
return " Destination "+destination;
}
public abstract double calculateCost();
}
class allInclusive extends Vacation
{
private String brand ;
private int rating;
private double price;
public allInclusive(String brand,int rating,double price,String destination)
{
super(destination);
this.brand = brand;
this.rating = rating;
this.price = price;
}
public String toString()
{
return super.toString()+" Brand : "+brand +" Rating : "+rating +" Price : "+price;
}
public double calculateCost()
{
return price;
}
}
class pieceMeal extends Vacation
{
private double hotelCost;
private double mealCost;
private double airfareCost;
public pieceMeal(double hotelCost,double mealCost,double airfareCost,String destination)
{
super(destination);
this.hotelCost = hotelCost;
this.mealCost = mealCost;
this.airfareCost = airfareCost;
}
public String toString()
{
return super.toString()+ " Hotel Cost: "+hotelCost +" Meal Cost : "+mealCost +" Airfare Cost : "+airfareCost;
}
public double calculateCost()
{
return (hotelCost +mealCost+airfareCost);
}
}
class TestVacation
{
public static void main (String[] args)
{
List<Vacation> mylist = new ArrayList<Vacation>(); // create list of vacations
allInclusive ai = new allInclusive("Super",4,789.56,"Paris");
pieceMeal pm = new pieceMeal(657.56,78.9,45.6,"Hawaii");
mylist.add(ai);
mylist.add(pm);
for(int i=0;i<mylist.size();i++)
{
System.out.println(mylist.get(i).toString());
System.out.println(" Cost : $"+mylist.get(i).calculateCost());
}
}
}
Output:
Destination Paris Brand : Super Rating : 4 Price : 789.56
Cost : $789.56
Destination Hawaii Hotel Cost: 657.56 Meal Cost : 78.9 Airfare Cost : 45.6
Cost : $782.06