We were given a class (ParkingSpot) where the constructor, getters/setters, and toString are already set up. Our assignment is to create a class (FindParking) that creates a current car location and four parking spots on four streets. One of the tasks to complete for the assignment is to change the amout charged for parking on streets 3 and 4. The instructions for this project state: "Let the first two parking spots have the default charge (25 cents) and set the last two to have a charge of 30 cents per 10 minutes interval. You can set the charge using the setCharge method from the ParkingSpot class." I not sure how to do this...I'm under the impression that I was to do this only from the FindParking class that I created. Both classes have been attached if someone could please take a look. I tried changing the code under the "Formulas" header in my class from: cost1 = spot1.getCharge()/spot1.INTERVAL * time; to cost1 = spot1.getCharge()+.05/spot1.INTERVAL * time; but this only added .05 to the result of the calculation....where am I going wrong?
This project is due @ midnight so any quick help would be greatly appreciated.
Thanks,
-C.
ParkingSpot Class:
public class ParkingSpot {
private String street;
private double locationX;
private double locationY;
private int duration;
private boolean available;
private double charge;
/**
* The minimum time interval which can be paid for.
*/
public final int INTERVAL = 10; // minutes
/**
* The default charge for INTERVAL minutes
*/
public final double DEFAULT_CHARGE = 0.25;
/**
* Constructor: creates a parking spot object with specified values.
* @param street is the street on which the parking spot is located
* @param locationX is the x (E-W) coordinate
* @param locationY is the Y (N-S) coordinate
* @param duration is the maximum limit on how long a car can park at this spot
*/
public ParkingSpot(String street, double locationX, double locationY, int duration) {
this.street = street;
this.locationX = locationX;
this.locationY = locationY;
this.duration = duration;
available = true;
charge = DEFAULT_CHARGE;
}
/** Gets the maximum limit on how long a car can park at this spot.
* @return the maximum limit on how long a car can park at this spot
*/
public int getDuration() {
return duration;
}
/** Set the the maximum limit on how long a car can park at this spot
* @param duration the maximum limit on how long a car can park at this spot
*/
public void setDuration(int duration) {
this.duration = duration;
}
/** Checks whether the parking spot is available
* @return whether the parking spot is available
*/
public boolean isAvailable() {
return available;
}
/** return the cost for one time interval
* @return the charge
*/
public double getCharge() {
return charge;
}
/** Set the charge for one time interval
* @param charge the charge to set
*/
public void setCharge(double charge) {
this.charge = charge;
}
/** Sets the space to be available or not available
* @param available true is the parking spot is available, false otherwise
*/
public void setAvailable(boolean available) {
this.available = available;
}
/** Get the street on which the parking space is located
* @return the street
*/
public String getStreet() {
return street;
}
/** Get the east-west coordinate of the parking spot
* @return the locationX
*/
public double getLocationX() {
return locationX;
}
/** Get the north-south coordinate of the parking spot
* @return the locationY
*/
public double getLocationY() {
return locationY;
}
/* Returns a String representation of the parking spot
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ParkingSpot [street=" + street + ", locationX=" + String.format("%6.2f", locationX)
+ ", locationY=" + String.format("%6.2f", locationY) + ", duration=" + duration
+ ", available=" + available + "]";
}
}
FindParking Class:
import java.text.DecimalFormat;
import java.util.*;
public class FindParking {
/**
* @param args
*/
public static void main(String[] args) {
int time;
double distance1,distance2,distance3,distance4, cost1, cost2, cost3, cost4, x0, y0, x1,x2,x3,x4,y1,y2,y3,y4;
DecimalFormat fmt = new DecimalFormat ("0.##");
ParkingSpot spot1, spot2, spot3, spot4;
/**Creates a random generator and
* plants a "seed" to create a range
* of values for the starting point
* location and the parking spot location.
*/
long seed;
Random rand = new Random();
/**Creates a scanner to input the
* "seed" and the amount of time
* needed to park.
*/
Scanner scan = new Scanner (System.in);
System.out.println("Enter a number between 0 and 9000000000000000000: ");
seed = scan.nextLong();
rand.setSeed(seed);
System.out.println ("Enter the number of minutes: ");
time = scan.nextInt();
x0 = rand.nextDouble()*100;
x1 = rand.nextDouble()*100;
x2 = rand.nextDouble()*100;
x3 = rand.nextDouble()*100;
x4 = rand.nextDouble()*100;
y0 = rand.nextDouble()*100;
y1 = rand.nextDouble()*100;
y2 = rand.nextDouble()*100;
y3 = rand.nextDouble()*100;
y4 = rand.nextDouble()*100;
/**Creates the parking spot
* objects
*/
spot1 = new ParkingSpot ("Thumb Drive", x1, y1 ,60);
spot2 = new ParkingSpot ("Kershaw Blvd",x2,y2,60);
spot3 = new ParkingSpot ("Dodger Blue Pkwy",x3,y3,60);
spot4 = new ParkingSpot ("Matt Kemp Ln",x4,y4,60);
/**Formulas used for finding the
* distance to the parking spot
* and the cost to park there.
*/
distance1 = Math.abs ((x0 - y0) + (x1 - y1));
distance2 = Math.abs ((x0 - y0) + (x2 - y2));
distance3 = Math.abs ((x0 - y0) + (x3 - y3));
distance4 = Math.abs ((x0 - y0) + (x4 - y4));
cost1 = spot1.getCharge()/spot1.INTERVAL * time;--This is where I need help
cost2 = spot2.getCharge()/spot2.INTERVAL * time;
cost3 = spot3.getCharge()/spot3.INTERVAL * time;
cost4 = spot4.getCharge()/spot4.INTERVAL * time;
/**Prints out the spot's information and the
* results generated from the time and location
* "seed" inputs.
*/
System.out.println(spot1 + "\n" + "distance: " + fmt.format(distance1) + "\tcost: $"+ fmt.format(cost1));
System.out.println(spot2 + "\n" + "distance: " + fmt.format(distance2) + "\tcost: $"+ fmt.format(cost2));
System.out.println(spot3 + "\n" + "distance: " + fmt.format(distance3) + "\tcost: $"+ fmt.format(cost3));
System.out.println(spot4 + "\n" + "distance: " + fmt.format(distance4) + "\tcost: $"+ fmt.format(cost4));
System.out.println("Position of car: " + "x: " + fmt.format(x0) + "\ty: " + fmt.format(y0));
/**Sets up the if statements to
* determine the closest parking spot
*/
Math.min(distance1, distance2);
Math.min(distance1, distance3);
Math.min(distance1, distance4);
Math.min(distance2, distance1);
Math.min(distance2, distance3);
Math.min(distance2, distance4);
Math.min(distance3, distance1);
Math.min(distance3, distance2);
Math.min(distance3, distance4);
Math.min(distance4, distance1);
Math.min(distance4, distance2);
Math.min(distance4, distance3);
if (distance1<distance2 && distance1<distance3 && distance1<distance4){
System.out.println ("The closest spot is " + fmt.format(distance1) + " away.");
}
if (distance2<distance1 && distance2<distance3 && distance2<distance4){
System.out.println ("The closest spot is " + fmt.format(distance2) + " away.");
}
if (distance3<distance1 && distance3<distance2 && distance3<distance4){
System.out.println ("The closest spot is " + fmt.format(distance3) + " away.");
}
if (distance4<distance1 && distance4<distance2 && distance4<distance3){
System.out.println ("The closest spot is " + fmt.format(distance4) + " away.");
}