Question

In: Computer Science

Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...

Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental.


///////////////////////////////RentalDemo
package java1;
import java.util.Scanner;
public class RentalDemo {
public static void main(String[] args) {
   Rental object1 = new Rental();
   Rental object2 = new Rental();
   Rental object3 = new Rental();
   String contractNumber;
   int hours;
   int minutes;
   int totalMins;
   Scanner input = new Scanner(System.in);
              System.out.println("RENTAL 1: ");
              System.out.println("Enter contract number: ");
              contractNumber = input.nextLine();
              System.out.println("Enter rental hours: ");
              hours = input.nextInt();
              System.out.println("Enter rental minutes: ");
              minutes = input.nextInt();
              object1.setContractNumber(contractNumber);
              object1.setRentalTimeHours(hours);
              object1.setRentalTimeMins(minutes);
              System.out.println("RENTAL 2: ");input.nextLine();
              System.out.println("Enter contract number: ");
              contractNumber = input.nextLine();
              System.out.println("Enter rental hours: ");
              hours = input.nextInt();
              System.out.println("Enter rental minutes: ");
              minutes = input.nextInt();
              object2.setContractNumber(contractNumber);
              object2.setRentalTimeHours(hours);
              object2.setRentalTimeMins(minutes);
              System.out.println("RENTAL 3: ");input.nextLine();
              System.out.println("Enter contract number: ");
              contractNumber = input.nextLine();
              System.out.println("Enter rental hours: ");
              hours = input.nextInt();
              System.out.println("Enter rental minutes: ");
              minutes = input.nextInt();
              object3.setContractNumber(contractNumber);
              object3.setRentalTimeHours(hours);
              object3.setRentalTimeMins(minutes);
                displayMotto();
              System.out.println();
               displayobject1(object1);
                     displayobject2(object2);
                     displayobject3(object3);
                  System.out.println("Get largest rental between event # 1 and event # 2 is :");
                     displayobject1(getRentalWithLongerTime(object1, object2));
                     System.out.println("Get largest rental between event # 2 and event # 3 is :");
                     displayobject2(getRentalWithLongerTime(object2, object3));
                    System.out.println("Get largest rental from event # 1 and event # 3 is :");
                     displayobject3(getRentalWithLongerTime(object1, object3));
                     System.out.println("");
                   for (int r = 0; r < totalMins; r++) {
                object4[r] = new Rental();
                object4[r].setEventNumber();
                do {
                            System.out.println("RENTAL: ");
                            System.out.println("Enter contract number: ");
                            contractNumber = input.nextLine();
                            System.out.println("Enter the total time of the rental (in minutes): ");
                            totalMins = input.nextInt();
                            object1.setContractNumber(contractNumber);
                            object1.setRentalTimeHours(totalMins);
                            System.out.println("The rental time must be between 60 minutes and 7,200 minutes");
                            }while(!(totalMins >= 60 && totalMins <= 7200));
                   for(int i=0;i                              System.out.println("");
                             System.out.println("!!!!! This coupon is good for 10 percent off your next rental !!!!!"); }}}
                             private static void displayobject1(Rental object1) {
                           System.out.println("Rental Contract number: "+object1.getContractNumber()+", Time: "+object1.getRentalTimeHours()+"h+"+object1.getRentalTimeMins()+"m, Price: $"+object1.calculateRentalPrice());        }
               private static void displayobject2(Rental object2);{
                           System.out.println("Rental Contract number: "+object2.getContractNumber()+", Time: "+object2.getRentalTimeHours()+"h+"+object2.getRentalTimeMins()+"m, Price: $"+object2.calculateRentalPrice());     
                      private static void displayobject3(Rental object3) {
                           System.out.println("Rental Contract number: "+object3.getContractNumber()+", Time: "+object3.getRentalTimeHours()+"h+"+object3.getRentalTimeMins()+"m, Price: $"+object3.calculateRentalPrice());       }
                           public static Rental getRentalWithLongerTime(Rental rental1, Rental rental2){
              double totalTimeRental1 = rental1.getRentalTimeHours()+ rental1.getRentalTimeMins()/60;
              double totalTimeRental2 = rental2.getRentalTimeHours()+ rental2.getRentalTimeMins()/60;
                           if(totalTimeRental1>= totalTimeRental2)
                  return rental1;             
             else
                  return rental2;  }
       public static void displayMotto() {
       System.out.println();
       System.out.println("Motto");}}

public class Rental {
   private String contractNum;
   private int rentalTimeHours;
   private int rentalTimeMins;
   public String getContractNumber() {
   return contractNum;
   }
   public void setContractNumber(String contractNumber) {
   this.contractNum = contractNumber;
   }
   public int getRentalTimeHours() {
   return rentalTimeHours;
   }
   public void setRentalTimeHours(int rentalTimeHours) {
   this.rentalTimeHours = rentalTimeHours;
   }
   public int getRentalTimeMins() {
   return rentalTimeMins;
   }
   public void setRentalTimeMins(int rentalTimeMins) {
   this.rentalTimeMins = rentalTimeMins;
   }
       public double calculateRentalPrice(){
       int pricePerHour = 40;
   int pricePerMinute = 1;
       int minutesThatCount;
   if(this.getRentalTimeMins()<=40){
   minutesThatCount = this.getRentalTimeMins();
   }else{
   minutesThatCount = 40;
   }
   int HoursPrice = this.getRentalTimeHours()*pricePerHour ;
   int MinutesPrice = minutesThatCount*pricePerMinute;
   int totalRentalPrice = HoursPrice + MinutesPrice;
      return totalRentalPrice;
   }
   }

Solutions

Expert Solution

Short Summary:

  • Provided the source code and sample output as per the requirements.

Source Code:

RentalDemo.java:

import java.util.Scanner;

public class RentalDemo {
   public static void main(String[] args) {
       Rental object1 = new Rental();
   Rental object2 = new Rental();
   Rental object3 = new Rental();
   // Create array of rentals with size 3
   Rental rentals[] = new Rental[3];
  
   // Store all the three objects into array
   rentals[0] = object1;
   rentals[1] = object2;
   rentals[2] = object3;
  
   String contractNumber;
   int hours;
   int minutes;
  
  
   Scanner input = new Scanner(System.in);
       System.out.println("RENTAL 1: ");
       System.out.println("Enter contract number: ");
       contractNumber = input.nextLine();
       System.out.println("Enter rental hours: ");
       hours = input.nextInt();
       System.out.println("Enter rental minutes: ");
       minutes = input.nextInt();
      
       object1.setContractNumber(contractNumber);
       object1.setRentalTimeHours(hours);
       object1.setRentalTimeMins(minutes);
      
       System.out.println("RENTAL 2: ");input.nextLine();
       System.out.println("Enter contract number: ");
       contractNumber = input.nextLine();
       System.out.println("Enter rental hours: ");
       hours = input.nextInt();
       System.out.println("Enter rental minutes: ");
       minutes = input.nextInt();
      
       object2.setContractNumber(contractNumber);
       object2.setRentalTimeHours(hours);
       object2.setRentalTimeMins(minutes);
      
       System.out.println("RENTAL 3: ");input.nextLine();
       System.out.println("Enter contract number: ");
       contractNumber = input.nextLine();
       System.out.println("Enter rental hours: ");
       hours = input.nextInt();
       System.out.println("Enter rental minutes: ");
       minutes = input.nextInt();
      
       object3.setContractNumber(contractNumber);
       object3.setRentalTimeHours(hours);
       object3.setRentalTimeMins(minutes);
      
       displayMotto();
       System.out.println();
       displayobject1(object1);
   displayobject2(object2);
   displayobject3(object3);
   System.out.println("Get largest rental between event # 1 and event # 2 is :");
   displayobject1(getRentalWithLongerTime(object1, object2));
   System.out.println("Get largest rental between event # 2 and event # 3 is :");
   displayobject2(getRentalWithLongerTime(object2, object3));
   System.out.println("Get largest rental from event # 1 and event # 3 is :");
   displayobject3(getRentalWithLongerTime(object1, object3));
   System.out.println("");
  
   int totalMins;
   for (int r = 0; r < rentals.length; r++) {
       // get the total mintues until >=60 and <= 7200
           do {
   System.out.println("RENTAL " + (r+1) + ": ");
   System.out.println("Enter the total time of the rental (in minutes): ");
   totalMins = input.nextInt();
   // Display error only when there is error
   if(!(totalMins >= 60 && totalMins <= 7200)) {
       System.out.println("The rental time must be between 60 minutes and 7,200 minutes");
   }
   } while(!(totalMins >= 60 && totalMins <= 7200));
          
           int totalRentalMints = (rentals[r].getRentalTimeHours() * 60) + rentals[r].getRentalTimeMins();
          
           while(totalRentalMints >= totalMins) {
               System.out.println("");
               System.out.println("$" + (rentals[r].calculateRentalPrice() * .10) + " Off");
   System.out.println("!!!!! This coupon is good for 10 percent off your next rental !!!!!");
   totalRentalMints -= totalMins;
   }
   }

   }

   private static void displayobject1(Rental object1) {
       System.out.println(
               "Rental Contract number: " + object1.getContractNumber() + ", Time: " + object1.getRentalTimeHours()
                       + "h+" + object1.getRentalTimeMins() + "m, Price: $" + object1.calculateRentalPrice());
   }

   private static void displayobject2(Rental object2){
   System.out.println("Rental Contract number: "+object2.getContractNumber()+
           ", Time: "+object2.getRentalTimeHours()+"h+"
           +object2.getRentalTimeMins()+"m, Price: $"+object2.calculateRentalPrice());
   }

   private static void displayobject3(Rental object3) {
       System.out.println(
               "Rental Contract number: " + object3.getContractNumber() + ", Time: " + object3.getRentalTimeHours()
                       + "h+" + object3.getRentalTimeMins() + "m, Price: $" + object3.calculateRentalPrice());
   }

   public static Rental getRentalWithLongerTime(Rental rental1, Rental rental2) {
       double totalTimeRental1 = rental1.getRentalTimeHours() + rental1.getRentalTimeMins() / 60;
       double totalTimeRental2 = rental2.getRentalTimeHours() + rental2.getRentalTimeMins() / 60;
       if (totalTimeRental1 >= totalTimeRental2)
           return rental1;
       else
           return rental2;
   }

   public static void displayMotto() {
       System.out.println();
       System.out.println("Motto");
   }
}

Rental.java:

public class Rental {
   private String contractNum;
   private int rentalTimeHours;
   private int rentalTimeMins;
  
   public Rental() {
       contractNum = "";
       rentalTimeHours = 0;
       rentalTimeMins = 0;
   }

   public String getContractNumber() {
       return contractNum;
   }

   public void setContractNumber(String contractNumber) {
       this.contractNum = contractNumber;
   }

   public int getRentalTimeHours() {
       return rentalTimeHours;
   }

   public void setRentalTimeHours(int rentalTimeHours) {
       this.rentalTimeHours = rentalTimeHours;
   }

   public int getRentalTimeMins() {
       return rentalTimeMins;
   }

   public void setRentalTimeMins(int rentalTimeMins) {
       this.rentalTimeMins = rentalTimeMins;
   }

   public double calculateRentalPrice() {
       int pricePerHour = 40;
       int pricePerMinute = 1;
       int minutesThatCount;
       if (this.getRentalTimeMins() <= 40) {
           minutesThatCount = this.getRentalTimeMins();
       } else {
           minutesThatCount = 40;
       }
       int HoursPrice = this.getRentalTimeHours() * pricePerHour;
       int MinutesPrice = minutesThatCount * pricePerMinute;
       int totalRentalPrice = HoursPrice + MinutesPrice;
       return totalRentalPrice;
   }
}

Sample Run:

**************************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

**************************************************************************************


Related Solutions

I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Hello, I am having trouble getting started on my project and building these functions. How do...
Hello, I am having trouble getting started on my project and building these functions. How do I build a function that continuously adds new "slices" to the list if they are below/above the size limit? I didn't copy the entire problem, but just for reference, when the code is run it will take user input for size limit (L), time cost for a random slice(R), and time cost for an accurate slice(A). Question: In real life, a steak is a...
Please see if you can correct my code. I am getting an ReferenceError in Windows Powershell...
Please see if you can correct my code. I am getting an ReferenceError in Windows Powershell that says payment is undefined. I am trying to create a main.js file that imports the function from the hr.js file; call the function passing the necessary arguments and log the result to the console. main.js var Dev = require("./hr.js") const { add } = require("./hr.js") var dev_type = 1; var hr = 40; console.log("your weekly payment is " + payment(dev_type, hr)) dev_type =...
I am getting an error at linen 57 and can't figure out how to fix it....
I am getting an error at linen 57 and can't figure out how to fix it. // Java program to read a CSV file and display the min, max, and average of numbers in it. import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main {     // method to determine and return the minimum number from the array     public static int minimum(int numbers[])     {         int minIdx = 0;         for(int i=1;i<numbers.length;i++)         {             if((minIdx...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
I am making a html game with phaser 3 I keep getting an error at line...
I am making a html game with phaser 3 I keep getting an error at line 53 of this code (expected ;) I have marked the line that needs to be fixed. whenever I add ; my whole code crashes const { Phaser } = require("./phaser.min"); var game; var gameOptions = {     tileSize: 200,     tileSpacing: 20,     boardSize: {     rows: 4,     cols: 4     }    }    window.onload = function() {     var gameConfig = {         width: gameOptions.boardSize.cols * (gameOptions.tileSize +             gameOptions.tileSpacing) + gameOptions.tileSpacing,...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I am having a hard time getting started on how to do this assignment. I would...
I am having a hard time getting started on how to do this assignment. I would like some ideas on how to start the MEMO. and maybe some ideas on what you would include. But I don't necessarily need the assignment completed for me. just need ideas!!! One routine negative message in the form of an internal memo. 400-500 word message. Single-spaced, with 1-inch margins on all sides. Objective: To announce organizational restructuring, one routine negative message addressed to employees....
What is Type I error? How do we correct for Type I error? What happens when...
What is Type I error? How do we correct for Type I error? What happens when we correct for Type I error? What is Type II error? How do we correct for Type II error? What happens when we correct for Type II error? How can we correct for both Type I and Type II error at the same time? Which error is considered the worst type of error to commit?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT