Question
In previous chapters, you have created several classes for Sammys Seashore Supplies. Now, Sammy has decided to restructure his rates to include different fees for
In previous chapters, you have created several classes for Sammys Seashore Supplies. Now, Sammy has decided to restructure his rates to include different fees for equipment types in addition to the fees based on rental length, and to charge for required lessons for using certain equipment. Create an abstract class named Equipment that holds fields for a numeric equipment type, a String equipment name, and a fee for renting the equipment. Include a final array that holds the equipment namesjet ski, pontoon boat, rowboat, canoe, kayak, beach chair, umbrella, and other. Also include a final array that includes the surcharges for each equipment type$50, $40, $15, $12, $10, $2, $1, and $0, respectively. Include a constructor that requires an equipment type and sets the field to the type unless it is out of range, in which case the type is set to the other code. Include get and set methods for each field and include an abstract method that returns a String explaining the lesson policy for the type of equipment. Save the file as Equipment.java.
Create two classes that extend EquipmentEquipmentWithoutLessonand EquipmentWithLesson. The constructor for each class requires that the equipment type be in rangethat is, jet skis, pontoon boats, rowboats, canoes, and kayaks are EquipmentWithLesson objects, but other equipment types are not. In both subclasses, the constructors set the equipment type to other if it is not in range. The constructors also set the equipment fee, as described in part 2a. Each subclass also includes a method that returns a message indicating whether a lesson is required, and the cost ($27) if it is. Save the files as EquipmentWithoutLesson.java and EquipmentWithLesson.java.
In Chapter8, you created a Rental class.Now,modify it to contain an Equipment data field and an additional price field that holds a base price before equipment fees are added. Remove the array of equipment Strings from the Rental class as well as the method that returns an equipment string. Modify the Rental constructor so that it requires three parameters: contract number, minutes for the rental, and an equipment type. The method that sets the hours and minutes now sets a base price before equipment fees are included. Within the constructor, set the contract number and time as before, but add statements to create either an EquipmentWithLesson object or an EquipmentWithoutLesson object, and assign it to the Equipment data field. Assign the sum of the base price (based on time) and the equipment fee (based on the type of equipment) to the price field. Save the file as Rental.java.
InChapter8,you createda RentalDemo class that displays details for four Rental objects. Modify the class as necessary to use the revised Rental class that contains an Equipment field. Be sure to modify the method that displays details for the Rental to include all the pertinent data for the equipment. Figure 11-36 shows the last part of the output from a typical execution. Save the file as RentalDemo.java.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Rental
{
public static final int MINUTES_IN_HOUR = 60; public static final int HOUR_RATE = 40; public static final int CONTRACT_NUM_LENGTH = 4; public static final String[] EQUIP_TYPES = { "jet ski", "pontoon boat", "rowboat", "canoe", "kayak", "beach chair", "umbrella", "other" }; private String contractNumber; private int hours; private int extraMinutes; private double price; private String contactPhone; private int equipType;
public Rental(String num, int minutes) { setContractNumber(num); setHoursAndMinutes(minutes); }
public Rental() { this("A000", 0); }
public void setContractNumber(String num) { boolean numOk = true; if (num.length() != CONTRACT_NUM_LENGTH || !Character.isLetter(num.charAt(0)) || !Character.isDigit(num.charAt(1)) || !Character.isDigit(num.charAt(2)) || !Character.isDigit(num.charAt(3))) contractNumber = "A000";
else contractNumber = num.toUpperCase(); }
public void setHoursAndMinutes(int minutes) { hours = minutes / MINUTES_IN_HOUR; extraMinutes = minutes % MINUTES_IN_HOUR; if (extraMinutes
public String getContractNumber() { return contractNumber; }
public int getHours() { return hours; }
public int getExtraMinutes() { return extraMinutes;
}
public double getPrice() { return price; }
public String getContactPhone() { String phone; phone = "(" + contactPhone.substring(0, 3) + ") " + contactPhone.substring(3, 6) + "-" + contactPhone.substring(6, 10); return phone;
}
public void setContactPhone(String phone) { final int VALID_LEN = 10; final String INVALID_PHONE = "0000000000"; contactPhone = ""; int len = phone.length(); for (int x = 0; x
public void setEquipType(int eType) { if (eType
} public int getEquipType() { return equipType; } public String getEquipTypeString()
{ return EQUIP_TYPES[equipType]; }
}
----------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class RentalDemo
{ public static void main(String[] args)
{
String contractNum; int minutes; Rental[] rentals = new Rental[8]; int i; for (i = 0; i
contractNum = getContractNumber(); minutes = getMinutes(); rentals[i] = new Rental(contractNum, minutes); rentals[i].setContactPhone(getPhone()); rentals[i].setEquipType(getType());
} for (i = 0; i
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()
{
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 HIGH_MIN)
{ System.out.println("Time must be between " + LOW_MIN + " minutes and " + HIGH_MIN + " minutes"); System.out.print("Please reenter minutes >> "); minutes = input.nextInt();
}
return minutes;
}
public static int getType() {
int eType; Scanner input = new Scanner(System.in); System.out.println("Equipment types:"); for (int i = 0; i > "); eType = input.nextInt(); return eType;
}
public static void displayDetails(Rental r)
{
System.out.println(" Contract #" + r.getContractNumber()); System.out.println("For a rental of " + r.getHours() + " hours and " + r.getExtraMinutes() + " minutes, at a rate of $" + r.HOUR_RATE + " per hour and $1 per minute, the price is $" + r.getPrice()); System.out.println("Contact phone number is: " + r.getContactPhone()); System.out.println("Equipment rented is type #" + r.getEquipType() + " " + r.getEquipTypeString());
}
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;
}
public static String getPhone()
{ String phone; Scanner input = new Scanner(System.in); System.out.print("Enter contact phone number >> "); phone = input.nextLine(); return phone;
}
}
Command Prompt Contract #C78? For a rental of 2 hours and 10 ninutes. at a rate of $40 per hou and $1 per extra ninute. the base price is 98.0 Contact phone nunber is (847 912-2255 Equipment rented is type #6 -- unbre lla his type of rental does not require a lesson he equipnent fee with lesson is 1.0 he total price is 91.0 Contract #D1 For a rental of 4 hours and 10 ninutes. 8? at a rate of $40 per hour and $1 per extra ninute. the base price is $178.0 Contact phone nunber is 213 984-8933 his type of rental requires a lesson for $27.0 he equipnent fee with lesson is 67.0 he total price is 237.0 C: Java> Command Prompt Contract #C78? For a rental of 2 hours and 10 ninutes. at a rate of $40 per hou and $1 per extra ninute. the base price is 98.0 Contact phone nunber is (847 912-2255 Equipment rented is type #6 -- unbre lla his type of rental does not require a lesson he equipnent fee with lesson is 1.0 he total price is 91.0 Contract #D1 For a rental of 4 hours and 10 ninutes. 8? at a rate of $40 per hour and $1 per extra ninute. the base price is $178.0 Contact phone nunber is 213 984-8933 his type of rental requires a lesson for $27.0 he equipnent fee with lesson is 67.0 he total price is 237.0 C: Java>
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