The theatre wants to collect the names of all audience who visit the theatre and calculate the total amount collected as fare for the tickets from the audience. Write a Java program that implements the above using the concept of Constructor Overloading (Polymorphism).
Question
The theatre wants to collect the names of all audience who visit the theatre and calculate the total amount collected as fare for the tickets from the audience. Write a Java program that implements the above using the concept of Constructor Overloading (Polymorphism).
Solution
Here is a simple Java program that uses constructor overloading to implement the above scenario:
import java.util.ArrayList;
class Audience {
String name;
double fare;
// Default constructor
Audience() {
this.name = "";
this.fare = 0.0;
}
// Constructor with name parameter
Audience(String name) {
this.name = name;
this.fare = 0.0;
}
// Constructor with name and fare parameters
Audience(String name, double fare) {
this.name = name;
this.fare = fare;
}
}
class Theatre {
ArrayList<Audience> audienceList;
double totalFare;
// Constructor
Theatre() {
audienceList = new ArrayList<>();
totalFare = 0.0;
}
// Method to add audience
void addAudience(String name, double fare) {
Audience audience = new Audience(name, fare);
audienceList.add(audience);
totalFare += fare;
}
// Method to display total fare
void displayTotalFare() {
System.out.println("Total fare collected: " + totalFare);
}
}
public class Main {
public static void main(String[] args) {
Theatre theatre = new Theatre();
theatre.addAudience("John Doe", 50.0);
theatre.addAudience("Jane Doe", 60.0);
theatre.displayTotalFare();
}
}
In this program, we have two classes: Audience and Theatre. The Audience class has three constructors: a default constructor, a constructor that takes a name, and a constructor that takes a name and a fare. The Theatre class has a method addAudience that creates an Audience object and adds it to the audienceList. It also updates the totalFare. The displayTotalFare method prints the total fare collected. In the main method, we create a Theatre object and add two Audience objects to it, then display the total fare.
Similar Questions
The theatre wants to collect the names of all audience who visit the theatre and calculate the total amount collected as fare for the tickets from the audience. Write a Java program
In a theatre four types of audience come to watch movies. They are, 1. Singles 2. Couples 3. A set of parents + 1 kid 4. A set of parents + 2 kid The theatre ticket rate for any audience group as follows: 1st person in the group -Rs.400/- 2nd person in the group -Rs.300/- 3rd person in the group -Rs.200/- 4th person in the group -Rs.100/- The theatre wants to collect the names of all audience who visit the theatre and calculate the total amount collected as fare for the tickets from the audience. Write a Java program that implements the above using the concept of Constructor Overloading (Polymorphism). Input 1: Enter the total number of audience group : 2 Enter the group category: 1 Enter name of first person: Kaushik Enter the group category: 3 Enter name of first person: Ashmi Enter name of second person: Krish Enter name of third person: Mahi Output 1 The audience names are Kaushik Ashmi Krish Mahi The total amount collected: 1300 Input 2: Enter the total number of audience group : 3 Enter the group category: 1 Enter name of first person: Kaushik Enter the group category: 3 Enter name of first person: Ashmi Enter name of second person: Krish Enter name of third person: Mahi Enter the group category: 4 Enter name of first person: Rashmi Enter name of second person: Rakesh Enter name of third person: Jesi Enter name of fourth person: Meha Output 2 The audience names are Kaushik Ashmi Krish Mahi Rashmi Rakesh Jesi Meha The total amount collected: 2300
For a movie ticket booking system, write a program in Java to implement the process. Some persons need First class seating system (Option1). Another option is Second class seating system (Option 2). Implement this process using Polymorphism and inheritance. In the ‘First’ class get the inputs like movie name, Theater Name, Show Date, and Number of Seats. If the date is 2022-11-20 print the price as 500 else print the price as 1000 for each seat. In the ‘Second’ class Get the inputs like movie name, Theater Name, Show Date, and Number of Seats. Cost of each ticket is Rs.200. If theater name is “mnbjhrbjbj” apply coupon code and reduce Rs.500 from actual price. Print all the details as a report.
write a java program to define a class Item having item namepricequantityfind the total cost of the itemuse accept method to get the data from the user and toString method to display the detailscreate two objects and display the name of the item which is ordered more quantitydisplay total amount of items purchased.edited 10:37 AM
Develop a Java program for a ticket pricing system. Prompt the user to enter their age and the type of ticket they want to purchase (1 for regular, 2 for VIP). Use if-then-else statements to calculate the ticket price based on the following criteria:Regular Ticket Prices:Age 0-5: FreeAge 6-12: $10Age 13-18: $15Age 19 and above: $20VIP Ticket Prices:Age 0-5: $5Age 6-12: $15Age 13-18: $20Age 19 and above: $30Display the calculated ticket price to the user. Ensure that your program handles invalid input, such as negative age or an invalid ticket type.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.