Previous Code: *****************************************************************
ID: 3566653 • Letter: P
Question
Previous Code:
***********************************************************************************
class Location
{
int x =0;
int y =0;
int terrain =1;
Location()
{
return;
}
Location(int x1, int y1, int t)
{
x=x1;
y=y1;
terrain=t;
return;
}
public void setX(int X)
{
x=X;
return;
}
public void setY(int Y)
{
y=Y;
return;
}
public void setTerrain(int T)
{
terrain=T;
return;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getTerrain()
{
return terrain;
}
public double getDist()
{
double dist = Math.sqrt(x*x+y*y);
return dist;
}
}
***********************************************************************************
class moonBuggy
{
private int speed = 10;
private double handling = 0;
moonBuggy()
{
return;
}
moonBuggy(int s, double h)
{
speed = s;
handling = h;
return;
}
public void setSpeed(int s)
{
speed=s;
return;
}
public void setHandling(double h)
{
handling=h;
return;
}
public int getSpeed()
{
return speed;
}
public double getHandling()
{
return handling;
}
public double getTime(Location L)
{
double distance = L.getDist();
int terrain = L.getTerrain();
double time = (distance/speed)*(terrain)*(1-handling);
return time;
}
}
***********************************************************************************
import java.util.Scanner;
class moonRace
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int s, x, y, t;
double h;
moonBuggy mb1 = new moonBuggy();
moonBuggy mb2 = new moonBuggy(5,0.6);
moonBuggy mb3 = new moonBuggy();
System.out.println("Please enter the speed of buggy 1.");
s= kb.nextInt();
kb.nextLine();
mb1.setSpeed(s);
System.out.println("Please enter the handling of buggy 1.");
h= kb.nextDouble();
kb.nextLine();
mb1.setHandling(h);
Location mL1 = new Location();
Location mL2 = new Location(4, 8, 5);
System.out.println("Please enter the x value of the first location");
x= kb.nextInt();
kb.nextLine();
System.out.println("Please enter the y value of the first location");
y= kb.nextInt();
kb.nextLine();
System.out.println("Please enter the terrain value of the first location");
t= kb.nextInt();
kb.nextLine();
mL1.setX(x);
mL1.setY(y);
mL1.setTerrain(t);
System.out.println("For the first location");
System.out.println("The time for mb1 is "+mb1.getTime(mL1));
System.out.println("The time for mb2 is "+mb2.getTime(mL1));
System.out.println("The time for mb3 is "+mb3.getTime(mL1));
if((mb1.getTime(mL1)<=mb2.getTime(mL1))&&(mb1.getTime(mL1)<=mb3.getTime(mL1)))
{
System.out.println("Moonbuggy1 Wins");
}
if((mb2.getTime(mL1)<=mb1.getTime(mL1))&&(mb2.getTime(mL1)<=mb3.getTime(mL1)))
{
System.out.println("Moonbuggy2 Wins");
}
if((mb3.getTime(mL1)<=mb1.getTime(mL1))&&(mb3.getTime(mL1)<=mb2.getTime(mL1)))
{
System.out.println("Moonbuggy3 Wins");
}
System.out.println(" For the second location");
System.out.println("The time for mb1 is "+mb1.getTime(mL2));
System.out.println("The time for mb2 is "+mb2.getTime(mL2));
System.out.println("The time for mb3 is "+mb3.getTime(mL2));
if((mb1.getTime(mL2)<=mb2.getTime(mL2))&&(mb1.getTime(mL2)<=mb3.getTime(mL2)))
{
System.out.println("Moonbuggy1 Wins");
}
if((mb2.getTime(mL2)<=mb1.getTime(mL2))&&(mb2.getTime(mL2)<=mb3.getTime(mL2)))
{
System.out.println("Moonbuggy2 Wins");
}
if((mb3.getTime(mL2)<=mb1.getTime(mL2))&&(mb3.getTime(mL2)<=mb2.getTime(mL2)))
{
System.out.println("Moonbuggy3 Wins");
}
}
}
********************************************************************
You will create an expansion to your previous moonrace program. . You will create a new class called 3DLocation which will inherit from your previous location class. This location will now include a new parameter for the distance in the z direction. You may have to override or overload certain methods. . You will also create a new class called TurboBuggy which will inherit from your buggy class. These buggies will now include a new parameter called acceleration which will be an integer between 1 and 10 (default of 1) and one called boost which will be an integer between 0 and 3 (default 0). You may have to override or overload certain methods. . You will modify your main program to implement these two new classes. You will create 3 TurboBuggies and 2 3Dlocations in the same ways that you did in the previous program. You will then calculate the time it takes each TurboBuggy to get to each 3Dlocation and declare a winner for each location. . New formulas to useExplanation / Answer
Program Code:
//Implementation class
import java.util.Scanner;
public class moonRace
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
//additionally z, acceleration, boost variables are added
int s, x, y, t, z;
int acc, bst;
double h;
//creating new objects to the
//new class TurboBuggy
TurboBuggy mb1 = new TurboBuggy();
TurboBuggy mb2 = new TurboBuggy(5,0.6, 1, 0);
TurboBuggy mb3 = new TurboBuggy();
System.out.println("Please enter the speed of buggy 1.");
s= kb.nextInt();
kb.nextLine();
mb1.setSpeed(s);
System.out.println("Please enter the handling of buggy 1.");
h= kb.nextDouble();
kb.nextLine();
mb1.setHandling(h);
//print statement for acceleration
System.out.println("Please enter the acceleration(1-10) of buggy 1.");
acc= kb.nextInt();
kb.nextLine();
//set the value of the acceleration
mb1.setAcceleration(acc);
//print statement for booster
System.out.println("Please enter the boosters(0-3) of buggy 1.");
bst= kb.nextInt();
kb.nextLine();
//set the value of the booster
mb1.setBoost(bst);
//create two objects for the ThreeDLocation
ThreeDLocation mL1 = new ThreeDLocation();
ThreeDLocation mL2 = new ThreeDLocation(4, 8, 10, 5 );
System.out.println("Please enter the x value of the first location");
x= kb.nextInt();
kb.nextLine();
System.out.println("Please enter the y value of the first location");
y= kb.nextInt();
kb.nextLine();
//accept input for the z-axis
System.out.println("Please enter the z value of the first location");
z= kb.nextInt();
kb.nextLine();
System.out.println("Please enter the terrain value of the first location");
t= kb.nextInt();
kb.nextLine();
//set the x, y, z, and t values
mL1.setX(x);
mL1.setY(y);
mL1.setY(z);
mL1.setTerrain(t);
//at first location
System.out.println("For the first location");
System.out.println("The time for mb1 is "+mb1.getTimes(mL1));
System.out.println("The time for mb2 is "+mb2.getTimes(mL1));
System.out.println("The time for mb3 is "+mb3.getTimes(mL1));
//conditions to check which buggy wins the race
if((mb1.getTimes(mL1)<=mb2.getTimes(mL1))&&(mb1.getTimes(mL1)<=mb3.getTimes(mL1)))
{
System.out.println("Moonbuggy1 Wins");
}
if((mb2.getTimes(mL1)<=mb1.getTimes(mL1))&&(mb2.getTimes(mL1)<=mb3.getTimes(mL1)))
{
System.out.println("Moonbuggy2 Wins");
}
if((mb3.getTimes(mL1)<=mb1.getTimes(mL1))&&(mb3.getTimes(mL1)<=mb2.getTimes(mL1)))
{
System.out.println("Moonbuggy3 Wins");
}
//race at second location
System.out.println(" For the second location");
System.out.println("The time for mb1 is "+mb1.getTimes(mL2));
System.out.println("The time for mb2 is "+mb2.getTimes(mL2));
System.out.println("The time for mb3 is "+mb3.getTimes(mL2));
//conditions to check which buggy wins
if((mb1.getTimes(mL2)<=mb2.getTimes(mL2))&&(mb1.getTimes(mL2)<=mb3.getTimes(mL2)))
{
System.out.println("Moonbuggy1 Wins");
}
if((mb2.getTimes(mL2)<=mb1.getTimes(mL2))&&(mb2.getTimes(mL2)<=mb3.getTimes(mL2)))
{
System.out.println("Moonbuggy2 Wins");
}
if((mb3.getTimes(mL2)<=mb1.getTimes(mL2))&&(mb3.getTimes(mL2)<=mb2.getTimes(mL2)))
{
System.out.println("Moonbuggy3 Wins");
}
}
}
-----------------------------------------------------------------------------------------------------------
public class Location
{
int x =0;
int y =0;
int terrain =1;
public Location()
{
return;
}
public Location(int x1, int y1, int t)
{
x=x1;
y=y1;
terrain=t;
return;
}
public void setX(int X)
{
x=X;
return;
}
public void setY(int Y)
{
y=Y;
return;
}
public void setTerrain(int T)
{
terrain=T;
return;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getTerrain()
{
return terrain;
}
public double getDist()
{
double dist = Math.sqrt(x*x+y*y);
return dist;
}
}
-----------------------------------------------------------------------------------------------------
//ThreeDLocation which extends the features of Location class
public class ThreeDLocation extends Location
{
//z-axis variable
private int z;
//default constructor
public ThreeDLocation()
{
super();
return;
}
//Over riding the Location constructor
public ThreeDLocation(int x1, int y1, int z1, int t1)
{
super(x1, y1, t1);
z=z1;
}
//set the value of z
public void setZ(int Z)
{
z=Z;
return;
}
//get the value of z
public int getZ()
{
return z;
}
//over ride the Terrain method
public int getTerrain()
{
return super.getTerrain();
}
//getDistance method that uses the super class distance
public double getDistance()
{
double dist = Math.sqrt(getDist()*getDist()+z*z);
return dist;
}
}
--------------------------------------------------------------------------------------------------------
public class moonBuggy
{
private int speed = 10;
private double handling = 0;
public moonBuggy()
{
return;
}
public moonBuggy(int s, double h)
{
speed = s;
handling = h;
return;
}
public void setSpeed(int s)
{
speed=s;
return;
}
public void setHandling(double h)
{
handling=h;
return;
}
public int getSpeed()
{
return speed;
}
public double getHandling()
{
return handling;
}
public double getTime(Location L)
{
double distance = L.getDist();
int terrain = L.getTerrain();
double time = (distance/speed)*(terrain)*(1-handling);
return time;
}
}
//TurboBuggy class that extends moonBuggy class
public class TurboBuggy extends moonBuggy
{
//declare acceleration and boost variables
int acceleration;
int boost;
moonBuggy mB;
//default constructor
public TurboBuggy()
{
super();
return;
}
//parameterized constructor the over rides the
//super class constructor of moonBuggy
public TurboBuggy(int s, double h, int accel, int boo)
{
super(s, h);
acceleration=accel;
boost=boo;
}
//set acceleration method
public void setAcceleration(int a)
{
acceleration=a;
return;
}
//set boost method
public void setBoost(int b)
{
boost=b;
return;
}
//get acceleration method
public int getAcceleration()
{
return acceleration;
}
//get boost method
public int setBoost()
{
return boost;
}
//getTimes method that uses the ThreeDLocation variables
//and super class variables
public double getTimes(ThreeDLocation L)
{
double distance = L.getDistance();
int terrain = L.getTerrain();
//formula to calculate the terrain difficulty fraction
double terdiffFraction=(terrain/(getHandling()+1));
//formula to calculate the distance to the speed and acceleration
double distbyspeedaccel=10*distance/(getSpeed()*acceleration);
//Formula to calculate the min function
double minfunct=Math.min(Math.floor(distance/5), boost);
//formula to calculate the time
double time = Math.max(0,((Math.floor(distbyspeedaccel)*terdiffFraction)-(minfunct/2)));
return time;
}
}
-----------------------------------------------------------------------------------
Sample Output:
Please enter the speed of buggy 1.
50
Please enter the handling of buggy 1.
6
Please enter the acceleration(1-10) of buggy 1.
5
Please enter the boosters(0-3) of buggy 1.
1
Please enter the x value of the first location
8
Please enter the y value of the first location
9
Please enter the z value of the first location
10
Please enter the terrain value of the first location
20
For the first location
The time for mb1 is 0.0
The time for mb2 is 312.5
The time for mb3 is Infinity
Moonbuggy1 Wins
For the second location
The time for mb1 is 0.0
The time for mb2 is 81.25
The time for mb3 is Infinity
Moonbuggy1 Wins
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.