0% found this document useful (0 votes)
144 views2 pages

Java Slab Rent Calculation Program

The document describes a Java program that calculates the rent for renting a CD based on the number of days. It defines a method 'calculateRent' which computes the total rent using a slab system: Rs. 10 for the first 2 days, Rs. 12 for the next 3 days, and Rs. 15 for any days beyond 5. The main method takes user input for the number of days and displays the total rent calculated by the method.

Uploaded by

revathyrenjit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views2 pages

Java Slab Rent Calculation Program

The document describes a Java program that calculates the rent for renting a CD based on the number of days. It defines a method 'calculateRent' which computes the total rent using a slab system: Rs. 10 for the first 2 days, Rs. 12 for the next 3 days, and Rs. 15 for any days beyond 5. The main method takes user input for the number of days and displays the total rent calculated by the method.

Uploaded by

revathyrenjit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Slab calculation programs

Write a program in Java to input the number of days a CD is rented.

Define a method calculateRent(int days) that receives the number of days as an argument and
returns the total rent. The rent is calculated as follows:

 For the first 2 days: Rs. 10 per day

 For the next 3 days: Rs. 12 per day

 For all days beyond 5: Rs. 15 per day

Calculate and return the total rent to the main method, where it should be displayed.

import [Link];

public class CDRent


{

public static int calculateRent(int days)


{
int rent = 0;

if (days <= 2)
{
rent = days * 10;
} else if (days <= 5)
{
rent = (2 * 10) + (days - 2) * 12;
} else
{
rent = (2 * 10) + (3 * 12) + (days - 5) * 15;
}

return rent;
}

public static void main()


{
Scanner sc = new Scanner([Link]);
[Link]("Enter number of days CD is rented: ");
int days = [Link]();

int totalRent = calculateRent(days);

[Link]("Total rent for " + days + " days is: Rs. " +
totalRent);
}
}

🧾 Variable Description

Variable Name Data Type Description

sc Scanner Scanner object to take input from the user

days int Number of days the CD was rented

rent int Calculated rent based on slab

Rent returned by the method and displayed


totalRent int
in main()

Common questions

Powered by AI

The Java code dynamically adapts to different inputs by using the 'calculateRent' method that accepts an integer 'days' from the user's input via the 'Scanner' class. The method uses conditional if-else logic to apply different per-day rates based on the input value. This adaptability allows it to compute the correct total rental amount for any number of days entered by the user, ensuring the program can handle various scenarios without modification to the logic itself .

The 'calculateRent' method is crucial as it encapsulates the logic for calculating the rental cost. It accepts an integer 'days' as a parameter and initializes the rent variable. Based on the number of days, it uses conditional logic structured through if-else statements to apply the correct rental rate for various day ranges: Rs. 10 per day for the first two days, Rs. 12 per day for days three through five, and Rs. 15 per day for any days beyond five. The method then returns the computed rent, which the main method uses to display the result, thereby separating the logic from the input/output operations and allowing for cleaner code and easier maintenance .

The program can be refactored for better scalability and maintainability by encapsulating CD rental logic into a separate class, such as 'CDRental', with attributes for rental rates and methods to set or get these rates. Implementing this design follows object-oriented principles, promoting encapsulation and separation of concerns. This allows easy adjustment of rental policies or addition of new features, like special discounts or membership-based rates, without affecting the main program logic. By setting default rates as class constants or through a configuration file, changes can be propagated easily throughout instance methods .

The 'Scanner' class is used to handle user input in the Java program. It creates an object 'sc' which reads input from the standard input stream (keyboard). By calling 'sc.nextInt()', the program reads an integer value which represents the number of days the CD is rented. This input procedure allows the program to be dynamic and flexible, enabling user interaction and the program to calculate different rental costs based on varying input days .

Removing or altering the input prompt within the 'main' method would affect program usability by making it less intuitive. The prompt guides users to input the number of days the CD is rented, thus serving a crucial user interface role in making the application interactive and user-friendly. Altering the prompt to be less descriptive could lead to confusion, resulting in incorrect input and a potential decrease in user satisfaction. Ensuring clear instructions directly correlates with effective program interaction and usability .

Using conditional statements in the form of if-else chains is an efficient and straightforward way to implement logic that depends on several exclusive conditions. This design choice improves program readability by clearly showing which computations apply to which ranges of days. It allows a direct mapping of the problem's requirements into code: charging rates segment by how many days have been rented, which makes the code easy to follow and maintain. However, while efficient for this small scale, more complex scenarios might better utilize switch-case structures or look-up tables for improved scalability .

If the rental service introduces a discount for weekends, the 'calculateRent' method would need to account for which days the rental period includes. To modify the method, it would require additional parameters or pre-processed inputs to indicate weekend days. Additional logic would be necessary to apply a different rate for weekend days or compute a percentage discount off the total rent. This could involve integrating a calendar API to check day types or a more sophisticated data structure that flags days as weekdays or weekends, thereby increasing the complexity but providing more functionality .

Adjusting the rate from Rs. 15 to Rs. 13 for days beyond the fifth in the 'calculateRent' method would decrease the total rent cost for rentals exceeding five days. The change would require editing the else block in the conditional logic from '(days - 5) * 15' to '(days - 5) * 13'. This adjustment would directly reduce the total rental cost, making extended rentals more economically attractive and could potentially increase customer retention for longer rental periods .

The program calculates the total rental cost based on the slabs: for the first 2 days, it charges Rs. 10 per day; for days 3 to 5, it charges Rs. 12 per day; and for any additional days beyond 5, it charges Rs. 15 per day. It uses a method 'calculateRent(int days)' which applies conditional logic to determine which slab rates to apply and then sums the charges accordingly. This total is calculated within the method using if-else statements and is returned to the main method where it is printed .

Potential edge cases include input values such as 0 or negative numbers, which do not logically apply to rental situations but are not explicitly handled by the current logic. Addressing these would involve introducing checks at the beginning of the 'calculateRent' method to verify that 'days' is a positive integer greater than zero. If not, the program should prompt the user to enter a valid number of days or return an appropriate error message, thus ensuring robustness against invalid data entry .

You might also like