Write a program in Java using a method Discount( ), to calculate a single discount or a successive discount. Use overload methods Discount(int), Discount(int,int) and Discount(int,int,int) to calculate single discount and successive discount respectively. Calculate and display the amount to be paid by the customer after getting discounts on the printed price of an article.
Question
Write a program in Java using a method Discount( ), to calculate a single discount or a successive discount. Use overload methods Discount(int), Discount(int,int) and Discount(int,int,int) to calculate single discount and successive discount respectively. Calculate and display the amount to be paid by the customer after getting discounts on the printed price of an article.
Solution
Here is a simple Java program that uses method overloading to calculate single and successive discounts:
public class Main {
// Method to calculate single discount
static double Discount(double price, double discount){
return price - (price * discount / 100);
}
// Method to calculate two successive discounts
static double Discount(double price, double discount1, double discount2){
double afterFirstDiscount = price - (price * discount1 / 100);
return afterFirstDiscount - (afterFirstDiscount * discount2 / 100);
}
// Method to calculate three successive discounts
static double Discount(double price, double discount1, double discount2, double discount3){
double afterFirstDiscount = price - (price * discount1 / 100);
double afterSecondDiscount = afterFirstDiscount - (afterFirstDiscount * discount2 / 100);
return afterSecondDiscount - (afterSecondDiscount * discount3 / 100);
}
public static void main(String[] args) {
double price = 1000;
System.out.println("Price after single discount: " + Discount(price, 10));
System.out.println("Price after two successive discounts: " + Discount(price, 10, 20));
System.out.println("Price after three successive discounts: " + Discount(price, 10, 20, 30));
}
}
In this program, we have three overloaded methods named Discount(). Each method calculates the price after applying the discount(s) and returns the final price. The first method applies a single discount, the second method applies two successive discounts, and the third method applies three successive discounts. The main() method demonstrates how to use these methods.
Similar Questions
Discount Calculator:Build a program that calculates the discounted price of an item based on the original price and a discount percentage. If the original price is greater than 1000, apply a 10% discount; otherwise, apply a 5% discount.
Write a java program to perform the concept of method overriding for calculating simple interest of 3 banks A,B,C., Bank A’s rate of interest-10%, Bank’s B rate of interest-9%, Bank’C rate of interest -7%.
Write a Java program that has three interfaces1. Academic Fees with a method calcAA() which takes the number of course as input parameter to calculate the academic fees by assigning Rs.500/- per course.2. Hostel Fees with a method calcHF() which takes the number of days as input parameter to calculate the hostel fees by assigning Rs.50/- per day.3. Extracurricular Fees with a method calcECF() which takes the number of extra-curricular course as input parameter to calculate the extra-curricular fees by assigning Rs.600/- per extra-curricular course.Create classes and objects using interface to calculate the total fees for1. A general day scholar student involved in no extracurricular courses2. A general hosteller student involved in no extracurricular courses3. A scholar student involved in extracurricular courses4. A hosteler student involved in extracurricular courses
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
Consider a ticket reservation system that allows users to book tickets for various destinations. Each ticket has details such as passenger name, destination, ticket price, and passenger age. The ticket prices are calculated based on the passenger's age, with children (age <= 12) receiving a 50% discount, senior citizens (age >= 60) receiving a 30% discount, and others paying the regular price. The system also tracks the confirmation status of each ticket.Design a Java program that implements this ticket reservation system. The program should prompt the user to input details for multiple tickets, including passenger name, destination, ticket price, and passenger age. It should then calculate and display the total amount of tickets booked for confirmed tickets alone, considering the discounted prices based on passenger age.Write the Java code for the ticket reservation system and demonstrate its functionality with a sample input/output scenario.Sample Input: 3 //No of tickets to book Ravi // NameDelhi // Destination2000 // Price70 // Age Manu // NameDelhi // Destination2000 // Price9 // Age Priya // NameDelhi // Destination2000 // Price40 // Ageconfirm // Do you want to confirm or cancel ticket 1? (confirm/cancel)cancel // Do you want to confirm or cancel this ticket 2? (confirm/cancel)confirm // Do you want to confirm or cancel this ticket 3? (confirm/cancel)Sample Output:3400.0 // Total Amount of Confirmed Tickets
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.