Question

In: Computer Science

Exercise3 Given the following classes: The Holiday class has: Properties of destination (String), duration (int days)...

Exercise3

Given the following classes:

The Holiday class has:

  • Properties of destination (String), duration (int days) and cost (int $).
  • All expected getters and setters.
  • A default constructor
  • A fully parameterised constructor (which takes all properties)
  • A toString method which returns a String showing all the properties.

The TravelAgent class has:

  • Properties of name (String), postcode (String) and holidays (an ArrayList of Holidays) – complete with getters and setters.
  • A parameterised constructor (name and postcode only) which also initialises the holidays ArrayList.
  • A method (addHoliday) which adds a holiday to the Travel Agents list.
  • A toString method which returns a String showing all the properties including details of the Holidays.

(RunTravelAgent.java

public class RunTravelAgent 
{
    public static void main(String[] args)
    {
        Holiday h1 = new Holiday("Bermuda", 2, 800);
        Holiday h2 = new Holiday("Hull", 14, 8);
        Holiday h3 = new Holiday("Los Angeles", 12, 2100);

        TravelAgent t1 = new TravelAgent("CheapAsChips", "MA99 1CU");
        t1.addHoliday(h1);
        t1.addHoliday(h2);
        t1.addHoliday(h3);
        
        TravelAgent t2 = new TravelAgent("Shoe String Tours", "CO33 2DX");
        
        System.out.println(t1);
        
        System.out.printf("h3 Duration=%s days & Cost=$%s\n", h3.getDuration(), h3.getCost());
        System.out.printf("t2 %s %s\n", t2.getName(), t2.getPostcode());
    }
}       

)

Implement these two classes. You are supplied with a test file called RunTravelAgent.java which should produce this output:

CheapAsChips at MA99 1CU

Holiday{destination=Bermuda, duration=2 days, cost=$800}

Holiday{destination=Hull, duration=14 days, cost=$8}

Holiday{destination=Los Angeles, duration=12 days, cost=$2100}

h3 Duration=$12 & Cost =$2100

t2 Shoe String Tours CO33 2DX

Solutions

Expert Solution

Hi,

Please find below code as per your requirement.

I didn't modify RunTravelAgent.java class so not posting again.

Let me know if you have any concern/doubt in this answer via comments.

Hope this answer helps you.

Thanks.

/*************************JAVA CODE**************************/

/**************Holiday.java***************/

public class Holiday {

   //instance variables
   private String destination;
   private int duration;
   private int cost;
  
   /**
   * This is default constructor which construct Holiday object with default values.
   */
   public Holiday() {
      
   }
  
   /**
   * Parameterized constructor which takes 3 parameter to construct Holiday object
   * @param destination
   * @param duration
   * @param cost
   */
   public Holiday(String destination, int duration, int cost) {
       this.destination = destination;
       this.duration = duration;
       this.cost = cost;
   }

   /**
   * @return the destination
   */
   public String getDestination() {
       return destination;
   }

   /**
   * @param destination the destination to set
   */
   public void setDestination(String destination) {
       this.destination = destination;
   }

   /**
   * @return the duration
   */
   public int getDuration() {
       return duration;
   }

   /**
   * @param duration the duration to set
   */
   public void setDuration(int duration) {
       this.duration = duration;
   }

   /**
   * @return the cost
   */
   public int getCost() {
       return cost;
   }

   /**
   * @param cost the cost to set
   */
   public void setCost(int cost) {
       this.cost = cost;
   }

   /**
   * This method returns String representation of Holiday object
   */
   @Override
   public String toString() {
       return "Holiday {destination=" + destination + ", duration=" + duration + " days, cost=$" + cost + "}";
   }
  
  
}

/*************TravelAgent.java***************/

import java.util.ArrayList;

public class TravelAgent {

   //instance variables
   private String name;
   private String postcode;
   private ArrayList<Holiday> holidays;
  
   /**
   * Parameterized constructor which takes 2 parameter to construct Travel Agent object
   * @param name
   * @param postcode
   */
   public TravelAgent(String name, String postcode) {
       this.name = name;
       this.postcode = postcode;
       //initializing arraylist of holiday
       this.holidays = new ArrayList<Holiday>();
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the postcode
   */
   public String getPostcode() {
       return postcode;
   }

   /**
   * @param postcode the postcode to set
   */
   public void setPostcode(String postcode) {
       this.postcode = postcode;
   }
  
   /**
   * This method adds holiday into the list of holiday
   * @param holiday
   */
   public void addHoliday(Holiday holiday) {
       this.holidays.add(holiday);
   }

   /**
   * This method returns String representation of TravelAgent including holiday
   */
   @Override
   public String toString() {
       //StringBuilder used to make mutable string
       StringBuilder sb = new StringBuilder();
       sb.append(name + " at " + postcode);
       //iterating over all holiday objects in list and used toString of holiday to get String representation of Holiday object
       for (Holiday holiday : holidays) {
           sb.append("\n"+holiday.toString());  
       }
       return sb.toString();
   }
  
  
}


Related Solutions

Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); };...
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); }; And this code in main: vector<Issue> tickets; Issue issu; a) tickets.push_back(-99); State whether this code is valid, if not, state the reason b) Properly implement the setProblem() method as it would be outside of the class. You MUST name the parameter problem as shown in the prototype like: (string problem) c) Write code that will output the problem attribute for every element in the...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String),...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String), phoneNumber (String), and address (String). 2. Implement the initialization constructor . 3. Implement the setters and getters for all attributes. 4. Implement the toString() method to display all attributes. 5. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. 6. Implement the compareTo (Entry...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
Complete the following tasks: a. Create a class named Trip that includes four string variables: destination...
Complete the following tasks: a. Create a class named Trip that includes four string variables: destination (for example, “London”), means of transportation (for example, “air”), departure date (for example, “12/15/2015”), and trip's purpose (for example, “business”). Include two overloaded constructors. The default constructor sets each field to “XXX”. The nondefault constructor accepts four parameters—one for each field. Include two overloaded display() methods. The parameterless version displays all the Trip details. The second version accepts a string that represents a destination...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top = -1; } //Assume that your answers to 3.1-3.3 will be inserted here, ex: // full() would be here //empty() would be here... //push() //pop() //displayStack() } 1. Write two boolean methods, full( ) and empty( ). 2. Write two methods, push( ) and pop( ). Note: parameters may be expected for push and/or pop. 3. Write the method displayStack( ) that prints the...
Given a string and a non-negative int n, we'll say that the front of the string...
Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front; frontTimes("Chocolate", 2) → "ChoCho" frontTimes("Chocolate", 3) → "ChoChoCho" frontTimes("Abc", 3) → "AbcAbcAbc" Must work the following problem using a while loop or do while.
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT