Garage.java package unl.cse.parking; public class Garage { private final Vehicle
ID: 3870633 • Letter: G
Question
Garage.java
package unl.cse.parking;
public class Garage {
private final Vehicle stalls[];
public Garage(int capacity) {
stalls = new Vehicle[capacity];
}
/**
* "Parks" the vehicle in a stall if one is available. Returns true if
* the vehicle was added successfully; false otherwise.
*/
public boolean addVehicle(Vehicle automobile) {
//TODO: implement this method
return false;
}
/**
* Remove the vehicle having the provided
* license plate number from the garage. The method returns
* the vehicle if it is found otherwise it returns null.
*/
public Vehicle removeVehicle(String license) {
//TODO: implement this method
return null;
}
/**
* Returns the maximum capacity of this Garage
*/
public int getCapacity() {
return this.stalls.length;
}
/**
* Returns the number of empty stalls in the garage--the number of open
* spots
* @return
*/
public int getNumFreeSpots() {
int freeSpots = 0;
for(int i=0; i<stalls.length; i++) {
if(stalls[i] == null) {
freeSpots++;
}
}
return freeSpots;
}
/**
* Simulates the passing of a day by adding a day in the garage to each vehicle
* in the garage.
* @return
*/
public void addDay() {
//TODO: implement this
}
public void addDay(int numDays) {
//TODO: optionally implement this as well
}
/**
* Displays the current "state" of the garage by printing out information about each
* stall
*/
public void displayReport() {
System.out.println("Stall License Type Days Total Fee");
for(int i=0; i<this.stalls.length; i++) {
if(stalls[i] == null) {
System.out.println(String.format("%3d EMPTY", (i+1)));
} else {
//TODO: You will need to modify these arguments to display the report correctly
System.out.println(String.format("%3d %-10s %-11s %3d $%5.2f", (i+1), stalls[i].getLicense(), "TODO-TYPE", -1, -1.0));
}
}
}
}
GarageStimulation.java
package unl.cse.parking;
public class GarageSimulation {
public static void main(String[] args) {
Garage safePark = new Garage(10);
Vehicle herbie = new Vehicle("OFP 857");
safePark.addVehicle(herbie);
safePark.addDay();
safePark.addDay();
safePark.addDay();
safePark.displayReport();
}
}
Vehicle.java
package unl.cse.parking;
public class Vehicle {
private final String license;
// The class constructor
public Vehicle(String license) {
this.license = license;
}
/**
* The getter method granting public access to reading the
* license plate number. Notice license does not have a
* setter since it cannot be modified.
*/
public String getLicense() {
return license;
}
}
. Lab Objectives & Topics Upon completion of this lab you should be able to .Use inheritance, composition (aggregation), simple polymorphism .Planning, evaluating, and selecting different design strategies to efficiently and effectively solve problems 2. Problem Statement Your company is responsible for designing billing software for a parking garage named SafePark. SafePark has a capacity of 20 vehicles and can service 3 types of vehicles; motorbikes, compact cars, and SUVs. Parking fees are based on the following table:Explanation / Answer
Below is the modified code with following changes (in bold):
NOTE: Since image is not clear, not able to read vehicle details. Thus omitting adding vehicles in garbageSimulation. Please add it yourself as it is pretty straightforward.
Garage.java
package unl.cse.parking;
public class Garage {
private final Vehicle stalls[];
public Garage(int capacity) {
stalls = new Vehicle[capacity];
}
/**
* "Parks" the vehicle in a stall if one is available. Returns true if
* the vehicle was added successfully; false otherwise.
*/
public boolean addVehicle(Vehicle automobile) {
int availability;
availability = getNumFreeSpots();
if (availability = = 0)
return false;
else
{
int index = stalls.length;
stalls[index] = automobile;
return true;
}
}
/**
* Remove the vehicle having the provided
* license plate number from the garage. The method returns
* the vehicle if it is found otherwise it returns null.
*/
public Vehicle removeVehicle(String license) {
for(int i=0; i<stalls.length; i++) {
if(stalls[i]. getLicense() == license) {
return stalls[i];
}
}
return null;
}
/**
* Returns the maximum capacity of this Garage
*/
public int getCapacity() {
return this.stalls.length;
}
/**
* Returns the number of empty stalls in the garage--the number of open
* spots
* @return
*/
public int getNumFreeSpots() {
int freeSpots = 0;
for(int i=0; i<stalls.length; i++) {
if(stalls[i] == null) {
freeSpots++;
}
}
return freeSpots;
}
/**
* Simulates the passing of a day by adding a day in the garage to each vehicle
* in the garage.
* @return
*/
public void addDay() {
for(int i=0; i<this.stalls.length; i++) {
stalls[i].numDays++;
}
}
/**
* Displays the current "state" of the garage by printing out information about each
* stall
*/
public void displayReport() {
System.out.println("Stall License Type Days Total Fee");
for(int i=0; i<this.stalls.length; i++) {
if(stalls[i] == null) {
System.out.println(String.format("%3d EMPTY", (i+1)));
} else {
System.out.println(String.format("%3d %-10s %-11s %3d $%5.2f", (i+1), stalls[i].getLicense(),stalls[i].vehicleType, stalls[i].numDays,calculateFee(stalls[i])));
}
}
}
Public double calculateFee (Vehicle veh){
double totalFee;
switch(veh.vehicleType)
{
case “Motorbike”:
if(veh.numDays<=7){ totalFee = veh.numDays * 4;}
else { totalFee = veh.numDays * 3 ; }
break;
case “compactCar”:
if(veh.numDays<=7){ totalFee = veh.numDays * 6;}
else { totalFee = veh.numDays * 4.5 ; }
break;
case “SUV”:
if(veh.numDays<=7){ totalFee = veh.numDays * 8;}
else { totalFee = veh.numDays * 6 ; }
break;
}
return totalFee;
}
}
GarageStimulation.java
package unl.cse.parking;
public class GarageSimulation {
public static void main(String[] args) {
Garage safePark = new Garage(10);
Vehicle herbie = new Vehicle("OFP 857");
safePark.addVehicle(herbie);
safePark.addDay();
safePark.addDay();
safePark.addDay();
safePark.displayReport();
}
}
Vehicle.java
package unl.cse.parking;
public class Vehicle {
private final String license;
public numDays=0;
// The class constructor
public Vehicle(String license) {
this.license = license;
}
/**
* The getter method granting public access to reading the
* license plate number. Notice license does not have a
* setter since it cannot be modified.
*/
public String getLicense() {
return license;
}
}
public class Motorbike extends Vehicle{
public String vehicleType = “Motorbike”;
public Motorbike(String license){
super(license);
}
}
public class compactCar extends Vehicle{
public String vehicleType = “compactCar”;
public compactCar (String license){
super(license);
}
}
public class SUV extends Vehicle{
public String vehicleType = “SUV”;
public SUV (String license){
super(license);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.