Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program called RentalRate.java which reads in a date of birth, today’s d

ID: 3709463 • Letter: W

Question

Write a program called RentalRate.java which reads in a date of birth, today’s date and a male/female designation from the user, and will then determine the basic daily and weekly rates for rental of an economy class car.

Rental rate classes are:

o Best rate (male drivers, age 33 – 65 and female drivers, age 30 - 62) -- $40.00 per day, $200.00 per week

o Risk rate 1 (female drivers, age 25 – 29)
– Best rate plus $10.00 per day or best rate plus $55.00 per week.

o Risk rate 2 (male drivers, age 25 – 32) – Risk rate 1 plus $7.00 per day or risk rate 1 plus $30.00 per week.

o Risk rate 3 (male driver, age 66+ and female drivers, age 63+) – Best rate plus $2.00 for each year over age 65 (male) or 62 (female), per day or best rate plus $5.00 for each year over age 65 (male) or 62 (female), per week.

PROGRAM PARTICULARS:

? When the program starts, ask the user for the renter’s gender – m or f.

? If not attempting extra credit, ask the user for today’s date

? Then ask the user for the renter’s month, day and year of birth (also see extra credit.)

? The program will determine the renter’s age and display:

o The renter’s gender and age
o The renter’s rate class
o The renter’s rate per day and per week.

You may assume that user input provided will be valid for the base assignment (see Extra Credit below.) Dates will be entered as individual integer values.

? Your program should be modular. Create separate methods for:

o Calculating age

o Determining the rate class

o Displaying the results.

EXAMPLE RUN:

Thank you.
The female renter is 30 years old.
The rate class is: Best rate - $40.00 per day or $200.00 per week.
I?

Note: If you attempt the extra credit, your sample run will differ from the above listing as you will not need to ask for today’s date, and invalid data will cause an error message to be displayed.

HINTS:

? Be sure to test for ‘borderline’ cases. EXTRA CREDIT:

If you attempt the extra credit portion of the assignment, be sure to include a comment to the grader.

5 points: Use the java.util.Date class to get the current date. ? DEPRECATED!!!
Note: The ‘Date’ class has been replaced with a more capable utility – the ‘Calendar’ class. Use

the Calendar class instead.
You must figure out how to use this Java library API yourself.

Special Note: This assignment will provide you with a pre-written main() method. You must make your code conform to the pre-written main method:

// Rental rates assignment
// Pre-set main for testing (see DEBUG constant)
// Required methods to be added: calcAge(...), calcRateClass(...) and displayResult(...) // Also, insert code into main as indicated.

private static final boolean DEBUG = true;

private static final String BEST_RATE = "Best rate - $40.00 per day or $200.00 per week."; private static final String RISK_RATE_1= "Risk rate 1-$50.00 per day or $255.00 per week."; private static final String RISK_RATE_2= "Risk rate 2-$57.00 per day or $285.00 per week."; private static final String RISK_RATE_3= "Risk rate 3-$%4.2f per day or $%5.2f per week.";

public static void main(String[] args) {

// Establish a 'current' date for testing...

System.out.println("First test case: Renter is not old enough to rent..."); birthMonth = 2;
birthDay = 2;
birthYear = 1991;

gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear); rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Second test case: Renter is barely old enough (57/285)..."); birthMonth = 2;
birthDay = 1;
birthYear = 1991;

gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear); rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Third test case: Renter is 35 and male (40/200)..."); birthMonth = 1;
birthDay = 1;
birthYear = 1981;

gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear); rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Fourth test case: Renter is 35 and female (40/200)..."); birthMonth = 1;

// //

// //

If you're not attempting the EC, get today's date from the user... Your code goes here...

birthDay = 1;
birthYear = 1981;
gender = "f";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear); rateResult = calcRateClass(age, gender);

displayResults(gender, age, rateResult);

System.out.println(" Fifth test case: Renter is 30 and male (57/285)..."); birthMonth = 1;
birthDay = 1;
birthYear = 1986;

gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear); rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Sixth test case: Renter is 30 and female (40/200)..."); birthMonth = 1;
birthDay = 1;
birthYear = 1986;

gender = "f";
age = calcAge(curMonth, curDay, curYear, rateResult = calcRateClass(age, gender); displayResults(gender, age, rateResult);

System.out.println(" Seventh test case: birthMonth = 1;
birthDay = 1;
birthYear = 1940;

gender = "m";
age = calcAge(curMonth, curDay, curYear, rateResult = calcRateClass(age, gender); displayResults(gender, age, rateResult);

birthMonth, birthDay, birthYear);

birthMonth, birthDay, birthYear);

System.out.println(" Eighth test case: Renter is 76 and female (68/270)..."); birthMonth = 1;
birthDay = 1;
birthYear = 1940;

gender = "f";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear); rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

}

else

{
Scanner kb = new Scanner(System.in); System.out.println("Welcome to the car renter's rate finder.");

EC, use the Calendar class to get today's date...

// Get age...

age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);

rateResult = calcRateClass(age, gender);

displayResults(gender, age, rateResult); }

} }

Explanation / Answer


Given below is the code for the question. The extra credit part is done. Also you can change the DEBUG = true to test the various test cases. If DEBUG = false, then it will prompt for data from user. It will not prompt for current date since extra credit part is implemented and the program gets the current date from Calendar.

To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


// Rental rates assignment
// Pre-set main for testing (see DEBUG constant)
// Required methods to be added: calcAge(...), calcRateClass(...) and displayResult(...) // Also, insert code into main as indicated.

import java.util.*;

public class RentalRates {
private static final boolean DEBUG = true;

private static final String BEST_RATE = "Best rate - $40.00 per day or $200.00 per week.";
private static final String RISK_RATE_1 = "Risk rate 1-$50.00 per day or $255.00 per week.";
private static final String RISK_RATE_2 = "Risk rate 2-$57.00 per day or $285.00 per week.";
private static final String RISK_RATE_3 = "Risk rate 3-$%4.2f per day or $%5.2f per week.";

public static void main(String[] args) {

int curMonth = 0;
int curDay = 0;
int curYear = 0;
int birthMonth = 0;
int birthDay = 0;
int birthYear = 0;
String gender = "";
int age = 0;
String rateResult;
// Testing mode...
if (DEBUG == true)
{
// Establish a 'current' date for testing...

curMonth = 2;
curDay = 1;
curYear = 2016;
System.out.println("First test case: Renter is not old enough to rent...");
birthMonth = 2;
birthDay = 2;
birthYear = 1991;

gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Second test case: Renter is barely old enough (57/285)...");
birthMonth = 2;
birthDay = 1;
birthYear = 1991;

gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Third test case: Renter is 35 and male (40/200)...");
birthMonth = 1;
birthDay = 1;
birthYear = 1981;

gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear); rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Fourth test case: Renter is 35 and female (40/200)...");
birthMonth = 1;

// //
// //

//If you're not attempting the EC, get today's date from the user... Your code goes here...

birthDay = 1;
birthYear = 1981;
gender = "f";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Fifth test case: Renter is 30 and male (57/285)...");
birthMonth = 1;
birthDay = 1;
birthYear = 1986;
gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Sixth test case: Renter is 30 and female (40/200)...");
birthMonth = 1;
birthDay = 1;
birthYear = 1986;

gender = "f";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Seventh test case: Renter is 76 and male (62/255)...");
birthMonth = 1;
birthDay = 1;
birthYear = 1940;

gender = "m";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

System.out.println(" Eighth test case: Renter is 76 and female (68/270)...");
birthMonth = 1;
birthDay = 1;
birthYear = 1940;

gender = "f";
age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
rateResult = calcRateClass(age, gender);
displayResults(gender, age, rateResult);

}

else

{
Scanner kb = new Scanner(System.in); System.out.println("Welcome to the car renter's rate finder.");

//If you are attempting the
//Your code goes here...
//EC, use the Calendar class to get today's date...
Calendar cal = Calendar.getInstance();
curDay = cal.get(Calendar.DAY_OF_MONTH);
curMonth = cal.get(Calendar.MONTH);
curYear = cal.get(Calendar.YEAR);

// Get the gender...

System.out.print("Please enter the renter’s gender (m/f): ");
gender = kb.next();
// Get the date of birth...
System.out.print("Please enter the renter’s date of birth (mm dd yyyy): ");
birthMonth = kb.nextInt();
birthDay = kb.nextInt();
birthYear = kb.nextInt();

// Get age...

age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);

// Get the rental rate...
rateResult = calcRateClass(age, gender);

// Display the results...
displayResults(gender, age, rateResult); }

}

private static void displayResults(String gender, int age, String rateResult) {
String s = "";
if(gender.equalsIgnoreCase("m"))
s = "male";
else
s = "female";

System.out.printf("The %s renter is %d years old. ", s, age);
System.out.println("The rate class is: " + rateResult);
}

private static String calcRateClass(int age, String gender) {
if(age < 25)
return "Sorry, the renter is not 25 years of age or older.";

if(gender.equalsIgnoreCase("m"))
{
if(age <= 32)
return RISK_RATE_2;
else if(age <= 65)
return BEST_RATE;
else
return RISK_RATE_3;
}
else
{
if(age <= 29)
return RISK_RATE_1;
else if(age <= 62)
return BEST_RATE;
else
return RISK_RATE_3;
}
}

private static int calcAge(int curMonth, int curDay, int curYear, int birthMonth, int birthDay, int birthYear) {
int age = curYear - birthYear;

//check for conditions when the birthday for the current year is not yet over
if(birthMonth > curMonth)
age = age-1;
else if(birthMonth == curMonth && birthDay > curDay)
age = age - 1;

return age;
}
}

output( DEBUG = false)
--------------------------
Welcome to the car renter's rate finder.
Please enter the renter’s gender (m/f): m
Please enter the renter’s date of birth (mm dd yyyy): 10 4 2011
The male renter is 6 years old.
The rate class is: Sorry, the renter is not 25 years of age or older.


output( DEBUG = true)
-------------
First test case: Renter is not old enough to rent...
The male renter is 24 years old.
The rate class is: Sorry, the renter is not 25 years of age or older.

Second test case: Renter is barely old enough (57/285)...
The male renter is 25 years old.
The rate class is: Risk rate 2-$57.00 per day or $285.00 per week.

Third test case: Renter is 35 and male (40/200)...
The male renter is 35 years old.
The rate class is: Best rate - $40.00 per day or $200.00 per week.

Fourth test case: Renter is 35 and female (40/200)...
The female renter is 35 years old.
The rate class is: Best rate - $40.00 per day or $200.00 per week.

Fifth test case: Renter is 30 and male (57/285)...
The male renter is 30 years old.
The rate class is: Risk rate 2-$57.00 per day or $285.00 per week.

Sixth test case: Renter is 30 and female (40/200)...
The female renter is 30 years old.
The rate class is: Best rate - $40.00 per day or $200.00 per week.

Seventh test case: Renter is 76 and male (62/255)...
The male renter is 76 years old.
The rate class is: Risk rate 3-$%4.2f per day or $%5.2f per week.

Eighth test case: Renter is 76 and female (68/270)...
The female renter is 76 years old.
The rate class is: Risk rate 3-$%4.2f per day or $%5.2f per week.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote