Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java program Follow all the instructions below, and make sure to include constru

ID: 3849764 • Letter: J

Question

java program

Follow all the instructions below, and make sure to include constructors, as well as getters and setters for all the attributes.

• Implement the interface Event. It declares two methods, getStart() and getEnd(), both returning a value of type Date

• Write the implementation of the abstract class Trip. It implements the characteristics that are common to its sub-classes, here CarTrip and PlaneTrip

– Trip implements the interface Event

– All the trips have a start and end date, as well as a departure location and a destination. Their initial values are passed to the constructor.

– Trip declares an abstract method called checklist, which checks whether all the conditions for a trip are satisfied. If they are, it returns true. Otherwise, it returns false

• Implement the class CarTrip

– CarTrip has a constructor which, in addition to the default parameters of Trip, also assigns values to isFull (indicates whether or not the gas tank is full), breaksChecked (true or false), and the number of passengers (an integer)

– CarTrip implements the method checklist. It returns true if the gas tank is full, the breaks were checked, and the number of people traveling is 4 or less, which is the capacity of the car

• Implement the class PlaneTrip

– PlaneTrip has a constructor which, in addition to the default parameters of Trip, also assigns values to ticketIssued (true or false), passportValid (true or false), numberCheckedLuggage (an integer), and numberCarryOn (an integer).

– PlaneTrip implements the method checklist. It returns true if the ticket was issued, the passport is valid, the number of checked items is two or less, and the number of carry-on items is one or less.

Implement the class Agenda. Similarly to Assignment #1 Question #2, Agenda uses a fixedsize array to store elements. Give all the necessary class and instance variables, as well as a constructor. For simplicity, herein, you will only implement one method, add

– boolean add(Event e): adds an event to the agenda. Events are kept in increasing order of start time. The method returns true if the event was added to the collection, and false otherwise.

Car Trip Trip Plane Trip interface Event Agenda

Explanation / Answer

Note: Apart from what is specified above, for the sake of completeness I have added additional constructors, through which values of Trip class can be directly initialized. Also, Class Agenda might be slightly different as there is no information on "Similarly to Assignment #1 Question #2, Agenda uses a fixedsize array to store elements"...

Here is the Complete Program:

import java.util.Date;

interface Event{
   Date getStart();
   Date getEnd();
}

abstract class Trip implements Event{
   private String departureLocation;
   private String destination;
   private Date start;
   private Date end;
  
   public Trip(){
       departureLocation="Here";
       destination="Here";
       start=new Date();
       end=new Date();
   }
  
   public Trip(String departureLocation, String destination,Date startDate, Date endDate){
       departureLocation=this.departureLocation;
       destination=this.destination;
       startDate=this.start;
       endDate=this.end;
   }
  
  
   public abstract boolean checkList();
  
   public String getDepartureLocation() {
       return departureLocation;
   }

   public void setDepartureLocation(String departureLocation) {
       this.departureLocation = departureLocation;
   }

   public String getDestination() {
       return destination;
   }

   public void setDestination(String destination) {
       this.destination = destination;
   }

   public Date getStart() {
       return start;
   }

   public void setStart(Date start) {
       this.start = start;
   }

   public Date getEnd() {
       return end;
   }

   public void setEnd(Date end) {
       this.end = end;
   }

  
}

class CarTrip extends Trip{
   private boolean isFull;  
    private boolean breaksChecked;
    private int numOfPassengers;
  

    public CarTrip(boolean isFull, boolean breaksChecked, int numOfPassengers) {
       super();
       this.isFull = isFull;
       this.breaksChecked = breaksChecked;
       this.numOfPassengers = numOfPassengers;
   }

  
    public CarTrip(String departureLocation, String destination, Date startDate, Date endDate, boolean isFull,
           boolean breaksChecked, int numOfPassengers) {
      
       super(departureLocation, destination, startDate, endDate);
       this.isFull = isFull;
       this.breaksChecked = breaksChecked;
       this.numOfPassengers = numOfPassengers;
   }

   public boolean isFull() {
       return isFull;
   }

   public void setFull(boolean isFull) {
       this.isFull = isFull;
   }

   public boolean isBreaksChecked() {
       return breaksChecked;
   }

   public void setBreaksChecked(boolean breaksChecked) {
       this.breaksChecked = breaksChecked;
   }

   public int getNumOfPassengers() {
       return numOfPassengers;
   }

   public void setNumOfPassengers(int numOfPassengers) {
       this.numOfPassengers = numOfPassengers;
   }

   @Override
   public boolean checkList() {
       boolean status=true;
      
       if((!isFull) || (!breaksChecked) || (numOfPassengers>4 || numOfPassengers<1)){
           status=false;
       }
      
       return status;
   }
}

class PlaneTrip extends Trip{
   private boolean ticketAssigned;
   private boolean validPassport;
   private int numOfCheckedItems;
   private int carryOnItems;
  
  
   public PlaneTrip(boolean ticketAssigned, boolean validPassport, int numOfCheckedItems, int carryOnItems) {
       super();
       this.ticketAssigned = ticketAssigned;
       this.validPassport = validPassport;
       this.numOfCheckedItems = numOfCheckedItems;
       this.carryOnItems = carryOnItems;
   }

   public PlaneTrip(String departureLocation, String destination, Date startDate, Date endDate, boolean ticketAssigned,
           boolean validPassport, int numOfCheckedItems, int carryOnItems) {
      
       super(departureLocation, destination, startDate, endDate);
       this.ticketAssigned = ticketAssigned;
       this.validPassport = validPassport;
       this.numOfCheckedItems = numOfCheckedItems;
       this.carryOnItems = carryOnItems;
   }


   @Override
   public boolean checkList() {
       boolean status=true;
      
       if((!ticketAssigned) || (!validPassport) || (numOfCheckedItems>2) || (carryOnItems> 1)){
           status=false;
       }
       return status;
   }
  
}


public class Agenda {

   private Event[] events;
   private int nextIndex;           
  

   public Agenda(int size){
       events = new Event[size];
       nextIndex=0;
   }
  
   boolean addEvent(Event e){
       if(nextIndex==0){               //If this is the first element
           events[nextIndex]=e;
           ++nextIndex;
           return true;
       }
       else if(nextIndex == events.length-1)       //If the Array is Full
       {
           return false;
       }else if(e.getStart().after(events[nextIndex-1].getEnd())){   //If Event e starts after, the last event in array ends(Time)
               events[nextIndex]=e;
               ++nextIndex;
               return true;
       }
          
       return false;
   }
  
   public Event[] getEvents() {
       return events;
   }

   public void setEvents(Event[] events) {
       this.events = events;
   }
  
   public int getNextIndex() {
       return nextIndex;
   }

   public void setNextIndex(int nextIndex) {
       this.nextIndex = nextIndex;
   }
  
}