IN JAVA CODE IN JAVA CODE Programming Challenge Car Race Si
IN JAVA CODE
IN JAVA CODE
Programming Challenge : Car Race Simulator
I) Write a class named Car that has the following fields (attributes):
year (an int which holds the car\'s year)
model (a String with holds the car\'s model)
make (a String which holds the make of the car)
speed (an int which holds the car\'s initial speed)
The Car class should have the following constructors and other methods:
Constructor - Accepts the car\'s year, model, make, and speed as arguments.
Default Constructor - Does not accept any input parameters, and uses data type defaults.
Accessors (getters) for the object\'s attributes and Modifiers (setters).
Methods:
1. accelerate - each time it is called, it should add a random number between 5 and 30, to the speed field
2. brake - each time it is called, it should subtract a random number between 5 and 30, to the speed field
II) Write a Driver class, the DrivingSimulation class, which does the following:
- Prompt the user for the year, model, make, and initial speed of car #1.
- Create a Car object #1 using the default constructor. Then use the setters to initialize it\'s values.
- Prompt the user for the year, model, make, and initial speed of car #2.
- Create a Car object #2 using the non-default constructor.
- Display the information for each car.
- Display an announcement that a race is about to begin between the 2 cars. Use your creativity regarding all messages displayed.
- Create a loop that will simulate racing around a track 5 times.
Within the loop, call the accelerate for each of the car objects, and after each call, use the accessor method to display the current speed of the car
Within the loop, call the brake for each of the car objects, and after each call, use the accessor method to display the current speed of the car
Within the loop, compare the speed for each car, and store the fastest speed for each car (hint: use 2 local variables in the driver to hold the fastest speed of each car. Compare current speed to fastest speed, at each loop iteration)
- At the end of the loop, display the fastest speed that each car reached. Make a conclusion about which car achieved the fastest speed, and report on it.
Solution
Ans:
import java.util.Random;
import java.util.Scanner;
class Car
{
private int year;
private String model;
private String make;
private int speed;
//parameterized constructor
Car(int year,String model,String make,int speed)
{
this.year=year;
this.model=model;
this.make=make;
this.speed=speed;
}
//default constructor
Car(){}
public void setYear(int year){
this.year=year;
}
public void setModel(String model){
this.model=model;
}
public void setMake(String make){
this.make=make;
}
public void setSpeed(int speed){
this.speed=speed;
}
public int getYear(){
return year;
}
public String getModel(){
return model;
}
public String getMake(){
return make;
}
public int getSpeed(){
return speed;
}
public void accelerate(){
Random r = new Random();
int ran=r.nextInt((30 - 5) + 1) + 5;
speed=speed+ran;
}
public void brake(){
Random r = new Random();
int ran=r.nextInt((30 - 5) + 1) + 5;
speed=speed-ran;
}//end of brake
}//end of car
public class Driver //Driver class
{
public static void main(String args[])
{
int year,speed;
String model,make;
int cs1;//to store speed of first car
int cs2;//to stror speed of second car
Scanner sc=new Scanner(System.in);
//Car1 details
System.out.println(\"Enter Year of car1\");
year=sc.nextInt();
System.out.println(\"Enter model of car1\");
model=sc.next();
System.out.println(\"Enter make of car1\");
make=sc.next();
System.out.println(\"Enter Speed of car1\");
speed=sc.nextInt();
Car c1=new Car();//car object1
//setter methods
c1.setYear(year);
c1.setModel(model);
c1.setMake(make);
c1.setSpeed(speed);
//Car2 details
System.out.println(\"Enter Year of car2\");
year=sc.nextInt();
System.out.println(\"Enter model of car2\");
model=sc.next();
System.out.println(\"Enter make of car2\");
make=sc.next();
System.out.println(\"Enter Speed of car2\");
speed=sc.nextInt();
Car c2=new Car(year,model,make,speed);
System.out.println(\"\ Car1 details\");//displaying car1 details
System.out.printf(\"Year=%d\ \",c1.getYear());
System.out.printf(\"Model=%s\ \",c1.getModel());
System.out.printf(\"Year=%s\ \",c1.getMake());
System.out.printf(\"Speed=%d\ \",c1.getSpeed());
System.out.println(\"\ Car2 details\");//displaying car2 details
System.out.printf(\"Year=%d\ \",c2.getYear());
System.out.printf(\"Model=%s\ \",c2.getModel());
System.out.printf(\"Year=%s\ \",c2.getMake());
System.out.printf(\"Speed=%d\ \",c2.getSpeed());
cs1=c1.getSpeed();//initial speed of car1
cs2=c2.getSpeed();//initial speed of car2
int cur1,cur2;
for(int i=1;i<=5;i++)//for loop
{
c1.accelerate();
c2.accelerate();
System.out.println(\"Speeds After Acceleration\");
System.out.println(\"Speed of First Car=\"+c1.getSpeed());
System.out.println(\"Speed of Second Car=\"+c2.getSpeed());
if(i==1)//first case
{
cs1=c1.getSpeed();
cs2=c2.getSpeed();
}//end of if
else
{
cur1=c1.getSpeed();
if(cs1<cur1)
cs1=cur1;
cur2=c2.getSpeed();
if(cs2<cur2)
cs2=cur2;
}//end of else
System.out.println(\"Speeds After Brake\");
c1.brake();
c2.brake();
System.out.println(\"Speed of First Car=\"+c1.getSpeed());
System.out.println(\"Speed of Second Car=\"+c2.getSpeed());
if(i==5)//last iteration
{
System.out.println(\"After 5 iterations\");
System.out.println(\"Fastest speed that car1 reached=\"+cs1);
System.out.println(\"Fastest speed that car2 reached=\"+cs2);
}
}//end of for loop
if(cs1>cs2)
System.out.println(\"Car1 has achieved the fastest speed\");
else
System.out.println(\"Car2 has achieved the fastest speed\");
}//end if main()
}//end of Driver class



