// car class ----------------------------------
public class Car
{
private int yearModel;
private String make;
private int speed;
public Car(String m, int year)
{
yearModel = year;
make = m;
speed = 0;
}
// Declare mutators (methods).
// Declare accessors (methods).
public int getModel() // Model year of car.
{
return yearModel;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
public void setModel(int year)
{
yearModel = year;
}
public void setMake(String carMake)
{
make = carMake;
}
public void setSpeed(int s)
{
speed = s;
}
public void accelerateSpeed()
{
speed += 5;
// Each call will increase by 5.
}
public void brakeSpeed()
{
speed -= 5;
// Each call will decrease by 5.
}
}
public class Car
{
private int yearModel;
private String make;
private int speed;
public Car(String m, int year)
{
yearModel = year;
make = m;
speed = 0;
}
// Declare mutators (methods).
// Declare accessors (methods).
public int getModel() // Model year of car.
{
return yearModel;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
public void setModel(int year)
{
yearModel = year;
}
public void setMake(String carMake)
{
make = carMake;
}
public void setSpeed(int s)
{
speed = s;
}
public void accelerateSpeed()
{
speed += 5;
// Each call will increase by 5.
}
public void brakeSpeed()
{
speed -= 5;
// Each call will decrease by 5.
}
}
// CarResult calss ------------------------------------------------------------------------------
import javax.swing.JOptionPane;
import java.io.*;
public class CarResults {
public static void main(String[] args) throws IOException
{
String input, make;
int model, yearModel, year, speed;
Car myCar = new Car("Car", 2010);
// Retrieve car's Make & Model.
make = JOptionPane.showInputDialog("What is the Make of your car? ");
myCar.getMake();
year = Integer.parseInt(JOptionPane.showInputDialog("What is the Model Year of your car? "));
myCar.getModel();
input = JOptionPane.showInputDialog("Enter your car's speed: ");
speed = Integer.parseInt(input);
myCar.getSpeed();
for (int i=0;i<5;i++)
{
myCar.accelerateSpeed();
System.out.println();
System.out.println("The" + " " + myCar.getModel() + " " + myCar.getMake() +
" is gradually accelerating. " );
// Apply acceleration.
speed=speed+10;
System.out.println("Your current speed is: " + speed);
System.out.println();
}
// Begin applying brakes.
System.out.println("\t>>> Now, let's get the results for applying the brakes... ");
System.out.println();
for (int i=0;i<5;i++)
{
myCar.brakeSpeed();
System.out.println();
System.out.println("Your" + " " + myCar.getModel() + " " + myCar.getMake() + " is now traveling at: " );
// Apply brakes.
speed=speed-10;
System.out.println("Now your speed is: " + speed);
}
// End the program.
System.exit(0);
}
}
OUTPUT: