Question: In this exercise, you will need to write two methods. A car showroom consists of N numbered spaces. Each space can hold at most one
In this exercise, you will need to write two methods.
A car showroom consists of N numbered spaces. Each space can hold at most one car. The spaces are indexed starting from 0 though the last space, N - 1. No two cars in the showroom are the same model.
Write the CarShowroom method findCarSpace. This method returns the index of the space in which the car with the specified model is located. If there is no car of that model in the showroom, the method returns -1.
Then write the CarShowroom method consolidate. This method should move the cars so that cars in adjacent spaces, starting at index 0, have no empty spaces between any two cars. Keep the order of the cars the same as the array in the code shows. The toString method has been provided to print out filled and unfilled spaces.
Output:
Space 0 has a Mustang with a top speed of 157 Space 1 has a Camaro with a top speed of 155 Space 2 has a null Space 3 has a Corvette with a top speed of 194 Space 4 has a null Space 5 has a null Space 6 has a Porsche with a top speed of 210 Index of Mustang should be 0 and is 0 Index of Corvette should be 3 and is 3 Index of Miata should be -1 and is -1 Original Car Showroom: Space 0 has a Mustang with a top speed of 157 Space 1 has a Camaro with a top speed of 155 Space 2 has a null Space 3 has a Corvette with a top speed of 194 Space 4 has a null Space 5 has a null Space 6 has a Porsche with a top speed of 210 Car Showroom after cars have been consolidated: Space 0 has a Mustang with a top speed of 157 Space 1 has a Camaro with a top speed of 155 Space 2 has a Corvette with a top speed of 194 Space 3 has a Porsche with a top speed of 210 Space 4 has a null Space 5 has a null Space 6 has a null
public class CarShowroom { public static void main (String[] args) { CarShowroom showroom = new CarShowroom(7); showroom.spaces[0] = new CarModel("Mustang", 157); showroom.spaces[1] = new CarModel("Camaro",155); showroom.spaces[3] = new CarModel("Corvette", 194); showroom.spaces[6] = new CarModel("Porsche", 210);
// print out what is in the showroom System.out.println(showroom);
// test output System.out.println("Index of Mustang should be 0 and is " + showroom.findCarSpace("Mustang")); System.out.println("Index of Corvette should be 3 and is " + showroom.findCarSpace("Corvette")); System.out.println("Index of Miata should be -1 and is " + showroom.findCarSpace("Miata")); System.out.println(" Original Car Showroom:"); System.out.println(showroom); showroom.consolidate(); System.out.println("Car Showroom after cars have been consolidated:"); System.out.println(showroom); } private CarModel[] spaces;
// constructor for number of spaces in showroom public CarShowroom(int numParkingSpaces) { spaces = new CarModel[numParkingSpaces]; }
// Returns index of space that contains the car with the specified model // and -1 if no car in the showroom is that model.No two cars in the showroom are the same model.
public int findCarSpace(String name) { // your code goes here } // Consolidates cars in the showroom so that there are no gaps // in the parking spaces so that it's easier to move new models in public void consolidate() { // your code goes here }
public String toString() { String result = ""; CarModel sp = null; // iterate through the spaces for (int i = 0; i < spaces.length; i++) { sp = spaces[i]; result = result + "Space " + i + " has a "; // locate empty spaces if (sp == null) { result = result + " null "; } else result = result + sp.toString() + " "; } return result; } }
public class CarShowroom { public static void main (String[] args) { CarShowroom showroom = new CarShowroom(7); showroom.spaces[0] = new CarModel("Mustang", 157); showroom.spaces[1] = new CarModel("Camaro",155); showroom.spaces[3] = new CarModel("Corvette", 194); showroom.spaces[6] = new CarModel("Porsche", 210);
// print out what is in the showroom System.out.println(showroom);
// test output System.out.println("Index of Mustang should be 0 and is " + showroom.findCarSpace("Mustang")); System.out.println("Index of Corvette should be 3 and is " + showroom.findCarSpace("Corvette")); System.out.println("Index of Miata should be -1 and is " + showroom.findCarSpace("Miata")); System.out.println(" Original Car Showroom:"); System.out.println(showroom); showroom.consolidate(); System.out.println("Car Showroom after cars have been consolidated:"); System.out.println(showroom); } private CarModel[] spaces;
// constructor for number of spaces in showroom public CarShowroom(int numParkingSpaces) { spaces = new CarModel[numParkingSpaces]; }
// Returns index of space that contains the car with the specified model // and -1 if no car in the showroom is that model.No two cars in the showroom are the same model.
public int findCarSpace(String name) { // your code goes here } // Consolidates cars in the showroom so that there are no gaps // in the parking spaces so that it's easier to move new models in public void consolidate() { // your code goes here }
public String toString() { String result = ""; CarModel sp = null; // iterate through the spaces for (int i = 0; i < spaces.length; i++) { sp = spaces[i]; result = result + "Space " + i + " has a "; // locate empty spaces if (sp == null) { result = result + " null "; } else result = result + sp.toString() + " "; } return result; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
