Hello, For my Java Object Oriented Programming class, we are learning about poly
ID: 3593742 • Letter: H
Question
Hello,
For my Java Object Oriented Programming class, we are learning about polymorphism and abstract classes. I unfortunately had to miss one of the lectures so I am a bit confused on how to get this program going. Here's what needs to be done with the program and one of the pre-defined classes is attached to here. This will need to use polymorphism as well.
//Abstract class ParkingSpace
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();
}
Create a Java project named CS235PA4. Right click "src" to import the jar file Parking (General->Archive File). You will find an abstract class called ParkingSpace is defined, which includes an abstract method showSpaceType to show the type of the parking space. 1. Define a class RegularParkingSpace, which is a subclass of ParkingSpace. Add a constructor which makes a regular parking space with a given space number. Override the abstract method showSpaceType to show this is a "Regular Parking Space". 2. Define a class HandicappedParkingSpace, which is a subclass of ParkingSpace. Add a constructor which makes a handicapped parking space with a given space number. Override the abstract method showSpaceType to show this is a "Handicapped Parking Space" 3. Define a class ParkingLot. A parking lot generally has a number of parking spaces. (You are asked to use composition) 4. a. Add a default constructor which initiae a prking 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. b. c. Define a driver class TestParking. In its main method, declare and instantiate a parking lot using default constructor. Print the type of every parking space in this parking lot, such as 5. "Parking space 10: It is a handicapped parking space." Your code MUST reflect the use of polymorphism. Otherwise, this assignment will be failed. Create a Jar file for your project and upload to PA4 on D2L. Wrong submission will result penalty.Explanation / Answer
ParkingSpace.java
public abstract 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
public class RegularParkingSpace extends ParkingSpace {
public RegularParkingSpace(int spaceNum) {
super(spaceNum);
}
@Override
public void showSpaceType() {
System.out.println(" Parking Space " + getSpaceNum() + ": It is a Regular Parking Space");
}
}
__________________
HandicappedParkingSpace.java
public class HandicappedParkingSpace extends ParkingSpace {
public HandicappedParkingSpace(int spaceNum) {
super(spaceNum);
}
@Override
public void showSpaceType() {
System.out.println("Parking Space " + getSpaceNum() + ": It is a Handicapped Parking Space");
}
}
____________________
ParkingLot.java
public class ParkingLot {
ParkingSpace p[] = null;
public ParkingLot() {
p = new ParkingSpace[5];
p[0] = new HandicappedParkingSpace(1000);
p[1] = new HandicappedParkingSpace(1001);
p[2] = new RegularParkingSpace(1002);
p[3] = new RegularParkingSpace(1003);
p[4] = new RegularParkingSpace(1004);
}
public int getNoOfParkingSpaces() {
return p.length;
}
public ParkingSpace[] getP() {
return p;
}
}
_______________________
TestParking.java
public class TestParking {
public static void main(String[] args) {
ParkingLot p = new ParkingLot();
for (int i = 0; i < p.getNoOfParkingSpaces(); i++) {
if (p.getP()[i] instanceof RegularParkingSpace) {
p.getP()[i].showSpaceType();
} else if (p.getP()[i] instanceof HandicappedParkingSpace) {
p.getP()[i].showSpaceType();
}
}
}
}
_____________________
Output:
Parking Space 1000: It is a Handicapped Parking Space
Parking Space 1001: It is a Handicapped Parking Space
Parking Space 1002: It is a Regular Parking Space
Parking Space 1003: It is a Regular Parking Space
Parking Space 1004: It is a Regular Parking Space
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.