Question

In: Computer Science

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 and displays the Trip details only if the Trip's destination matches the parameter.
  • b. Create an application that instantiates several Trip objects and demonstrates all the methods.

Solutions

Expert Solution

class Trip {

    private String destination;
    private String transportation;
    private String departureDate;
    private String purpose;

    public Trip() {
        this.destination = "XXX";
        this.transportation = "XXX";
        this.departureDate = "XXX";
        this.purpose = "XXX";
    }

    public Trip(String destination, String transportation, String departureDate, String purpose) {
        this.destination = destination;
        this.transportation = transportation;
        this.departureDate = departureDate;
        this.purpose = purpose;
    }

    public void display() {
        System.out.println("Destination: " + destination);
        System.out.println("Transportation: " + transportation);
        System.out.println("Departure Date: " + departureDate);
        System.out.println("Trip's Purpose: " + purpose);
        System.out.println();
    }

    public void display(String destination) {
        if (destination.equals(this.destination)) {
            display();
        }
    }
}

class TripTest {

    public static void main(String[] args) {
        Trip trip1 = new Trip("London", "Air", "12/15/2015", "Business");
        Trip trip2 = new Trip();
        Trip trip3 = new Trip("Paris", "Air", "9/20/2016", "Vacation");
        trip1.display();
        trip2.display();
        trip3.display("Paris");
        trip3.display("London");
    }
}


Related Solutions

Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data....
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin name as part of the constructor and stores it in a private string             Has a method that returns the coins name             Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following:             A constructor that passes the coins name to the parent             A private variable that defines the...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Create the class diagram and write the pseudocode that defines the class. b. Design an application that declares three BaseballGame objects and sets and displays their values. c. Design an application that declares an array of 12 BaseballGame objects. Prompt the user for...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and average petrol consumption. Provide 2 constructors, the first takes name and type as parameter, the second takes four parameters for the four instance variables. (2 pt) Provide also a method called distancePerTank that calculate the average distance that a vehicle can travel if the tank is full (multiplies the tank size by the average petrol consumption), then returns the value. (2 pt) Provide a...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT