Question
a. Sammy's Seashore Supplies rents beach equipment such as kayaks, canoes, beach chairs, and umbrellas to tourists. In Chapters 3 and 4, you created a
a. Sammy's Seashore Supplies rents beach equipment such as kayaks, canoes, beach chairs, and umbrellas to tourists. In Chapters 3 and 4, you created a Rental class for the company.
Now, make the following change to the class:
- Currently, a rental price is calculated as $40 per hour plus $1 for each minute over a full hour. This means that a customer who rents equipment for 41 or more minutes past an hour pays more than a customer who waits until the next hour to return the equipment. Change the price calculation so that a customer pays $40 for each full hour and $1 for each extra minute up to and including 40 minutes.
Save the file as Rental.java.
b. In Chapter 4, you modified the RentalDemo class to demonstrate a Rental object. Now, modify that class again as follows:
- Instantiate three Rental objects, and prompt the user for values for each object. Display the details for each object to verify that the new price calculation works correctly.
- Create a method that accepts two Rental objects and returns the one with the longer rental time. (If the Rentals have the same time, you can return either object.) Call this method three timesonce with each pair of instantiated Rentalsand display the contract number and time in hours and minutes for each argument as well as the contract number of the longer Rental.
Save the file as RentalDemo.java.
Create a zip file of the .java file(s) and submit the assignment.
**Code I have now, which is not giving me the right output. Price is showing the same for all 3 Rental objects and rental minutes is showing 0.**
-- Rental --
import java.util.Scanner; public class Rental {
public final static int minutesInHour = 60; public final static int ratePerHour = 40; public final static int ratePerMin = 1; private String contractNumber; private int rentalMinutes; private int rentalHours; private static int price; public Rental() { this("ABC123", 60); } public Rental(String contractNumber, int rentalMinutes) { contractNumber = contractNumber; rentalMinutes = rentalMinutes; }
private Scanner input = new Scanner(System.in); public void setContractNumber() { System.out.print("What is the contract number? " ); contractNumber = input.nextLine(); } public void setRentalMinutes() { System.out.print("How many minutes did you rent the equipment for? " ); int mins = input.nextInt(); int hours = mins/60; mins %= 60; if(mins > 40) price = ((hours + 1) * ratePerHour); else price = (hours * ratePerHour) + mins; } public String getContractNumber() { return contractNumber; } public int getRentalMinutes() { return rentalMinutes; } public int getPrice() { return price; } }
-- RentalDemo --
public class RentalDemo {
public static void main(String args[]) { Rental rental1 = new Rental(); Rental rental2 = new Rental(); Rental rental3 = new Rental(); System.out.println("Enter the details for the first contract"); rental1.setContractNumber(); rental1.setRentalMinutes(); System.out.println(""); System.out.println("Enter the details for the second contract"); rental2.setContractNumber(); rental2.setRentalMinutes(); System.out.println(""); System.out.println("Enter the details for the third contract"); rental3.setContractNumber(); rental3.setRentalMinutes(); System.out.println(""); display(rental1); display(rental2); display(rental3); System.out.println("The longer rental between contract 1 and 2 is "); display(getLargestContract(rental1, rental2));
System.out.println("The longer rental between contract 2 and 3 is "); display(getLargestContract(rental2, rental3));
System.out.println("The longer rental between contract 1 and 3 is "); display(getLargestContract(rental1, rental3)); } public static void display(Rental rental) { System.out.println("Contract Number: " + rental.getContractNumber()); System.out.println("Total price: $" + rental.getPrice()); System.out.println("Total minutes rented: " + rental.getRentalMinutes()); System.out.println(""); } public static Rental getLargestContract(Rental firstContract, Rental secondContract) {
if (firstContract.getRentalMinutes() > secondContract.getRentalMinutes()) return firstContract; else return secondContract; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started