-
-
Save bytecodeman/5b5f08c8480e01f47dcc8598b59f08e6 to your computer and use it in GitHub Desktop.
CSC-112 - Multiple Car Race Solution (Extra Credit)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 | |
1974 Chevy Nova | |
1978 Ford Thunderbird | |
1983 Pontiac Sunbird |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Antonio Silvestri | |
* Date: 01/31/2019 | |
* Course Number: CSC-112 | |
* Course Name: Intermediate Java Programming | |
* Problem Number: HW 1 | |
* Email: silvestri@stcc.edu | |
* Multiple Race Simulation | |
*/ | |
package vehicle; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class SimulateMultiCarRace { | |
final static String TITLE = "Multiple Car Race Simulation V3.0"; | |
final static String CONTINUE_PROMPT = "Race again? [y/N] "; | |
final static double DISTANCE = 500; | |
private static void reportResults(int countMin, Vehicle []cars) { | |
System.out.printf("Race Done in %d mins\n", countMin); | |
sort(cars); | |
System.out.printf("%s won the race and travelled %.3f miles\n", cars[0], cars[0].getDistance()); | |
for (int i = 1; i < cars.length; i++) { | |
System.out.printf("%s lost and travelled %.3f miles\n", cars[i], cars[i].getDistance()); | |
} | |
} | |
// ********************************************** | |
private static void sort(Vehicle[] cars) { | |
for (int i = 0; i < cars.length - 1; i++) | |
for (int j = i + 1; j < cars.length; j++) | |
if (cars[j].getDistance() > cars[i].getDistance()) { | |
Vehicle temp = cars[i]; | |
cars[i] = cars[j]; | |
cars[j] = temp; | |
} | |
} | |
// ********************************************** | |
private static void adjustCarSpeeds(Vehicle cars[]) { | |
for (int i = 0; i < cars.length; i++) { | |
cars[i].adjustCarSpeed(); | |
} | |
} | |
// ********************************************** | |
private static void updateCarDistances(Vehicle[] cars) { | |
for (int i = 0; i < cars.length; i++) { | |
cars[i].setDistance(1); | |
} | |
} | |
// ********************************************** | |
private static boolean noCarCrossedFinishLine(Vehicle[] cars) { | |
for (int i = 0; i < cars.length; i++) | |
if (cars[i].getDistance() >= DISTANCE) | |
return false; | |
return true; | |
} | |
// ********************************************** | |
private static void raceThemCars(Vehicle[] cars) { | |
int countMin = 0; | |
while (noCarCrossedFinishLine(cars)) { | |
adjustCarSpeeds(cars); | |
updateCarDistances(cars); | |
countMin++; | |
} | |
reportResults(countMin, cars); | |
} | |
// ********************************************** | |
private static Vehicle[] loadCars(String vehicleFilename) throws FileNotFoundException { | |
Scanner sc = new Scanner(new File(vehicleFilename)); | |
int numberOfCars = sc.nextInt(); | |
Vehicle[] cars = new Vehicle[numberOfCars]; | |
for (int i = 0; i < numberOfCars; i++) { | |
int year = sc.nextInt(); | |
String make = sc.next(); | |
String model = sc.next(); | |
cars[i] = new Vehicle(make, model, year); | |
sc.nextLine(); | |
} | |
return cars; | |
} | |
// ********************************************** | |
private static void process(Scanner sc, String args[]) throws FileNotFoundException { | |
Vehicle cars[] = loadCars("RaceVehicles.txt"); | |
raceThemCars(cars); | |
} | |
// ********************************************** | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
public static void main(String args[]) throws FileNotFoundException { | |
System.out.println("Welcome to " + TITLE); | |
Scanner sc = new Scanner(System.in); | |
do { | |
process(sc, args); | |
} while (doThisAgain(sc, CONTINUE_PROMPT)); | |
sc.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Antonio Silvestri | |
* Date: 02/05/2020 | |
* Course Number: CSC-112 | |
* Course Name: Intro to Java Programming | |
* Problem Number: HW 1 | |
* Email: silvestri@stcc.edu | |
* Vehicle Class to Support Race Car Simulation | |
*/ | |
package vehicle; | |
public class Vehicle { | |
private String make; | |
private String model; | |
private int year; | |
private double speed; | |
private double distance; | |
// ********************* | |
// Getters (Accessors) | |
// ********************* | |
public String getMake() { | |
return this.make; | |
} | |
public String getModel() { | |
return this.model; | |
} | |
public int getYear() { | |
return this.year; | |
} | |
public double getSpeed() { | |
return this.speed; | |
} | |
public double getDistance() { | |
return this.distance; | |
} | |
// ********************* | |
// Setters (Mutators) | |
// ********************* | |
public void setDistance(double timeInMinutes) { | |
if (timeInMinutes >= 0) | |
this.distance += this.getSpeed() / 60.0 * timeInMinutes; | |
else | |
System.err.println("Bad Time Passed: " + timeInMinutes); | |
} | |
public void setMake(String make) { | |
this.make = (make != null && !make.equals("")) ? make : "Bogus Make"; | |
} | |
public void setModel(String model) { | |
this.model = (model != null && !model.equals("")) ? model : "Bogus Model"; | |
} | |
public void setYear(int year) { | |
this.year = (year > 1940 && year < 2020) ? year : 0; | |
// if (year > 1940 && year < 2020) | |
// this.year = year; | |
// else | |
// System.err.println("Bad Year: " + year); | |
} | |
public void setSpeed(double speed) { | |
this.speed = (speed >= 0) ? speed : 0; | |
} | |
// ********************* | |
// Constructors | |
// ********************* | |
public Vehicle(String make, String model, int year) { | |
setMake(make); | |
setModel(model); | |
setYear(year); | |
setSpeed(speed); | |
this.distance = 0; | |
} | |
// ********************* | |
// General Purpose Methods | |
// ********************* | |
public void accelerate() { | |
double speed = this.speed + 10.0; | |
this.speed = speed > 120 ? 120.0 : speed; | |
} | |
public void brake() { | |
double speed = this.speed - 10.0; | |
this.speed = speed < 0 ? 0 : speed; | |
} | |
public void adjustCarSpeed() { | |
double rand = Math.random(); | |
if (rand < 0.50) | |
this.accelerate(); | |
else if (rand < 0.75) | |
this.brake(); | |
} | |
@Override | |
public String toString() { | |
return year + " " + make + " " + model; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment