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

In Chapter 8, you created a Rental class for Sammy’s Seashore Supplies. Now exte

ID: 3702610 • Letter: I

Question

In Chapter 8, you created a Rental class for Sammy’s Seashore Supplies. Now extend the class to create a LessonWithRental class. In the extended class, include a new Boolean field that indicates whether a lesson is required or optional for the type of equipment rented. Also include a final array that contains Strings representing the names of the instructors for each of the eight equipment types, and store names that you choose in the array. Create a LessonWithRental constructor that requires arguments for an event number, minutes for the rental, and an integer equipment type. Pass the first two parameters to the Rental constructor, and assign the last parameter to the equipment type. For the first two equipment types (jet ski and pontoon boat), set the Boolean lesson required field to true; otherwise, set it to false. Also include a getInstructor() method that builds and returns a String including the String for the equipment type, a message that indicates whether a lesson is required, and the instructor’s name. Save the file as LessonWithRental.java.

The program uses an array of Rental objects and allows the user to sort Rentals in ascending order by contract number, equipment type, or price. Now modify the program to use an array of four LessonWithRental objects. Prompt the user for all values for each object, and then allow the user to continuously sort the LessonWithRental descriptions by contract number, equipment type, or price. Save the file as LessonWithRentalDemo.java.

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 <= 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; } 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 < len; ++x) { if (Character.isDigit(phone.charAt(x))) contactPhone += phone.charAt(x); } if (contactPhone.length() != VALID_LEN) contactPhone = INVALID_PHONE; } public void setEquipType(int eType) { if (eType < EQUIP_TYPES.length) equipType = eType; else equipType = EQUIP_TYPES.length - 1; } 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[3]; int x; for (x = 0; x < rentals.length; ++x) { contractNum = getContractNumber(); minutes = getMinutes(); rentals[x] = new Rental(contractNum, minutes); rentals[x].setContactPhone(getPhone()); rentals[x].setEquipType(getType()); } for (x = 0; x < rentals.length; ++x) displayDetails(rentals[x]); } 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 < LOW_MIN || 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 x = 0; x < Rental.EQUIP_TYPES.length; ++x) System.out.println(" " + x + " " + Rental.EQUIP_TYPES[x]); System.out.print("Enter equipment type >> "); 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; } }

Explanation / Answer

Code

LessonWithRentalDemo

import java.util.Scanner;
public class LessonWithRentalDemo {
public static void main(String[] args) {
String contractNum;
int minutes;
LessonWithRental[] rentals = new LessonWithRental[3];
int x;
for (x = 0; x < rentals.length; ++x) {
contractNum = getContractNumber();
minutes = getMinutes();
int eqipType = getType();
String phone = getPhone();
rentals[x] = new LessonWithRental(contractNum, minutes,eqipTypr);
rentals[x].setContactPhone(phone);

}
for (x = 0; x < rentals.length; ++x) displayDetails(rentals[x]);
}
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 < LOW_MIN || 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 x = 0; x < Rental.EQUIP_TYPES.length; ++x) System.out.println(" " + x + " " + Rental.EQUIP_TYPES[x]);
System.out.print("Enter equipment type >> ");
eType = input.nextInt();
return eType;
}
public static void displayDetails(LessonWithRental 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());
System.out.println(r.getInstructor());
}
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;
}
}

Rental class

class Rental implements Comparable<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 <= 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;
}
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 < len; ++x) {
if (Character.isDigit(phone.charAt(x))) contactPhone += phone.charAt(x);
}
if (contactPhone.length() != VALID_LEN) contactPhone = INVALID_PHONE;
}
public void setEquipType(int eType) {
if (eType < EQUIP_TYPES.length) equipType = eType;
else equipType = EQUIP_TYPES.length - 1;
}
public int getEquipType() {
return equipType;
}
public String getEquipTypeString() {
return EQUIP_TYPES[equipType];
}

@Override
public int compareTo(Rental o) {
// TODO Auto-generated method stub
if(this.contractNumber.compareTo(o.contractNumber) > 0 )
return 1;
else if(this.contractNumber.compareTo(o.contractNumber) < 0 )
return -1;
else
return 0;
}

}

Lessonwithrental

public class LessonWithRental extends Rental{

boolean lessonReq;
final String[] instructorNames = {"Paul","Tom","Peter","John","Steven","Ronaldo","Messi","Hazard"};
public LessonWithRental(String eventnumber,int minutes,int equptype){

super(eventnumber,minutes);
this.setEquipType(equptype);
if(equptype == 0 || equptype == 1){
lessonReq = true;
}else
lessonReq = false;
}
public String getInstructor(){
String output="";

output = "Equipment Type: " + this.EQUIP_TYPES[this.getEquipType()] + (lessonReq ? "Lesson Requires instructor and Instructor name is"
+ " " + instructorNames[this.getEquipType()] : "Lesson does not require instructot");

return output;
}

}

Output


Enter contract number >> C123
Enter minutes >> 200
Equipment types:
0 jet ski
1 pontoon boat
2 rowboat
3 canoe
4 kayak
5 beach chair
6 umbrella
7 other
Enter equipment type >> 0
Enter contact phone number >> 1234567890

Enter contract number >> A123
Enter minutes >> 300
Equipment types:
0 jet ski
1 pontoon boat
2 rowboat
3 canoe
4 kayak
5 beach chair
6 umbrella
7 other
Enter equipment type >> 2
Enter contact phone number >> 0987654321

Contract #A123
For a rental of 5 hours and 0 minutes,
at a rate of $40 per hour and $1 per minute,
the price is $200.0
Contact phone number is: (098) 765-4321
Equipment rented is type #2 rowboat
Equipment Type: rowboatLesson does not require instructot

Contract #C123
For a rental of 3 hours and 20 minutes,
at a rate of $40 per hour and $1 per minute,
the price is $140.0
Contact phone number is: (123) 456-7890
Equipment rented is type #0 jet ski
Equipment Type: jet skiLesson Requires instructor and Instructor name is Paul