a. Carly’s Catering provides meals for parties and special events. In Chapter 3, you created an Event class for the company. The Event class contains two public final static fields that hold the price per guest ($35) and the cutoff value for a large event (50 guests), and three private fields that hold an event number, number of guests for the event, and the price. It also contains two public set methods and three public get methods.

Now, modify the Event class to contain two overloaded constructors

∎ One constructor accepts an event number and number of guests as parameters. Pass these values to the setEventNumber() and setGuests() methods, respectively. The setGuests() method will automatically calculate the event price.

∎ The other constructor is a default constructor that passes "A000" and 0 to the two-parameter constructor.

Save the file as Event.java

b. In Chapter 3, you also created an EventDemo class to demonstrate using two Event objects. Now, modify that class to instantiate two Event objects, and include the following new methods in the class:

∎ Instantiate one object to retain the constructor default values

∎ Accept user data for the event number and guests fields, and use this data set to instantiate the second object. Display all the details for both objects.

Save the file as EventDemo.java.

Respuesta :

Answer:

import java.util.Scanner;

public class Event {

  public final static double LOWER = 32.00;

  public final static double HIGHER = 35.00;

  public final static int CUTOFF_VALUE = 50;

  public boolean largeEvent;

  private String eventNum;

  private int totalGuests;

  private double price;

  private double pricePerGuest;

  public Event(String event, int guests) {

      eventNum = event;

      totalGuests = guests;

  }

  public Event() {

      this("A000", 0);

  }

  private Scanner input = new Scanner(System.in);

  public void setEventNumber() {

      System.out.print("Enter the event number? ");

      eventNum = input.nextLine();

  }

  public void settotalGuests() {

      System.out.print("Enter total guests that are attending the event? ");

      totalGuests = input.nextInt();

      if (isLargeEvent())

          pricePerGuest = LOWER;

      else

          pricePerGuest = HIGHER;

      price = totalGuests * pricePerGuest;

      largeEvent = (totalGuests >= CUTOFF_VALUE);

      System.out.println("");

  }

  public boolean isLargeEvent() {

      if (this.gettotalGuests() > 50)

          return true;

      else

          return false;

  }

  public String getEventNumber() {

      return eventNum;

  }

  public int gettotalGuests() {

      return totalGuests;

  }

  public double getPrice() {

      return price;

  }

}

public class EventDemo {

  public static void main(String[] args) {

      Event event1 = new Event();

      Event event2 = new Event();

      Event event3 = new Event();

      System.out.println("Enter Details of event1:");

      event1.setEventNumber();

      event1.settotalGuests();

      System.out.println("Enter Details of event2:");

      event2.setEventNumber();

      event2.settotalGuests();

      System.out.println("Enter Details of event3:");

      event3.setEventNumber();

      event3.settotalGuests();

      display(event1);

      display(event2);

      display(event3);

      System.out.println("Get largest event from event1 and event2 is :");

      display(getLargestEvent(event1, event2));

      System.out.println("Get largest event from event2 and event3 is :");

      display(getLargestEvent(event2, event3));

      System.out.println("Get largest event from event1 and event3 is :");

      display(getLargestEvent(event1, event3));

  }

  public static void display(Event e) {

      System.out.println("Event number: " + e.getEventNumber());

      System.out.println("Total guests: " + e.gettotalGuests());

      System.out.println("Total price: $"

              + String.format("%.2f", e.getPrice()));

      System.out.println("Is Large event: " + e.isLargeEvent());

      System.out.println("");

  }

  public static Event getLargestEvent(Event e1, Event e2) {

      if (e1.gettotalGuests() > e2.gettotalGuests())

          return e1;

      else

          return e2;

  }

}

Explanation:

  • Call isLargeEvent() method to check whether to set pricePerGuest as Lower or Higher.
  • Calculate the price by multiplying the total guests with price per guest.
  • Call gettotalGuests() method to check if the event is large or small.