Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSC-112 - Multiple Car Race Solution (Extra Credit
/*
* Name: Antonio Silvestri
* Date: 01/31/2019
* Course Number: CSC-112
* Course Name: Intermediate Java Programming
* Problem Number: HW 2 Extra Credit
* 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);
}
}
/*
* Name: Antonio Silvestri
* Date: 01/28/2019
* Course Number: CSC-112
* Course Name: Intro to Java Programming
* Problem Number: HW 2
* 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);
}
// *********************
// Constructors
// *********************
public Vehicle(String make, String model, int year) {
this.make = !make.trim().equals("") ? make : "Bogus Make";
this.model = !model.trim().equals("") ? model : "Bogus Model";
this.year = (year > 1940 && year < 2019) ? year : 0;
this.speed = 0;
this.distance = 0;
}
// Vehicle car = new Vehicle("", "", 2019)
// *********************
// General Purpose Methods
// *********************
public void accelerate() {
double speed = this.speed + 5.0;
this.speed = speed > 120 ? 120.0 : speed;
}
public void brake() {
double speed = this.speed - 5.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