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
Sure, here is a simple Java program that uses constructor overloading to achieve the desired functionality:
import java.util.ArrayList;
public class Theatre {
private ArrayList<String> audienceNames;
private double totalFare;
// Default constructor
public Theatre() {
this.audienceNames = new ArrayList<>();
this.totalFare = 0.0;
}
// Constructor with audience name and ticket fare
public Theatre(String audienceName, double ticketFare) {
this(); // calling default constructor to initialize the ArrayList and totalFare
this.audienceNames.add(audienceName);
this.totalFare += ticketFare;
}
// Method to add new audience and their ticket fare
public void addAudience(String audienceName, double ticketFare) {
this.audienceNames.add(audienceName);
this.totalFare += ticketFare;
}
// Method to get the total fare
public double getTotalFare() {
return this.totalFare;
}
// Method to get the audience names
public ArrayList<String> getAudienceNames() {
return this.audienceNames;
}
public static void main(String[] args) {
Theatre theatre = new Theatre();
theatre.addAudience("John Doe", 15.0);
theatre.addAudience("Jane Doe", 20.0);
System.out.println("Total fare: " + theatre.getTotalFare());
System.out.println("Audience names: " + theatre.getAudienceNames());
}
}
In this program, we have a Theatre class with two constructors. The default constructor initializes the audienceNames ArrayList and totalFare to 0. The second constructor takes an audience name and ticket fare as parameters, adds the audience name to the audienceNames ArrayList and adds the ticket fare to totalFare.
The addAudience method is used to add new audience and their ticket fare to the audienceNames ArrayList and totalFare respectively.
The getTotalFare and getAudienceNames methods are used to get the total fare and audience names respectively.
In the main method, we create a Theatre object and add two audiences using the addAudience method. Then we print the total fare and audience names.
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. Singles2. Couples3. A set of parents + 1 kid4. A set of parents + 2 kidThe 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
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.