Question

Activity 1:  Review Chapter 8 and All Inheritance examples. Build an application that handles setting up...

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.


Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Choose any entity of your choice to represent a superclass. It must not be any of...
Choose any entity of your choice to represent a superclass. It must not be any of the following: - Human, Student, Vehicle, Person, Car, Animal, Book • Create a class for your entity with the following: - 3 instance variables - 3 instance methods - 2 of the instance methods must show overloading - A default constructor - A parameterized constructor that initializes all instance variables - Getters and setters for the instance variables - Method to print all the...
Create java class with name Dog. Instructions for Dog class:              This class is modeled after...
Create java class with name Dog. Instructions for Dog class:              This class is modeled after a Dog. You should have instance variables as follows: The Dog’s name The number of tricks known by the Dog. The color*of the Dog’s coat (fur). *: For the Dog’s fur color you should use the Color class from the java.awt package. You should choose proper types and meaningful identifiers for each of these instance variables. Your class should contain two constructor methods. A...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called...
JAVA ONLY - Complete the numbered comments import java.text.DecimalFormat; /** * Complete the code below the...
JAVA ONLY - Complete the numbered comments import java.text.DecimalFormat; /** * Complete the code below the numbered comments, 1 - 4. * Do not change the pre-written code * @author */ public class HouseListing { /* * 1. Create instance variables as shown in UML */ /* * default constructor (If an empty house object is created) * assigns listingNumber the text string 0000 * assigns houseDescription an empty text string * assigns listingPrice the value of 1.0 */ public...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
1-To create an ArrayList of strings, which of the following is correct? `a ArrayList<String> list =...
1-To create an ArrayList of strings, which of the following is correct? `a ArrayList<String> list = new ArrayList<String>(); b ArrayList<String> list = new String<String>​[]; c ArrayList<String> list = new ArrayList<String>; d ArrayList​[String] list = new ArrayList​[String]; 2-All instance variables? a Are assigned a default value if they are not initialized b Must be initialized as part of their declaration c Are assigned an arbitrary value if they are not initialized d Must be initialized by the constructor 3-An instance method...
A stack can be used to print numbers in other bases (multi-base output). Examples: (Base8) 2810...
A stack can be used to print numbers in other bases (multi-base output). Examples: (Base8) 2810 = 3 * 81 + 4 * 80 = 348 (Base4) 7210 = 1 * 43 + 0 * 42 + 2 * 41 + 0 * 4 = 10204 (Base 2) 5310 = 1 * 25 + 1*24 + 0 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 1101012 Write a java program using stacks that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT