Hi can someone help me with this question. I really have no idea how to do it. A
ID: 663645 • Letter: H
Question
Hi can someone help me with this question. I really have no idea how to do it. AThanks! (Using java)
Construct a calling class and a Car class as follows:
Car Class:
Properties: Make (String), Year(int), Color(String), Speed (int)
Methods:
All getter, setter methods
Accelerate ( increase speed : 15MPH if year < 2010, 30 MPH >= 2010)
Brake ( lower speed : 5MPH if year < 2010, 10 MPH >= 2010, speed always >=0)
Stop (speed = 0)
Calling Class
Create an array of 10 cars. Give them random colors and makes. Select a random year for a car, but when done, half the cars should have a year >=2010. Initial Speed is zero.
Do the following 100 times
Pick a random car
randomly select Brake, Accelerate or Stop method (equal probabilities)
Invoke the selected method chosen
Finally, print out the car number, year, make, color and final speed.
Explanation / Answer
Here is the code for the question. Please do rate the answer if it helped. Thank you very much.
Car.java
public class Car {
private String make;
private int year;
private String color;
private int speed;
public Car()
{
make="";
year=0;
color="";
speed=0;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void accelerate()
{
if(year < 2010)
speed += 15;
else
speed +=30;
}
public void stop()
{
speed = 0;
}
public void brake()
{
if(year < 2010)
speed -= 5;
else
speed -= 10;
if(speed < 0)//speed reached a -ve number
speed = 0;
}
}
Calling.java
import java.util.Random;
public class Calling {
public static void main(String[] args) {
Car[] cars = new Car[10];
String colors[] = {"red","blue","black","white","gray"};
String make[] ={"Audi","Bugatti","Toyota Etios","Toyota Innova",};
Random random=new Random();
int j;
//create 5 cars with year less than 2010
for(int i=0;i<5; i++)
{
cars[i] = new Car();
j = random.nextInt(colors.length);
cars[i].setColor(colors[j]);
j = random.nextInt(make.length);
cars[i].setMake(make[j]);
j= 2000+random.nextInt(10);
cars[i].setYear(j);
cars[i].setSpeed(0);
}
//create 5 cars with year less than 2010
for(int i=5;i<10; i++)
{
cars[i] = new Car();
j = random.nextInt(colors.length);
cars[i].setColor(colors[j]);
j = random.nextInt(make.length);
cars[i].setMake(make[j]);
j= 2010+random.nextInt(10);
cars[i].setYear(j);
cars[i].setSpeed(0);
}
//do 100 times
for(int i=0;i<100; i++)
{
j = random.nextInt(10);
switch(random.nextInt(3))
{
case 0: cars[j].brake();
break;
case 1: cars[j].accelerate();
break;
case 2: cars[j].stop();
break;
}
}
for(int i=0;i<10;i++)
{
System.out.println((i+1)+". " + cars[i].getYear()+" "+cars[i].getMake()+" "+cars[i].getColor()+" "+cars[i].getSpeed());
}
}
}
output
1. 2007 Bugatti blue 15
2. 2006 Toyota Etios black 30
3. 2004 Audi red 10
4. 2007 Audi red 10
5. 2005 Audi white 15
6. 2010 Audi white 60
7. 2019 Audi white 60
8. 2015 Bugatti black 0
9. 2011 Toyota Innova gray 20
10. 2014 Bugatti blue 30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.