Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now modify the Rental and RentalDemo classes as follows: -Modify

In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now modify the Rental and RentalDemo classes as follows:

-Modify the Rental class to include an integer field that holds an equipment type. Add a final String array that holds names of the types of equipment that Sammy's rents -- jet ski, pontoon boat, rowboat, canoe, kayak, beach chair, umbrella, and other. Include get and set methods for the integer equipment type field. If the argument passed to the method that sets the equipment type is larger than the size of the array of String equipment types, then set the integer to the element number occupied by "other". Include a get method that returns a rental's String equipment type based on the numeric equipment type.

-To keep the RentalDemo class simple, remove all the statements that compare rental times and that display the coupon Strings.

-Modify the RentalDemo class so that instead of creating three single Rental objects, it uses an array of three Rental objects. Get data for each of the objects, and then display all the details for each object.

Save the files as Rental.java and RentalDemo.java.

Java Programming 8th Edition

Chapter 8 Case Problem 2

***Rental.java***

class Rental { public static final int MINUTES_IN_HOUR = 60; public static final double HOUR_RATE = 40; private String contractNumber; private int hours; private int extraMinutes; private double price; public Rental(String num, int minutes) { setContractNumber(num); setHoursAndMinutes(minutes); } public Rental() { this("A000", 0); } public void setContractNumber(String num) { contractNumber = num; } public void setHoursAndMinutes(int minutes) { hours = minutes / MINUTES_IN_HOUR; extraMinutes = minutes % MINUTES_IN_HOUR; if(extraMinutes <= HOUR_RATE) price = hours * HOUR_RATE + extraMinutes; else price = hours * HOUR_RATE + HOUR_RATE; } public String getContractNumber() { return contractNumber; } public int getHours() { return hours; } public int getExtraMinutes() { return extraMinutes; } public double getPrice() { return price; } }

***RentalDemo.java***

import java.util.Scanner; public class RentalDemo { public static void main(String[] args) { String contractNum; int minutes;

// Get Contract Numbers and instantiate the objects, r1, r2, r3 contractNum = getContractNumber(); minutes = getMinutes(); Rental r1 = new Rental(contractNum, minutes); contractNum = getContractNumber(); minutes = getMinutes(); Rental r2 = new Rental(contractNum, minutes); contractNum = getContractNumber(); minutes = getMinutes(); Rental r3 = new Rental(contractNum, minutes);

// Display Details displayDetails(r1); displayDetails(r2); displayDetails(r3);

// Compare rental objects to see which has a longer duration

System.out.println("Of Contract #" + r1.getContractNumber() + " with a time of " + r1.getHours() + " hours and " + r1.getExtraMinutes() + " minutes, and Contract #" + r2.getContractNumber() + " with a time of " + r2.getHours() + " hours and " + r2.getExtraMinutes() + " minutes, the one with the longer time is Contract #" + getLongerRental(r1, r2).getContractNumber()); System.out.println("Of Contract #" + r1.getContractNumber() + " with a time of " + r1.getHours() + " hours and " + r1.getExtraMinutes() + " minutes, and Contract #" + r3.getContractNumber() + " with a time of " + r3.getHours() + " hours and " + r3.getExtraMinutes() + " minutes, the one with the longer time is Contract #" + getLongerRental(r1, r3).getContractNumber()); System.out.println("Of Contract #" + r2.getContractNumber() + " with a time of " + r2.getHours() + " hours and " + r2.getExtraMinutes() + " minutes, and Contract #" + r3.getContractNumber() + " with a time of " + r3.getHours() + " hours and " + r3.getExtraMinutes() + " minutes, the one with the longer time is Contract #" + getLongerRental(r2, r3).getContractNumber());

// Set up the coupon value

int hoursInRental = r1.getHours();

for(int x = 0; x < hoursInRental; ++x) System.out.println("Coupon good for 10 percent off next rental");

} public static String getContractNumber() { String num; Scanner input = new Scanner(System.in); System.out.print("Enter contract number >> "); num = input.nextLine(); return num; } public static int getMinutes() // Add logic to check to see if minutes are between 60 and 7200 // Uses constants to control lower and upper limits { int minutes; final int LOW_MIN = 60; final int HIGH_MIN = 7200; Scanner input = new Scanner(System.in); System.out.print("Enter minutes >> "); minutes = input.nextInt();

while(minutes < LOW_MIN || minutes > HIGH_MIN) { System.out.println("Time must be between " + LOW_MIN + " minutes and " + HIGH_MIN + " minutes"); System.out.print("Please re-enter minutes >> "); minutes = input.nextInt(); } return minutes; } public static void displayDetails(Rental r) { System.out.println(" Contract #" + r.getContractNumber()); System.out.print("For a rental of " + r.getHours() + " hours and " + r.getExtraMinutes()); System.out.printf(" minutes, at a rate of $%.2f",r.HOUR_RATE); System.out.printf(" the price is $%.2f ",r.getPrice()); } public static Rental getLongerRental(Rental r1, Rental r2) { Rental longer = new Rental(); int minutes1; int minutes2; minutes1 = r1.getHours() * Rental.MINUTES_IN_HOUR + r1.getExtraMinutes(); minutes2 = r2.getHours() * Rental.MINUTES_IN_HOUR + r2.getExtraMinutes(); if(minutes1 >= minutes2) longer = r1; else longer = r2; return longer; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started