working with throwing our exceptions and using try and catch blocks. -----------
ID: 3708737 • Letter: W
Question
working with throwing our exceptions and using try and catch blocks.
-----------------------------------
Throwing the Exceptions:
LotFullException: Adjust both of ParkingLot's park() methods to throw this exception instead of returning false if the ParkingLot is full and your Auto/Bicycle cannot be parked.
VehicleMissingException: Adjust ParkingLot's find() method to throw this exception instead of return null if the target vehicle is unable to be found inside the ParkingLot
ParkingSpaceOccupiedException: Adjust ParkingSpace's parkAuto() and parkBicycle() methods so that if the ParkingSpace is occupied, this exception is thrown instead of return false;
NegativeValueException: Adjust setSpeed() and setPass() to throw this exception if negative values are passed into these methods. Will need to update Vehicle Constructors!
InvalidColorException: Adjust Auto's setColor() method to also throw this exception in addition to setting the color to Unknown. Will need to update an Auto Constructor!
InvalidBrandException: Adjust Bicycle's setBrand() method to also throw this exception in addition to setting the Brand to Unknown. Will need to update a Bicycle Constructor!
IllegallyParkedException: Create a new checkSpace() method in ParkingSpace. This method should:
Have no input parameters
Return boolean
Determine if there is something in the ParkingSpace
If there is something in the ParkingSpace AND that thing is an Auto which does not have a permit, then an IllegallyParkedException should be thrown. OTHERWISE return true;
----------------------------------------------
Catching the Exceptions:
NegativeValueException:
In your Driver, create a default Vehicle. Then call the setSpeed() and setPass() methods on that Vehicle inside of a try block. The catch block should print out the exception message if an exception is created.
IllegallyParkedException:
Add a method to ParkingLot called: checkLot(). This method should have no parameters and return nothing. The method should check every ParkingSpace in the ParkingLot (use the new ParkingSpace method checkSpace()). If an IllegallyParkedException occurs during this method, the message "An illegally parked vehicle has been detected, summoning parking enforcement!" should be printed.
--------------------------------------------
Grading Breakdown:
Throwing Exceptions (10 points for each exception correctly thrown as per specification
Try Catch (15 points for each try/catch correctly setup as per specification)
Explanation / Answer
AUTO JAVA:
package auto.project;
import exceptions.*;
public class Auto extends Vehicle implements Parkable
{
private String plate;
private Color color;
private boolean hasPermit;
/**
* Default Auto
*/
public Auto()
{
super();
plate = "N/A";
color = Color.Unknown;
hasPermit = false;
}
/**
* Full parameter Constructor
* @param inPlate String with Plate Data
* @param inColor String with Color Data
* @param inHasPermit boolean: True for has permit, False for not having permit
* @throws NegativeValueException
*/
public Auto(String inPlate, String inColor, boolean inHasPermit,
int inSpeed, int inPass) throws NegativeValueException, InvalidColorException
{
super(inSpeed, inPass);
setPlate(inPlate);
setColor(inColor);
hasPermit = inHasPermit;
}
@Override
public boolean park()
{
if (isHasPermit())
{
return true;
}
else
{
return false;
}
}
/**
* Describes the Auto
* @return a String with the Auto's data
*/
@Override
public String toString()
{
return "Auto{" + "plate=" + plate + ", color=" + color + ", hasPermit=" + hasPermit + super.toString() + '}';
}
/**
* Get the Auto's license plate data
* @return a String with License Plate data
*/
public String getPlate()
{
return plate;
}
/**
* Set the Auto's license plate data
* @param inPlate a String to use for the Auto's License Plate data
*/
public void setPlate(String inPlate)
{
if (inPlate.length() > 0 && inPlate.length() < 9)
plate = inPlate;
else
System.out.println("Attempted to set plate to a String too long or too short!");
}
/**
* Get the color of the Auto
* @return a String containing the color information for the Auto
*/
public Color getColor()
{
return color;
}
/**
* Set the color of the Auto
* @param inColor a String denoting the color of the Auto
* @throws InvalidColorException
*/
public void setColor(String inColor) throws InvalidColorException
{
Color[] validColors = Color.values();
boolean foundMatch = false;
//Check the elements/values in the array
for (int index = 0; index < validColors.length; index++)
{
if (inColor.equals( validColors[index].toString() ))
{
foundMatch = true; //We found a match!!!
}
}
inColor = inColor.replaceAll(" ", "");//Removing Spaces!
//foundMatch is true ONLY IF we found a matching enum value
if (foundMatch)
{
color = Color.valueOf(inColor);
}
else
{
throw new InvalidColorException();
}
}
/**
* Check if the Auto has a permit
* @return a true if Auto has a permit and a false if Auto does NOT have a permit
*/
public boolean isHasPermit()
{
return hasPermit;
}
/**
* Sets the Auto's Permit status
* @param inPermit a boolean denoting if the Auto should (true) or should not (false) have a permit
*/
public void setHasPermit(boolean inPermit)
{
hasPermit = inPermit;
}
public boolean equals(Object otherObject)
{
//Check if the other object is even an Auto
if (!(otherObject instanceof Auto))
{
return false; //Since it's not even a Auto!
}
else
{
// Your logic for figuring out if the objects should match goes here!
if (((Auto) otherObject).getPlate().equals(getPlate()) && ((Auto) otherObject).getColor().equals(getColor()) && ((Auto) otherObject).isHasPermit() == (isHasPermit()))
{
return true;
}
else
{
return false; // At least 1 attribute is different
}
}
}
@Override
public boolean unpark()
{
return true;
}
}
BICYCLE JAVA:
package auto.project;
import exceptions.*;
public class Bicycle extends Vehicle implements Parkable
{
private String serialNumber;
private Brand brand;
private boolean locked;
/**
* Default Bicycle
*/
public Bicycle()
{
super();
serialNumber = "N/A";
brand = Brand.Unknown;
locked = false;
}
/**
* Full parameter Bicycle constructor
* @param inSerialNumber String with Serial Number Data
* @param inBrand String with Brand Data
* @param inLocked boolean: True if bike is locked, False if bike is not locked
* @throws NegativeValueException
*/
public Bicycle(String inSerialNumber, String inBrand, boolean inLocked,
int inSpeed, int inPass) throws NegativeValueException, InvalidBrandException
{
super(inSpeed,inPass);
setSerialNumber(inSerialNumber);
setBrand(inBrand);
locked = inLocked;
}
@Override
public boolean park()
{
setLocked(true);
return true;
}
/**
* Describes the Bicycle
* @return a String with the Bicycle's data
*/
@Override
public String toString()
{
return "Bicycle{" + "serialNumber=" + serialNumber + ", brand=" + brand + ", locked=" + locked + super.toString() + '}';
}
/**
* The Bicycle's serial number
* @return a String containing the Bicycle's serial number
*/
public String getSerialNumber()
{
return serialNumber;
}
/**
* Change the Bicycle's serial number
* @param inSerialNumber a String to set as the Bicycle's serial number
*/
public void setSerialNumber(String inSerialNumber)
{
if ( inSerialNumber.length() > 2 && inSerialNumber.length() < 14)
serialNumber = inSerialNumber;
else
System.out.println("Attempted to set serial number to something too small or too large");
}
/**
* Get the type of Bicycle
* @return a String with the brand of the Bicycle
*/
public Brand getBrand()
{
return brand;
}
/**
* Set the type of Bicycle
* @param inBrand a String with the brand info for the Bicycle
*/
public void setBrand(String inBrand)throws InvalidBrandException
{
Brand[] validBrands = Brand.values();
boolean foundMatch = false;
//Check the elements/values in the array
for (int index = 0; index < validBrands.length; index++)
{
if (inBrand.equals( validBrands[index].toString() ))
{
foundMatch = true; //We found a match!!!
}
}
inBrand = inBrand.replaceAll(" ", "");//Removing Spaces!
//foundMatch is true ONLY IF we found a matching enum value
if (foundMatch)
{
brand = Brand.valueOf(inBrand);
}
else
{
throw new InvalidBrandException();
}
}
/**
* Check if the Bicycle is locked or not
* @return a boolean with data on if the Bicycle is locked (true) or not (false)
*/
public boolean isLocked()
{
return locked;
}
/**
* Set the locked status of the Bicycle
* @param lock a boolean which locks (true) or unlocks (false) a Bicycle
*/
public void setLocked(boolean lock)
{
locked = lock;
}
public boolean equals(Object otherObject)
{
if (!(otherObject instanceof Bicycle))
{
return false; //Since it's not even a Bicycle!
}
else
{
if (((Bicycle) otherObject).getSerialNumber().equals(getSerialNumber()) && ((Bicycle) otherObject).getBrand().equals(getBrand()) && ((Bicycle) otherObject).isLocked() == (isLocked()))
{
return true;
}
else
{
return false; // At least 1 attribute is different
}
}
}
@Override
public boolean unpark()
{
setLocked(false);
return true;
}
}
CODE JAVA:
package auto.project;
import exceptions.*;
import java.util.Random;
public enum Color
{
Unknown, Red, Orange, Pink, Black;
public static Color randomColor()
{
Color[] myColors = Color.values();
Random gen = new Random();
int index = gen.nextInt((myColors.length -1)) + 1;
return myColors[index];
}
public String toString()
{
String base = super.toString();
String output = "";
boolean firstChar = true;
for (int index = 0; index < base.length(); index++)
{
//Check for uppercase
if (Character.isUpperCase( base.charAt(index) ) && !firstChar)
{
output = output + " ";
}
output = output + base.charAt(index);
firstChar = false;
}
return output;
}
}
PARKING OT>JAVA:
package auto.project;
import exceptions.*;
import java.util.Random;
public enum Color
{
Unknown, Red, Orange, Pink, Black;
public static Color randomColor()
{
Color[] myColors = Color.values();
Random gen = new Random();
int index = gen.nextInt((myColors.length -1)) + 1;
return myColors[index];
}
public String toString()
{
String base = super.toString();
String output = "";
boolean firstChar = true;
for (int index = 0; index < base.length(); index++)
{
//Check for uppercase
if (Character.isUpperCase( base.charAt(index) ) && !firstChar)
{
output = output + " ";
}
output = output + base.charAt(index);
firstChar = false;
}
return output;
}
}
VEHICLE JAVA:
package auto.project;
import exceptions.*;
public class Vehicle
{
private int speed;
private int pass;
public Vehicle()
{
speed = 0;
pass = 0;
}
public Vehicle(int speed, int pass) throws NegativeValueException
{
setSpeed(speed);
setPass(pass);
}
@Override
public String toString()
{
return "Vehicle{" + "speed=" + speed + ", pass=" + pass + '}';
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int inSpeed) throws NegativeValueException
{
if (inSpeed > 0)
speed = inSpeed;
else
throw new NegativeValueException("Exception: Negative Speed");
}
public int getPass()
{
return pass;
}
public void setPass(int inPass) throws NegativeValueException
{
if (inPass > 0)
pass = inPass;
else
throw new NegativeValueException("Exception: Negative inPass");
}
}
DRIVER JAVA:
package auto.project;
import exceptions.*;
public class Driver {
public static void main(String args[])
{
Vehicle vehicle = new Vehicle();
try
{
vehicle.setPass(-10);
vehicle.setSpeed(-20);
}
catch(NegativeValueException ex)
{
System.out.println(ex.getMessage());
}
}
}
EXCEPTION CLASSES:
ILLEGALY PARKED EXCEPTION JAVA:
package exceptions;
public class IllegallyParkedException extends Exception {
public IllegallyParkedException()
{
super();
}
public IllegallyParkedException(String msg)
{
super(msg);
}
}
INVALIDBRANDEXCEPTION.JAVA:
package exceptions;
public class InvalidBrandException extends Exception{
public InvalidBrandException()
{
super();
}
public InvalidBrandException(String msg)
{
super(msg);
}
}
InvalidcolorException.java:
package exceptions;
public class InvalidColorException extends Exception {
public InvalidColorException()
{
super();
}
public InvalidColorException(String msg)
{
super(msg);
}
}
LOTFULL EXCEPTION>JAVA:
package exceptions;
public class LotFullException extends Exception{
public LotFullException()
{
super();
}
public LotFullException(String msg)
{
super(msg);
}
}
package exceptions;
public class NegativeValueException extends Exception {
public NegativeValueException()
{
super();
}
public NegativeValueException(String msg)
{
super(msg);
}
}
package exceptions;
public class ParkingSpaceOccupiedException extends Exception{
public ParkingSpaceOccupiedException()
{
super();
}
public ParkingSpaceOccupiedException(String msg)
{
super(msg);
}
}
package exceptions;
public class VehicleMissingException extends Exception {
public VehicleMissingException()
{
super();
}
public VehicleMissingException(String msg)
{
super(msg);
}
}
package exceptions;
public class LotFullException extends Exception{
public LotFullException()
{
super();
}
public LotFullException(String msg)
{
super(msg);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.