Question

In: Computer Science

I'm getting an error with my code on my EvenDemo class. I am supposed to have...

I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this?

//********************************************************
// Event Class code
//********************************************************

package java1;

import java.util.Scanner;

public class Event {
   public final static double lowerPricePerGuest = 32.00;
   public final static double higherPricePerGuest = 35.00;
   public final static int cutOffValue = 50;
   public boolean largeEvent;
   private String eventNum;
   private int numGuests;
   private double price;
   private double pricePerGuest;
   public Event(String event, int guests) {
   eventNum = event;
   numGuests = guests;
   }
   public Event() {
   this("A000", 0);
   }
   private Scanner input = new Scanner(System.in);
   public void setEventNumber() {
   System.out.print("Please enter your event number (i.e. A001): ");
   eventNum = input.nextLine();
   }
   public void setNumOfGuests() {
   System.out.print("Please enter the number of guests that will be attending? ");
   numGuests = input.nextInt();
   if (isLargeEvent()) {
   pricePerGuest = lowerPricePerGuest;
   }
   else {
   pricePerGuest = higherPricePerGuest;
   }
   price = numGuests * pricePerGuest;
   largeEvent = (numGuests >= cutOffValue);
   System.out.println("");
   }
   public boolean isLargeEvent() {
   if (this.getNumOfGuests() > 50) {
       return true;
   }
   else {
   return false;
   }
   }
   public String getEventNumber() {
   return eventNum;
   }
   public int getNumOfGuests() {
   return numGuests;
   }
   public double getPrice() {
   return price;
   }
   }
//********************************************************
// Event Demo Class code
//********************************************************
package java1;
import java.util.Scanner;
public class EventDemo {
   public static void main(String[] args) {
       Event eventOne = new Event();
       Event eventTwo = new Event();
       Event eventThree = new Event();
       System.out.println("Enter Details of event # 1:");
       eventOne.setEventNumber();
       eventOne.setNumOfGuests();
       System.out.println("Enter Details of event # 2:");
       eventTwo.setEventNumber();
       eventTwo.setNumOfGuests();
       System.out.println("Enter Details of event # 3:");
       eventThree.setEventNumber();
       eventThree.setNumOfGuests();
       displayMotto();
       System.out.println();
       display(eventOne);
       display(eventTwo);
       display(eventThree);
       System.out.println("Get largest event between event # 1 and event # 2 is :");
       display(getLargestEvent(eventOne, eventTwo));
       System.out.println("Get largest event between event # 2 and event # 3 is :");
       display(getLargestEvent(eventTwo, eventThree));
       System.out.println("Get largest event from event # 1 and event # 3 is :");
       display(getLargestEvent(eventOne, eventThree));
   runLoop();
   }
       public static void runLoop() {
           final int SIZE=4;
           Event[] eventObjects = new Event[SIZE];
           int numGuests;
           Scanner scanner = new Scanner(System.in);
           for (int i = 0; i < eventObjects.length; i++)
           {
           eventObjects[i]=new Event();
           eventObjects[i].setEventNumber();
           do
           {
           System.out.println("Enter number of guests : ");
           numGuests=scanner.nextInt();
           if(numGuests<5 ||numGuests>100)
           System.out.println("Number of guests must be between 5 to 100");
           }while(numGuests<5 ||numGuests>100);
           eventObjects[i].setGuests(numGuests);
           }
           Event oneEvent=eventObjects[0];
           System.out.println("For first event , printing message ");
           System.out.println(oneEvent.getGuestsCount()+" times");
           for (int i = 0; i < oneEvent.getGuestsCount(); i++)
           {
           System.out.println("Please come to my event!");
           }
      
   }
       public static void display(Event e) {
       System.out.println("Event number: " + e.getEventNumber());
       System.out.println("Total guests: " + e.getNumOfGuests());
       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.getNumOfGuests() > e2.getNumOfGuests()) {
       return e1;
       }
       else {
       return e2;
       }
       }
      
public static void displayMotto() {
        System.out.println();
    System.out.println("MOTTO");
}
}

Solutions

Expert Solution

import java.util.Scanner;

class Event {
        public final static double lowerPricePerGuest = 32.00;
        public final static double higherPricePerGuest = 35.00;
        public final static int cutOffValue = 50;
        public boolean largeEvent;
        private String eventNum;
        private int numGuests;
        private double price;
        private double pricePerGuest;

        public Event(String event, int guests) {
                eventNum = event;
                numGuests = guests;
        }

        public Event() {
                this("A000", 0);
        }

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

        public void setEventNumber() {
                System.out.print("Please enter your event number (i.e. A001): ");
                eventNum = input.nextLine();
        }

        public void setNumOfGuests() {
                System.out.print("Please enter the number of guests that will be attending? ");
                numGuests = input.nextInt();
                if (isLargeEvent()) {
                        pricePerGuest = lowerPricePerGuest;
                } else {
                        pricePerGuest = higherPricePerGuest;
                }
                price = numGuests * pricePerGuest;
                largeEvent = (numGuests >= cutOffValue);
                System.out.println("");
        }

        public boolean isLargeEvent() {
                if (this.getNumOfGuests() > 50) {
                        return true;
                } else {
                        return false;
                }
        }

        public String getEventNumber() {
                return eventNum;
        }

        public int getNumOfGuests() {
                return numGuests;
        }

        public double getPrice() {
                return price;
        }

        public int getGuestsCount() {
                return numGuests;
        }

        public void setGuests(int aNumGuests) {
                numGuests=aNumGuests;
        }
}

//********************************************************
// Event Demo Class code
//********************************************************
public class EventDemo {
        public static void main(String[] args) {
                Event eventOne = new Event();
                Event eventTwo = new Event();
                Event eventThree = new Event();
                System.out.println("Enter Details of event # 1:");
                eventOne.setEventNumber();
                eventOne.setNumOfGuests();
                System.out.println("Enter Details of event # 2:");
                eventTwo.setEventNumber();
                eventTwo.setNumOfGuests();
                System.out.println("Enter Details of event # 3:");
                eventThree.setEventNumber();
                eventThree.setNumOfGuests();
                displayMotto();
                System.out.println();
                display(eventOne);
                display(eventTwo);
                display(eventThree);
                System.out.println("Get largest event between event # 1 and event # 2 is :");
                display(getLargestEvent(eventOne, eventTwo));
                System.out.println("Get largest event between event # 2 and event # 3 is :");
                display(getLargestEvent(eventTwo, eventThree));
                System.out.println("Get largest event from event # 1 and event # 3 is :");
                display(getLargestEvent(eventOne, eventThree));
                runLoop();
        }

        public static void runLoop() {
                final int SIZE = 4;
                Event[] eventObjects = new Event[SIZE];
                int numGuests;
                Scanner scanner = new Scanner(System.in);
                for (int i = 0; i < eventObjects.length; i++) {
                        eventObjects[i] = new Event();
                        eventObjects[i].setEventNumber();
                        do {
                                System.out.println("Enter number of guests : ");
                                numGuests = scanner.nextInt();
                                if (numGuests < 5 || numGuests > 100)
                                        System.out.println("Number of guests must be between 5 to 100");
                        } while (numGuests < 5 || numGuests > 100);
                        eventObjects[i].setGuests(numGuests);
                }
                Event oneEvent = eventObjects[0];
                System.out.println("For first event , printing message ");
                System.out.println(oneEvent.getGuestsCount() + " times");
                for (int i = 0; i < oneEvent.getGuestsCount(); i++) {
                        System.out.println("Please come to my event!");
                }

        }

        public static void display(Event e) {
                System.out.println("Event number: " + e.getEventNumber());
                System.out.println("Total guests: " + e.getNumOfGuests());
                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.getNumOfGuests() > e2.getNumOfGuests()) {
                        return e1;
                } else {
                        return e2;
                }
        }

        public static void displayMotto() {
                System.out.println();
                System.out.println("MOTTO");
        }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental. ///////////////////////////////RentalDemo package java1; import java.util.Scanner; public class RentalDemo { public static void main(String[] args) {    Rental object1 =...
I have the following assignment for probability class: I am supposed to write a routine (code)...
I have the following assignment for probability class: I am supposed to write a routine (code) in MATLAB that does solve the following problem for me: a) Generate N=10000 samples of a Uniform random variable (X) using the rand () command. This Uniform RV should have a range of -1 to +3. b) Generate (Estimate) and plot the PDF of X from the samples. You are not allowed to use the Histogram () command, any commands from the Matlab Statistics...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;        count = 0;    } // end Dersop3 class constructor   ...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
I am implementing a generic List class and not getting the expected output. My current output...
I am implementing a generic List class and not getting the expected output. My current output is: [0, 1, null] Expected Output in a separate test class: List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size 6: [0, 1, null, Three, null, 4]. list.set(3, "Three"); is going to produce a list of size 5 (unchanged): [0,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT