Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Created February 6, 2019 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/94cc0ffc59f2876ca4978eb29aab3222 to your computer and use it in GitHub Desktop.
Save bytecodeman/94cc0ffc59f2876ca4978eb29aab3222 to your computer and use it in GitHub Desktop.
CSC-112 HW2 Vehicle Class Solution
/*
* 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
*/
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 != null && !make.equals("")) ? make : "Bogus Make";
this.model = (model != null && !model.equals("")) ? model : "Bogus Model";
this.year = (year > 1940 && year < 2018) ? year : 0;
this.speed = 0;
this.distance = 0;
}
// *********************
// 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