Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Fix my code (90% is working. Also I provide a completly working example. 6000 po

ID: 3569154 • Letter: F

Question

Fix my code (90% is working. Also I provide a completly working example. 6000 points!!!)

Car Simulation is working totally. Bus Simulation is glitching. Please fix Bus Simulation. Car simulation example can be found on https://www.dropbox.com/s/v7f8oykesxvv20e/Car%20Simulation.zip?dl=0

Bus Simulation can be found on https://www.dropbox.com/s/k4ao17kx2b5vj8f/Bus%20simulation.zip?dl=0

Purpose of bus simulation is to model passangers, 18 stops, 3 buses, and lines at bus stop.

6000 points!!!!!

http://www.chegg.com/homework-help/questions-and-answers/fix-code-s-90-accurate-code-https-wwwdropboxcom-s-k4ao17kx2b5vj8f-bus-20simulationzip-dl-0-q6284621

http://www.chegg.com/homework-help/questions-and-answers/fix-code-90-working-also-provide-completly-working-example-4500-points-car-simulation-work-q6296893

http://www.chegg.com/homework-help/questions-and-answers/fix-code-90-working-also-provide-completly-working-example-4500-points-car-simulation-work-q6296885

http://www.chegg.com/homework-help/questions-and-answers/fix-code-90-working-also-provide-completly-working-example-4500-points-car-simulation-work-q6296877

Explanation / Answer

public class Vehicle

{

private String make;
private String model;
private int year;
private double odometer;
private double gallons;
private double mpg;

//constants
private final int SPACER_WIDTH = 40;
private final char SPACER_CHAR = '-';
private final double TOL = 0.0001;

//Default constructor
public VehicleBN()

{
make = "VehicleBN Make";
model = "VehicleBN Model";
year = 1950;
odometer = 1.0;
gallons = 1.0;

mpg = calculateMpg();
}

//Full Constructor
public VehicleBN(String make, String model, int year, double odometer, double gallons) {
this();
  
this.make = make;
this.model = model;
setYear(year);
setOdometer(odometer);
setGallons(gallons);
  
mpg = calculateMpg();
}

//alternate constructor
public VehicleBN(String make, String model, int year) {
this();
  
this.make = make;
this.model = model;
setYear(year);
  
mpg = calculateMpg();
}


// accessors, mutators
  
// make
public String getMake() {
return this.make;
}

public void setMake(String make) {
this.make = make;
}

// model
public String getModel() {
return this.model;
}

public void setModel(String model) {
this.model = model;
}

//year
public int getYear() {
return this.year;
}

public void setYear(int year) {
this.year = year;
}

//odometer
public double getOdometer() {
return this.odometer;
}

public void setOdometer(double odometer) {
this.odometer = odometer;
mpg = calculateMpg();
}

//gallons
public double getGallons() {
return this.gallons;
}

public void setGallons(double gallons) {
this.gallons = gallons;
mpg = calculateMpg();
}


//MPG
public double getMpg() {
return this.mpg;
}


// display methods
  
// returns a String representation
public String toString() {
return make + ", " +
model + ", " +
year + ", " +
odometer + ", " +
gallons + ", " +
mpg;

}


//print fields
public void print() {
System.out.println("make: " + make);
System.out.println("model: " + model);
System.out.println("year: " + year);
System.out.println("odom: " + odometer);
System.out.println("gals: " + gallons);
System.out.println("mpg: " + mpg);
}

//print fields with header
public void print(String msg) {
  
spacer();
System.out.println(msg);
spacer();
print();
spacer();
System.out.println();
}
  
// prints a spacer line of a certain character
private void spacer(int width, char ch) {
for (int i=0; i<width; i++) {
System.out.print(ch);
}
  
System.out.println();
}
  
// prints a default spacer line
private void spacer() {
spacer(SPACER_WIDTH, SPACER_CHAR);
}


//take a trip
public void takeTrip(double miles) {
odometer += miles;
  
mpg = calculateMpg();
}

public void takeTrip(int avgSpeed, double hrs) {
odometer += (avgSpeed * hrs);
  
mpg = calculateMpg();
}


//add gas to Vehicle
public void addGas(double galsAdded) {
gallons += galsAdded;
  
mpg = calculateMpg();
}

//calculate the MPG
private double calculateMpg() {
double mpg;
if (gallons == 0) {
gallons = 1;
System.out.println("Error, gallons cannot equal 0, defaulting to 1");
}
  
mpg = odometer/gallons;
  
return mpg;
}


//check if two objects equal each other
public boolean equals(Object obj) {
boolean status;
  
if (obj instanceof VehicleBN) {
  
//Cast to Vehicle object
VehicleBN c = (VehicleBN) obj;
  
// check field by field
status = ( this.make.equals(c.getMake()) ) &&
( this.model.equals(c.getModel()) ) &&
( this.year == c.getYear() ) &&
( Math.abs(this.odometer - c.getOdometer()) <= TOL ) &&
( Math.abs(this.gallons - c.getGallons()) <= TOL ) &&
( Math.abs(this.mpg - c.getMpg()) <= TOL );

if (status) {
return true;
}
else {
return false;
}
  
}
  
else {
//obj is not Vehicle
return false;
}

}

//Tester for Class
public static void main(String [] args) {
VehicleBN Vehicle1 = new VehicleBN();
VehicleBN Vehicle2 = new VehicleBN("Ford", "F-150", 2010, 45000.23, 20.2);
VehicleBN Vehicle3 = new VehicleBN("Acura", "RSX", 2006);
  
//Display objects
System.out.println(Vehicle1);
Vehicle1.print("Default Constructor");
System.out.println(Vehicle2);
Vehicle2.print("Full Constructor");
System.out.println(Vehicle3);
Vehicle3.print("Alternate Constructor");
  
  
//Test mutators
Vehicle1.print("VehicleBN1 Before:");
Vehicle1.setMake("Porsche");
Vehicle1.setModel("VehicleBNrerra");
Vehicle1.setYear(2014);
Vehicle1.setOdometer(1234.56);
Vehicle1.setGallons(15.5);
Vehicle1.print("VehicleBN1 After:");
  
//Test accessors
System.out.println("Individual Fields:");
System.out.println(Vehicle1.getMake());
System.out.println(Vehicle1.getModel());
System.out.println(Vehicle1.getYear());
System.out.println(Vehicle1.getOdometer());
System.out.println(Vehicle1.getGallons());
System.out.println(Vehicle1.getMpg());
System.out.println();
  
//Test utility-methods
System.out.println("Add 5 gals to Vehicle1:");
Vehicle1.addGas(5);
System.out.println(Vehicle1.getGallons());
System.out.println();
  
System.out.println("Take a trip for 200 miles:");
Vehicle1.takeTrip(200);
System.out.println(Vehicle1.getOdometer());
System.out.println();
  
System.out.println("Take a trip at 30 mph for 2.5 hours");
Vehicle1.takeTrip(30, 2.5);
System.out.println(Vehicle1.getOdometer());
  
System.out.println(Vehicle1.equals(Vehicle1));
System.out.println(Vehicle1.equals(Vehicle2));
System.out.println(Vehicle1.equals(Vehicle3));
System.out.println(Vehicle3.equals(Vehicle3));
  
}
}

import java.text.DecimalFormat; // for display formats

public class VehicleBN {

// instance variables

private String make;
private String model;
private int year;
private double odometer;
private double gallons;
private double mpg;
  
//constants
private final int SPACER_WIDTH = 40;
private final char SPACER_CHAR = '-';
private final double TOL = 0.0001;
private final int MIN_YEAR = 1920;
private final int MAX_YEAR = 2150;
  
// format constants
private final DecimalFormat GAL_FMT = new DecimalFormat("###,##0.000");
private final DecimalFormat ODM_FMT = new DecimalFormat("###,##0.0");
private final DecimalFormat MPG_FMT = new DecimalFormat("###,##0.00");

  
// constructors
  
//Default constructor
public VehicleBN() {
make = "Vehicle Make";
model = "Vehicle Model";
year = 1950;
odometer = 1.0;
gallons = 1.0;

mpg = calculateMpg();
}

//Full Constructor
public VehicleBN(String make, String model, int year, double odometer, double gallons) {
this();
  
this.make = make;
this.model = model;
setYear(year);
setOdometer(odometer);
setGallons(gallons);
  
mpg = calculateMpg();
}

//alternate constructor
public VehicleBN(String make, String model, int year) {
this();
  
this.make = make;
this.model = model;
setYear(year);
  
mpg = calculateMpg();
}

  
// accessors, mutators


public String getMake() {
return this.make;
}

public void setMake(String make) {
this.make = make;
}
  
// model
public String getModel()

{
return this.model;
}

public void setModel(String model)

{
this.model = model;
}

//year
public int getYear() {
return this.year;
}

public void setYear(int year) {
if (year >= MIN_YEAR && year <= MAX_YEAR) {
this.year = year;
}
else {
System.out.println("Invalid Year, please set correct year [" + MIN_YEAR + "-" + MAX_YEAR + "]");
}
}

//odometer
public double getOdometer() {
return this.odometer;
}

public void setOdometer(double odometer) {
if (odometer >= 0) {
this.odometer = odometer;
mpg = calculateMpg();
}
else {
System.out.println("Invalid Odometer. Odometer must be >= 0");
}
}

//gallons
public double getGallons() {
return this.gallons;
}

public void setGallons(double gallons) {
if (gallons > 0) {
this.gallons = gallons;
mpg = calculateMpg();
}
else {
System.out.println("Invalid Gallons. Gallons must be > 0");
}
}

  
//MPG
public double getMpg() {
return this.mpg;
}

  

// returns a String representation
public String toString() {
return make + ", " +
model + ", " +
year + ", " +
ODM_FMT.format(odometer) + ", " +
GAL_FMT.format(gallons) + ", " +
MPG_FMT.format(mpg);

}

  
//print fields
public void print() {
System.out.println("make: " + make);
System.out.println("model: " + model);
System.out.println("year: " + year);
System.out.println("odom: " + ODM_FMT.format(odometer));
System.out.println("gals: " + GAL_FMT.format(gallons));
System.out.println("mpg: " + MPG_FMT.format(mpg));
}

//print fields with header
public void print(String msg) {
  
spacer();
System.out.println(msg);
spacer();
print();
spacer();
System.out.println();
}

// prints a spacer line of a certain character
private void spacer(int width, char ch) {
for (int i=0; i<width; i++) {
System.out.print(ch);
}
  
System.out.println();
}

// prints a default spacer line
private void spacer() {
spacer(SPACER_WIDTH, SPACER_CHAR);
}

  
//take a trip
public void takeTrip(double miles) {
odometer += miles;
  
mpg = calculateMpg();
}

public void takeTrip(int avgSpeed, double hrs) {
odometer += (avgSpeed * hrs);
  
mpg = calculateMpg();
}

  
//add gas to Vehicle
public void addGas(double galsAdded) {
gallons += galsAdded;
  
mpg = calculateMpg();
}
  
//calculate the MPG
private double calculateMpg() {
double mpg;
if (gallons == 0) { //catch division by 0
gallons = 1;
System.out.println("Error, gallons cannot equal 0, defaulting to 1");
}
  
mpg = odometer/gallons;
  
return mpg;
}


//check if two objects equal each other
public boolean equals(Object obj) {
boolean status;
  
if (obj instanceof VehicleBN) {
  
//Cast to Vehicle object
VehicleBN c = (VehicleBN) obj;
  
// check field by field
status = ( this.make.equals(c.getMake()) ) &&
( this.model.equals(c.getModel()) ) &&
( this.year == c.getYear() ) &&
( Math.abs(this.odometer - c.getOdometer()) <= TOL ) &&
( Math.abs(this.gallons - c.getGallons()) <= TOL ) &&
( Math.abs(this.mpg - c.getMpg()) <= TOL );
  
if (status) {
return true;
}
else {
return false;
}
  
}
  
else {
//obj is not Vehicle
return false;
}

}

//Tester for Class
public static void main(String [] args) {
VehicleBN Vehicle1 = new VehicleBN();
VehicleBN Vehicle2 = new VehicleBN("Ford", "F-150", 2010, 45000.23, 20.2);
VehicleBN Vehicle3 = new VehicleBN("Acura", "RSX", 2006);
  
//Display objects
System.out.println(Vehicle1);
Vehicle1.print("Default Constructor");
System.out.println(Vehicle2);
Vehicle2.print("Full Constructor");
System.out.println(Vehicle3);
Vehicle3.print("Alternate Constructor");
  
  
//Test mutators
Vehicle1.print("Vehicle1 Before:");
Vehicle1.setMake("Porsche");
Vehicle1.setModel("Vehiclererra");
Vehicle1.setYear(2014);
Vehicle1.setOdometer(1234.56);
Vehicle1.setGallons(15.5);
Vehicle1.print("Vehicle1 After:");
  
//Test accessors
System.out.println("Individual Fields:");
System.out.println(Vehicle1.getMake());
System.out.println(Vehicle1.getModel());
System.out.println(Vehicle1.getYear());
System.out.println(Vehicle1.getOdometer());
System.out.println(Vehicle1.getGallons());
System.out.println(Vehicle1.getMpg());
System.out.println();
  
//Test utility-methods
Vehicle1.print("Utility Classes Test: Vehicle1 Before:");
  
System.out.println("Add 5 gals to Vehicle1:");
Vehicle1.addGas(5);
System.out.println(Vehicle1.getGallons());
System.out.println();
  
System.out.println("Take a trip for 200 miles:");
Vehicle1.takeTrip(200);
System.out.println(Vehicle1.getOdometer());
System.out.println();
  
System.out.println("Take a trip at 30 mph for 2.5 hours");
Vehicle1.takeTrip(30, 2.5);
System.out.println(Vehicle1.getOdometer());
  
Vehicle1.print("Utility Classes Test: Vehicle1 After:");
  
//Test equals
System.out.println("Vehicle1 = Vehicle1? " + Vehicle1.equals(Vehicle1));
System.out.println("Vehicle1 = Vehicle2? " + Vehicle1.equals(Vehicle2));
System.out.println("Vehicle1 = Vehicle3? " + Vehicle1.equals(Vehicle3));
System.out.println("Vehicle3 = Vehicle3? " + Vehicle3.equals(Vehicle3));
  
}
}//