Question

In: Computer Science

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

These are the given classes with code:

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 ;
   }
  
}

Solutions

Expert Solution

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 ;
           }
          
        }

EasyRentals.java





import java.util.ArrayList;
import java.util.*;

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
   
   public void sort() {
           
           // using Collections.sort() with Comparator to caompare the colors
           Collections.sort(rents, new Comparator<Rentals>() {

           @Override
           public int compare(Rentals o1, Rentals o2) {
               String color1 = o1.getColor();
               String color2 = o2.getColor();
               return color1.compareTo(color2);
           }
           });
           
   }
   
   
   // 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
   
   int binarySearch(String color)
   {
           int p=0, q=rents.size()-1;
           while(p<=q){
               int mid = (p+q)/2;
               if(rents.get(mid).getColor().compareTo(color)==0)
                   return mid;
               if(rents.get(mid).getColor().compareTo(color)<0)
                   p = mid+1;
               else
                   q = mid-1;
           }
           return -1;
          
   }
  
   public void printVehicles() {
       for (Rentals r : rents) {
           System.out.println(r.toString());
       }
       System.out.println();
   }

}

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();
       /* Test Method to check whether sort is working fine or not*/
      // ezRent.printVehicles();
          
       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");
   }

}

output of the program:

End of solution


Related Solutions

USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics {...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics { /** * Calculates and returns the complement of a DNA sequence. In DNA sequences, 'A' and 'T' are * complements of each other, as are 'C' and 'G'. The complement is formed by taking the * complement of each symbol (e.g., the complement of "GTCA" is "CAGT"). * * @param dna a char array representing a DNA sequence of arbitrary length, * containing only...
Write an iterative method printHighEarners() to print all high earner employees. Method printHighEarners()is in the class...
Write an iterative method printHighEarners() to print all high earner employees. Method printHighEarners()is in the class LinkedList. LinkedList has private instance variable list of Node type. Class Node is private inner class inside of class LinkedList, and has public instance variables: data and next of Employee and Node type, respectively. In addition, class Node has constructor and toString method. See LinkedList class bellow on the right. Class Employee has getters for all data, method String toString() that returns string of...
Write an iterative method printHighEarners() to print all high earner employees. Method printHighEarners()is in the class...
Write an iterative method printHighEarners() to print all high earner employees. Method printHighEarners()is in the class LinkedList. LinkedList has private instance variable list of Node type. Class Node is private inner class inside of class LinkedList, and has public instance variables: data and next of Employee and Node type, respectively. In addition, class Node has constructor and toString method. See LinkedList class bellow on the right. Class Employee has getters for all data, method String toString() that returns string of...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class described below. Each class is worth 50 points. When you are finished, zip the project folder and submit it. The Headphone class represents a headphone. It stores the headphone’s manufacturer: manufacturer, name: name, quantity: quantity, number of times restocked: timesRestocked. Write the Headphone class according to the following requirements. Add the four fields to the Headphone class. Create a constructor that takes two parameters....
Complete the following class UML design class diagram by filling in all the sections based on...
Complete the following class UML design class diagram by filling in all the sections based on the information given below.         The class name is Boat, and it is a concrete entity class. All three attributes are private strings with initial null values. The attribute boat identifier has the property of “key.” The other attributes are the manufacturer of the boat and the model of the boat. Provide at least two methods for this class. Class Name: Attribute Names: Method...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine class. Definition of classes from the implementation of the classes should be split. The program is made of five files: Lion.h, Lion.cpp, Pine.h, Pine.cpp, and TestLionPine.cpp. The components of Lion class are defined in the Lion.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written...
irst, you will complete a class called HazMath (in the HazMath.java file) that implements the interface...
irst, you will complete a class called HazMath (in the HazMath.java file) that implements the interface Mathematical (in the Mathematical.java file). These should have the following definitions: public boolean isPrime(int n) You cannot change the signature for the method. This method is similar to the ones we've discussed in the lab and will return true or false depending on if the passed in integer values is prime or not, respectively. Return false if the invoker passes in a number less...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT