Hello. I\'m working on some homework and I\'m stuck. I don\'t know how to go abo
ID: 3799891 • Letter: H
Question
Hello. I'm working on some homework and I'm stuck. I don't know how to go about the next step, and creating the class that is being requested. I'll provide the code of the other classes I have, they're pretty basic. If you can create the class that's being requested, and explain the logic to how/why you created the class, I would be very appreciative. I'm not sure if I should create individual objects for each requested parking space, or if I should use an array. To me they both seem to be plausable but I'm not sure which is best, nor do I really know how to go about creating this class. So the homework directions for this step say: "Define a class ParkingLot. A parking lot generally has a number of parking spaces. (You are asked to use composition). Add a default constructor which initializes a parking lot with 5 parking spaces. The first two spaces numbered 1000 and 1001 are handicapped parking spaces. The left three spaces numbered 1002, 1003 and 1004 are regular parking spaces. Add a method that returns the number of parking spaces in the parking lot. Add getters as needed."
//ParkingSpace.java
package parking_space;
abstract public class ParkingSpace {
private int spaceNum;
private boolean isEmpty;
public ParkingSpace(int spaceNum)
{
this.spaceNum = spaceNum;
}
public int getSpaceNum() {
return spaceNum;
}
public boolean isEmpty() {
return isEmpty;
}
public void setEmpty(boolean isEmpty) {
this.isEmpty = isEmpty;
}
//It is to show the type of the parking space.
//Subclasses will override it.
abstract public void showSpaceType();
}
//RegularParkingSpace.java
package parking_space;
public class RegularParkingSpace extends ParkingSpace {
public RegularParkingSpace(int spaceNum) {
super(spaceNum);
// TODO Auto-generated constructor stub
}
@Override
public void showSpaceType() {
// TODO Auto-generated method stub
System.out.println("Regular Parking Space");
}
}
//HandicappedParkingSpace.java
package parking_space;
public class HandicappedParkingSpace extends ParkingSpace {
public HandicappedParkingSpace(int spaceNum) {
super(spaceNum);
// TODO Auto-generated constructor stub
}
@Override
public void showSpaceType() {
// TODO Auto-generated method stub
System.out.println("Handicapped Parking Space");
}
}
Explanation / Answer
package parking;
import java.util.ArrayList;
public class ParkingLot {
ArrayList<ParkingSpace> parkingspaces=new ArrayList<ParkingSpace>();
public ParkingLot()
{
HandicappedParkingSpace a=new HandicappedParkingSpace(1000);
HandicappedParkingSpace b=new HandicappedParkingSpace(1001);
RegularParkingSpace c=new RegularParkingSpace(1002);
RegularParkingSpace d=new RegularParkingSpace(1003);
RegularParkingSpace e=new RegularParkingSpace(1004);
parkingspaces.add(a);
parkingspaces.add(b);
parkingspaces.add(c);
parkingspaces.add(d);
parkingspaces.add(e);
}
public void addParkings(ParkingSpace p)
{
parkingspaces.add(p);
}
public ArrayList<ParkingSpace> getParkings()
{
return parkingspaces;
}
public static void main(String args[])
{
ParkingLot parkinglot=new ParkingLot();
parkinglot.addParkings(new RegularParkingSpace(1005));
System.out.println(parkinglot.getParkings());
}
}
explaination: I have used arraylist of parkingspace so that we can dynamically add lot of parking spaces in the parkinglot without thinking about the size of array using the add method. In the constructoir i have initialized the arraylist with 5 parking spaces. So whenever you make an object of a parking lot we get those 5 spaces from starting.
NOTE: in below classes i have added a tostring method in your abstract class and changed the showspacetype method to return string instead of void. The tostring method directly prints out object when printed using system.out.println. to console.
We are using tostring method is this line given above: System.out.println(parkinglot.getParkings());
This prints the objects of parking spaces present in arraylist to console.
package parking;
//ParkingSpace.java
abstract public class ParkingSpace {
private int spaceNum;
private boolean isEmpty;
public ParkingSpace(int spaceNum)
{
this.spaceNum = spaceNum;
}
public int getSpaceNum() {
return spaceNum;
}
public boolean isEmpty() {
return isEmpty;
}
public void setEmpty(boolean isEmpty) {
this.isEmpty = isEmpty;
}
//It is to show the type of the parking space.
//Subclasses will override it.
abstract public String showSpaceType();
@Override
public String toString() {
return "ParkingSpaceType " + this.showSpaceType() +
", space number: " + this.getSpaceNum()+" ";
}
}
package parking;
public class RegularParkingSpace extends ParkingSpace {
public RegularParkingSpace(int spaceNum) {
super(spaceNum);
// TODO Auto-generated constructor stub
}
@Override
public String showSpaceType() {
// TODO Auto-generated method stub
return "Regular parking space";
}
}
package parking;
public class HandicappedParkingSpace extends ParkingSpace {
public HandicappedParkingSpace(int spaceNum) {
super(spaceNum);
// TODO Auto-generated constructor stub
}
@Override
public String showSpaceType() {
// TODO Auto-generated method stub
return "Handicapped Parking Space";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.