Activity 1:
Review Chapter 8 and All Inheritance
examples.
Build an application that handles setting up vacation
plans.
Create an abstract superclass called Vacation
Instance Variables
destination - String
budget - double
Constructors - default and parameterized to set
all instance variables
Access and mutator methods
budgetBalance method - abstract method that returns
the amount the vacation is under or over budget. Under budget
is a positive number and over budget is a negative
number.
Create a concrete class called AllInclusive that is a subclass of Vacation and represents an all inclusive vacation like Sandals or Club Med.
Instance Variables
brand - String, such as Sandals , Club Med
etc
rating - int, representing number of stars such
as 5 star rating.
price - double, the total cost of the
vacation
Constructor - default and parameterized to set all
instance variables
Accessor and mutator methods
Overwritten budgetBalance method.
Create a concrete class called ALaCarte that is a subclass of Vacation and represents a vacation that requires payment for each activity separately
Instance Variables
hotelName - String
roomCost - double
airline - String
airfare - double
meals - double - estimated total meal
expenses.
Constructor - default and parameterized to set all
instance variables
Accessor and mutator methods
Overwritten budgetBalance method.
Create a VacationTester class
Test all methods.
Polymorphically call the budgetBalance method by
creating at least 2 object of each Vacation subclass, and place
those object in a collection of the Vacation object.
JAVA PROGRAM
//////////////////Vacation.java////////////////////////////
package test.vacation;
//Abstract superclass: Vacation
public abstract class Vacation {
private String destination;
private double budget;
//default constructor
public Vacation(){
this.destination = "";
this.budget = 0;
}
//parameterized constructor
public Vacation(String destination, double budget)
{
this.destination =
destination;
this.budget = budget;
}
//getters and setters
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination =
destination;
}
public double getBudget() {
return budget;
}
public void setBudget(double budget) {
this.budget = budget;
}
/**
* returns the amount the vacation is under or over
budget.
* Under budget is a positive number and over budget is
a negative number.
* Has to be implemented in the concrete class
extending the abstract Vacation class
*/
public abstract double budgetBalance();
}
//////////////////AllInclusive.java/////////////////////////////////////
package test.vacation;
/**
* a concrete class called AllInclusive that is a subclass of
Vacation
* and represents an all inclusive vacation like Sandals or Club
Med
*/
public class AllInclusive extends Vacation {
//private fields
private String brand;
private int rating;
private double price;
//default constructor
public AllInclusive() {
super(); //default constructor of
Vacation is called here
this.brand = "";
this.rating = 0;
this.price = 0;
}
//parameterized constructor
public AllInclusive(String destination,double budget,
String brand, int rating, double price) {
super(destination,budget);
//parameterized constructor of Vacation is called here
this.brand = brand;
this.rating = rating;
this.price = price;
}
//getters and setters
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
//overridden budgetBalance method
public double budgetBalance() {
double budget = getBudget();
return this.price - budget;
}
}
/////////////////ALaCarte.java/////////////////////////
package test.vacation;
/**
*
* a concrete class called ALaCarte that is a subclass of
Vacation
* and represents a vacation that requires payment
* for each activity separately
*
*/
public class ALaCarte extends Vacation {
//private fields
private String hotelName;
private double roomCost;
private String airline;
private double airfare;
private double meals;
//default constructor
public ALaCarte() {
super();//default constructor of
Vacation is called here
this.hotelName = "";
this.roomCost = 0;
this.airline = "";
this.airfare = 0;
this.meals = 0;
}
//parameterized constructor
public ALaCarte(String destination, double budget,
String hotelName, double roomCost,
String airline,
double airfare, double meals) {
super(destination,budget);//parameterized constructor of Vacation
is called here
this.hotelName = hotelName;
this.roomCost = roomCost;
this.airline = airline;
this.airfare = airfare;
this.meals = meals;
}
//getters and setters
public String getHotelName() {
return hotelName;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
public double getRoomCost() {
return roomCost;
}
public void setRoomCost(double roomCost) {
this.roomCost = roomCost;
}
public String getAirline() {
return airline;
}
public void setAirline(String airline) {
this.airline = airline;
}
public double getAirfare() {
return airfare;
}
public void setAirfare(double airfare) {
this.airfare = airfare;
}
public double getMeals() {
return meals;
}
public void setMeals(double meals) {
this.meals = meals;
}
@Override
//overridden budgetBalance method
public double budgetBalance() {
double budget = getBudget();
double totalPrice = this.roomCost +
this.airfare + this.meals;
return totalPrice - budget;
}
}
//////////////////////VacationTester.java/////////////////////////////////
package test.vacation;
import java.util.ArrayList;
import java.util.List;
//Testers class for Vacation
public class VacationTester {
public static void main(String[] args){
//create array list of
vacations
List<Vacation> vacations =
new ArrayList<Vacation>();
//Create AllInclusive instance
using default constructor
//and then set fields using
setters
AllInclusive va1 = new
AllInclusive();
va1.setBudget(1500);
va1.setDestination("Europe");
va1.setBrand("Sandals");
va1.setRating(3);
va1.setPrice(1350);
vacations.add(va1); //add the
instance into the vacations list
//Create ALaCarte instance using
default constructor
//and then set fields using
setters
ALaCarte vc1 = new
ALaCarte();
vc1.setBudget(1500);
vc1.setDestination("Europe");
vc1.setAirline("Emirates");
vc1.setAirfare(795);
vc1.setHotelName("The
KingPlace");
vc1.setRoomCost(300);
vc1.setMeals(596);
vacations.add(vc1); //add the
instance into the vacations list
//Create AllInclusive instance
using parametrized constructor
AllInclusive va2 = new
AllInclusive("Mexico",1200,"Club Med", 4, 1350);
vacations.add(va2); //add the
instance into the vacations list
//Create ALaCarte instnce using
parameterized constructor
ALaCarte vc2 = new
ALaCarte("Mexico", 1200, "Holiday Inn", 469, "KLM", 469,
516);
vacations.add(vc2); //add the
instance into the vacations list
int counter = 0;
//Traverse the vacations list
for(Vacation v : vacations){
System.out.println("\nVacation#"+ ++counter); //print the vacation
number
System.out.println("========================");
if(v instanceof
AllInclusive){
//If current vacation is a AllInclusive
AllInclusive ai = (AllInclusive)v; //cast
it
//print data using getters
System.out.println("Destination:
"+ai.getDestination());
System.out.println("Budget:
"+ai.getBudget()+"USD");
System.out.println("Brand:
"+ai.getBrand());
System.out.println("Rating:
"+ai.getRating());
System.out.println("Price:
"+ai.getPrice());
}else if(v
instanceof ALaCarte){
//If current vacation is a ALaCarte
ALaCarte ac = (ALaCarte)v; //cast it
//print data using getters
System.out.println("Destination:
"+ac.getDestination());
System.out.println("Budget:
"+ac.getBudget()+"USD");
System.out.println("Hotel name:
"+ac.getHotelName());
System.out.println("Airfare:
"+ac.getAirfare()+"USD");
System.out.println("Meals :
"+ac.getMeals()+"USD");
}
//get
budgetBalance using polymorphism
//i.e actual
instance and its budgetBalance behaviour is decided at
runtime
double
budgetBalance = v.budgetBalance();
//print the
value and whether the vacation is over or under budget
System.out.println("Budget balance: "+budgetBalance+"USD");
if(budgetBalance
> 0){
System.out.println("THe vacation is in Over
Budget");
}else{
System.out.println("The vacation is Under
Budget");
}
}
}
}
================================
OUTPUT
================================
Vacation#1
========================
Destination: Europe
Budget: 1500.0USD
Brand: Sandals
Rating: 3
Price: 1350.0
Budget balance: -150.0USD
The vacation is Under Budget
Vacation#2
========================
Destination: Europe
Budget: 1500.0USD
Hotel name: The KingPlace
Airfare: 795.0USD
Meals : 596.0USD
Budget balance: 191.0USD
THe vacation is in Over Budget
Vacation#3
========================
Destination: Mexico
Budget: 1200.0USD
Brand: Club Med
Rating: 4
Price: 1350.0
Budget balance: 150.0USD
THe vacation is in Over Budget
Vacation#4
========================
Destination: Mexico
Budget: 1200.0USD
Hotel name: Holiday Inn
Airfare: 469.0USD
Meals : 516.0USD
Budget balance: 254.0USD
THe vacation is in Over Budget
Get Answers For Free
Most questions answered within 1 hours.