Question

Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...

Complete the // TODO sections in the EasyRental class.

  1. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest)
  2. Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1

You are comparing Strings in an object not integers.

Ex. If the input is:

brown red white blue black -1

the output is:

Enter the colors of the vehicles you would like to search for on the same line followed by a -1

There are no rentals in brown
There are 7 compact Jeep's in red
There are no rentals in white
There are 5 economy Fiat's in blue
There are 5 compact Scion's in black

Done

CarRentals.java

import java.util.Scanner;

public class CarRentals {

   public static void main(String[] args) {
       Scanner scrn = new Scanner(System.in);
       EasyRental ezRent = new EasyRental();
      
       // this section passes the make, the color, the size, and the number of vehicles in stock
       // to the addVehicle method which adds the data to an arrayList in the EasyRental class
       ezRent.addVehicle("Ford", "orange", "mid-size", 3);
       ezRent.addVehicle("Fiat", "blue", "economy", 5);
       ezRent.addVehicle("Toyota", "grey", "compact", 6);
       ezRent.addVehicle("Saab", "canary", "mid-size", 2);
       ezRent.addVehicle("Nissan", "silver", "economy", 1);
       ezRent.addVehicle("Jeep", "red", "compact", 7);
       ezRent.addVehicle("Honda", "indigo", "economy", 4);
       ezRent.addVehicle("Dodge", "tan", "mid-size", 1);
       ezRent.addVehicle("Chevy", "turquoise", "full-size", 2);
       ezRent.addVehicle("Scion", "black", "compact", 5);

       // sort the data based on color
       ezRent.sort();
          
       System.out.println("Enter the colors of the vehicles you would like to search for on the same line followed by a -1");
       String color = scrn.next();
       System.out.println();
       while(!color.equals("-1")) {
           int index = ezRent.binarySearch(color);
           if(index == -1) {
               System.out.println("There are no rentals in " + color);
           }
           else {
               System.out.println(ezRent.printVehicleInfo(index));
           }
           color = scrn.next();
       }
       System.out.println();
       System.out.println("Done");
   }

}

EasyRental.java

import java.util.ArrayList;

public class EasyRental {

   private ArrayList<Rentals> rents = new ArrayList<Rentals>();

   public void addVehicle(String make, String color, String size, int amount) {
       rents.add(new Rentals(make, color, size, amount));

   }

   public String printVehicleInfo(int index) {
       return rents.get(index).toString();
   }

   // TODO 1. create an iterative sort method that sorts the rentals by color


   // TODO 2. create a Binary Search method below to search for a vehicle by a
   // color, that should return the index where the vehicle was found or -1

   public void printVehicles() {
       for (Rentals r : rents) {
           System.out.println(r.toString());
       }
       System.out.println();
   }

}

Rentals.java

public class Rentals {
  
   private String make;
   private String color;
   private String size;
   private int count;
  
   public Rentals(String make, String color, String size, int count) {
       this.make = make;
       this.color = color;
       this.size = size;
       this.count = count;
   }
  
   public String getMake() {
       return make;
   }

   public void setMake(String make) {
       this.make = make;
   }

   public String getColor() {
       return color;
   }

   public void setColor(String color) {
       this.color = color;
   }

   public String getSize() {
       return size;
   }

   public void setSize(String size) {
       this.size = size;
   }

   public int getCount() {
       return count;
   }

   public void setCount(int count) {
       this.count = count;
   }


   public String toString() {
       return "There are " + count + " " + size + " " + make + "'s" + " in " + color ;
   }
  
}

Homework Answers

Answer #1

//CarRentals.java

import java.util.Scanner;

public class CarRentals {

public static void main(String[] args) {
Scanner scrn = new Scanner(System.in);
EasyRental ezRent = new EasyRental();
  
// this section passes the make, the color, the size, and the number of vehicles in stock
// to the addVehicle method which adds the data to an arrayList in the EasyRental class
ezRent.addVehicle("Ford", "orange", "mid-size", 3);
ezRent.addVehicle("Fiat", "blue", "economy", 5);
ezRent.addVehicle("Toyota", "grey", "compact", 6);
ezRent.addVehicle("Saab", "canary", "mid-size", 2);
ezRent.addVehicle("Nissan", "silver", "economy", 1);
ezRent.addVehicle("Jeep", "red", "compact", 7);
ezRent.addVehicle("Honda", "indigo", "economy", 4);
ezRent.addVehicle("Dodge", "tan", "mid-size", 1);
ezRent.addVehicle("Chevy", "turquoise", "full-size", 2);
ezRent.addVehicle("Scion", "black", "compact", 5);

// sort the data based on color
ezRent.sort();
  
System.out.println("Enter the colors of the vehicles you would like to search for on the same line followed by a -1");
String color = scrn.next();
System.out.println();
while(!color.equals("-1")) {
int index = ezRent.binarySearch(color);
if(index == -1) {
System.out.println("There are no rentals in " + color);
}
else {
System.out.println(ezRent.printVehicleInfo(index));
}
color = scrn.next();
}
System.out.println();
System.out.println("Done");
}

}

//EasyRental.java

import java.util.ArrayList;

public class EasyRental {

private ArrayList<Rentals> rents = new ArrayList<Rentals>();

public void addVehicle(String make, String color, String size, int amount) {
rents.add(new Rentals(make, color, size, amount));

}

public String printVehicleInfo(int index) {
return rents.get(index).toString();
}

//iterative sort method that sorts the rentals by color
public void sort()
{
        for(int i=0; i<rents.size()-1; i++){
            for(int j=i+1; j<rents.size(); j++){
                if(rents.get(i).getColor().compareTo(rents.get(j).getColor())>0){
                    Rentals r = rents.get(i);
                    rents.set(i, rents.get(j));
                    rents.set(j, r);
                }
            }
        }      
}

// Binary Search method below to search for a vehicle by a
// color, that should return the index where the vehicle was found or -1
int binarySearch(String color)
{
        int i=0, j=rents.size()-1;
        while(i<=j){
            int mid = (i+j)/2;
            if(rents.get(mid).getColor().compareTo(color)==0)
                return mid;
            if(rents.get(mid).getColor().compareTo(color)<0)
                i = mid+1;
            else
                j = mid-1;
        }
        return -1;
       
}

public void printVehicles() {
for (Rentals r : rents) {
System.out.println(r.toString());
}
System.out.println();
}

}

//Rentals.java

public class Rentals {
  
private String make;
private String color;
private String size;
private int count;
  
public Rentals(String make, String color, String size, int count) {
this.make = make;
this.color = color;
this.size = size;
this.count = count;
}
  
public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}


public String toString() {
return "There are " + count + " " + size + " " + make + "'s" + " in " + color ;
}
  
}

Output:

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
I have a 2 class code and it works everything is fine and runs as it...
I have a 2 class code and it works everything is fine and runs as it supposed too. What will the UML be for both classes. Here's the code, any help will be awsome, Thanks. import java.util.Scanner; public class PayRollDemo { public static void main(String[] args) { double payRate; int hours; PayRoll pr = new PayRoll(); Scanner keyboard = new Scanner(System.in); for (int index = 0; index < 7 ; index++ ) { System.out.println(); System.out.println("EmployeeID:" + pr.getEmployeeID(index)); System.out.println(); System.out.println("Enter the...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT