Hey guys, in the below code, i am simulating a rover movement. I believe there i
ID: 3856836 • Letter: H
Question
Hey guys, in the below code, i am simulating a rover movement. I believe there is a calculation error that I was hoping to get help with. The code simulated the movement of a rover on a cliff The Rover should not fall off the cliff, however, it does. i believe the error is in damagedrover.java
Output
hii!
MarsRover ctor
Fell
1003.0
Expected Output: It should not fall
Code
DamagedRover.Java
package transport;
public class DamagedRover extends MarsRover
{
private double position; // Distance from start. Range is
// -1000 to +1000. If Rover travels
// beyond this range, it falls off
// a cliff.
private double metersTraveled; // Total meters traveled
// back and forth.
private boolean fell; // If true, an expensive loss.
private final static int MAX_TRAVEL_METERS_BEFORE_EMPTY_BATTERY = 10000;
private final static int METERS_FROM_START_TO_CLIFF = 1000;
private final static int N_SIMULATIONS = 5000;
//
// Simulates travel under damage conditions. In each turn, travels forward or
// backward either 1, 2, 3, or 4 meters. Continues until there's no more power
// in the battery, or we fall off a cliff. Cliffs are at position = 1000 or
// position = -1000.
//
public void simulateStormDamageTravel() {
position = 0;
while (metersTraveled < MAX_TRAVEL_METERS_BEFORE_EMPTY_BATTERY)
{
double distanceNextTurn = (int)(1 + 4*Math.random());
boolean forwardNotBack = (Math.random() > 0.5);
// Adjust position and metersTraveled.
if (forwardNotBack)
position = position + distanceNextTurn; // ????
else
position = position - distanceNextTurn; // ????
metersTraveled = metersTraveled+distanceNextTurn; // ????
// Check for falling off cliff. If Rover fell, set fell to true and
// terminate (break out of) the loop.
if (metersTraveled>1000||metersTraveled<-1000) // ????
{
fell = true;
break;
}
}
}
// Add accessor methods public double getMetersTraveled()
// and public boolean getFell().
public double getMetersTraveled(){ return metersTraveled;}
public boolean getFell(){ return fell;}
public static void main(String[] args) {
DamagedRover dodo = new DamagedRover();
dodo.simulateStormDamageTravel(); // shd i make it static
if(dodo.fell)
{
System.out.println("Fell");
System.out.println(dodo.metersTraveled);
}else if(dodo.metersTraveled >= 10000)
{
System.out.println("No more power");
}
}
}
MarsRover.Java
package transport;
public class MarsRover extends UnmannedVehicle{
public MarsRover(){
System.out.println( "MarsRover ctor");
}
public static void main(String[] args) {
MarsRover mr = new MarsRover();}
}
UnmannedVehicle.java
package transport;
public class UnmannedVehicle extends Vehicle {
public UnmannedVehicle(){super();}
public UnmannedVehicle(int c){
super(c);
System.out.println("UnmannedVehicle");
}
}
Vehicle.Java
package transport;
public class Vehicle {
public static void main(String[] args) {
int i = 10;
System.out.println(i);
i = i + 100;
System.out.println(i); i = i + 1000; System.out.println(i);
}
private int nWheels;
public Vehicle(){ System.out.println("hii!");}
/* add a Vehicle constructor that takes one int arg called nWheels.
* The constructor should store its arg in the instance variable
* nWheels.*/
public Vehicle(int nWheels){
this.nWheels=nWheels;
System.out.println("Vehicle ctor");
}
}
Explanation / Answer
From DamagedRover code snippet
if (metersTraveled>1000||metersTraveled<-1000){
fell = true;
break;
}
First of all rover will only fall if he either traveled 1000 forwards or backwards, and this displacement is calculated by position variable and not metersTraveled.That is, rover could travels more than 1000 meters distance while travelling in a circle and not covering actual 1000 displacement in any direction(forward or backward).So instead of using metersTraveled use position as your deciding case when your rover will fall of the cliff.
Therefore replace the above portion of code with the below snippet.
if (position>1000||postion<-1000){
fell = true;
break;
}