Automobile.java
public class Automobile
{
//Declaring instance variables
private int id;
private String make;
private String model;
private String color;
private int year;
private int vin_number;
private int miles_per_gallon;
private int speed;
//Parameterized constructor
public Automobile(int id, String make, String model, String color,
int year, int vin_number, int miles_per_gallon, int speed) {
super();
//Calling the setters methods by passing the parameters
setId(id);
setYear(year);
setMiles_per_gallon(miles_per_gallon);
setMake(make);
setModel(model);
setColor(color);
setVin_number(vin_number);
setSpeed(0);
}
//Setters and getters
public int getId() {
return id;
}
public void setId(int id) {
if(id<0 || id >9999)
{
this.id=0;
}
else
{
this.id = id;
}
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getYear() {
return year;
}
public void setYear(int year) {
if(year<2000 || year>2017)
{
this.year=0;
}
else
{
this.year = year;
}
}
public int getVin_number() {
return vin_number;
}
public void setVin_number(int vin_number) {
this.vin_number = vin_number;
}
public int getMiles_per_gallon() {
return miles_per_gallon;
}
public void setMiles_per_gallon(int miles_per_gallon) {
if(miles_per_gallon<10 || miles_per_gallon>60)
{
this.miles_per_gallon=0;
}
else
{
this.miles_per_gallon = miles_per_gallon;
}
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed =speed;
}
//accelerate() method is used to increase the speed of the car by 5
public int accelerate()
{
setSpeed(getSpeed()+5);
return getSpeed();
}
//brake() method is used to decrease the speed of the car by 5
public int brake()
{
setSpeed(getSpeed()-5);
return getSpeed();
}
/*accelerate() method is used to increase
* the speed of the car based on the supplied speed value
*/
public int accelerate(int speed)
{
setSpeed(getSpeed()+speed);
return getSpeed();
}
/*brake() method is used to decrease
* the speed of the car based on the supplied speed value
*/
public int brake(int speed)
{
//calling the setters method
setSpeed(getSpeed()-speed);
return getSpeed();
}
}
____________________________________________
TestAutomobiles.java
public class TestAutomobiles {
public static void main(String[] args) {
System.out.println(\"______CAR 1______\");
//Creating the Automobile Class Object by passing the parameters as input
Automobile am1=new Automobile(111,\"Ford\",\"Ford Fiesta\",\"Black\",2014,233,20,50);
//Displaying the make of the car
System.out.println(\"Car Make:\"+am1.getMake());
//Displaying the Model of the car
System.out.println(\"Car Model:\"+am1.getModel());
//Displaying the year
System.out.println(\"Car Model:\"+am1.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out..
Automobile.javapublic class Automobile { Declaring instan.pdf
1. Automobile.java
public class Automobile
{
//Declaring instance variables
private int id;
private String make;
private String model;
private String color;
private int year;
private int vin_number;
private int miles_per_gallon;
private int speed;
//Parameterized constructor
public Automobile(int id, String make, String model, String color,
int year, int vin_number, int miles_per_gallon, int speed) {
super();
//Calling the setters methods by passing the parameters
setId(id);
setYear(year);
setMiles_per_gallon(miles_per_gallon);
setMake(make);
setModel(model);
setColor(color);
setVin_number(vin_number);
setSpeed(0);
}
//Setters and getters
public int getId() {
return id;
}
public void setId(int id) {
if(id<0 || id >9999)
2. {
this.id=0;
}
else
{
this.id = id;
}
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getYear() {
return year;
}
public void setYear(int year) {
if(year<2000 || year>2017)
{
this.year=0;
}
else
{
3. this.year = year;
}
}
public int getVin_number() {
return vin_number;
}
public void setVin_number(int vin_number) {
this.vin_number = vin_number;
}
public int getMiles_per_gallon() {
return miles_per_gallon;
}
public void setMiles_per_gallon(int miles_per_gallon) {
if(miles_per_gallon<10 || miles_per_gallon>60)
{
this.miles_per_gallon=0;
}
else
{
this.miles_per_gallon = miles_per_gallon;
}
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed =speed;
}
//accelerate() method is used to increase the speed of the car by 5
public int accelerate()
{
setSpeed(getSpeed()+5);
return getSpeed();
}
4. //brake() method is used to decrease the speed of the car by 5
public int brake()
{
setSpeed(getSpeed()-5);
return getSpeed();
}
/*accelerate() method is used to increase
* the speed of the car based on the supplied speed value
*/
public int accelerate(int speed)
{
setSpeed(getSpeed()+speed);
return getSpeed();
}
/*brake() method is used to decrease
* the speed of the car based on the supplied speed value
*/
public int brake(int speed)
{
//calling the setters method
setSpeed(getSpeed()-speed);
return getSpeed();
}
}
____________________________________________
TestAutomobiles.java
public class TestAutomobiles {
public static void main(String[] args) {
System.out.println("______CAR 1______");
//Creating the Automobile Class Object by passing the parameters as input
Automobile am1=new Automobile(111,"Ford","Ford Fiesta","Black",2014,233,20,50);
5. //Displaying the make of the car
System.out.println("Car Make:"+am1.getMake());
//Displaying the Model of the car
System.out.println("Car Model:"+am1.getModel());
//Displaying the year
System.out.println("Car Model:"+am1.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am1.accelerate());
System.out.println("After Acceleration, Speed :"+am1.accelerate());
System.out.println("After Acceleration, Speed :"+am1.accelerate());
//Displaying the Speed of the car after applying the brakes
System.out.println("After Applying Brakes, Speed :"+am1.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am1.accelerate(30));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am1.brake(15));
System.out.println(" ______CAR 2______");
//Creating the Automobile Class Object by passing the parameters as input
Automobile am2=new Automobile(222,"Honda","Verna","White",2016,450,18,30);
//Displaying the make of the car
System.out.println("Car Make:"+am2.getMake());
//Displaying the Model of the car
System.out.println("Car Model:"+am2.getModel());
//Displaying the year
6. System.out.println("Car Model:"+am2.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
//Displaying the Speed of the car after applying the brakes
System.out.println("After Applying Brakes, Speed :"+am2.brake());
System.out.println("After Applying Brakes, Speed :"+am2.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am2.accelerate(40));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am2.brake(20));
System.out.println(" ______CAR 3______");
Automobile am3=new Automobile(333,"Honda","BRIO","GREY",2015,347,15,45);
//Displaying the make of the car
System.out.println("Car Make:"+am3.getMake());
//Displaying the Model of the car
System.out.println("Car Model:"+am3.getModel());
//Displaying the year
System.out.println("Car Model:"+am3.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am3.accelerate());
System.out.println("After Acceleration, Speed :"+am3.accelerate());
System.out.println("After Acceleration, Speed :"+am3.accelerate());
//Displaying the Speed of the car after applying the brakes
7. System.out.println("After Applying Brakes, Speed :"+am3.brake());
System.out.println("After Applying Brakes, Speed :"+am3.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am3.accelerate(50));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am3.brake(35));
}
}
___________________________________________
Output:
______CAR 1______
Car Make:Ford
Car Model:Ford Fiesta
Car Model:2014
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Applying Brakes, Speed :10
After Acceleration, Speed :40
After Applying Brakes, Speed :25
______CAR 2______
Car Make:Honda
Car Model:Verna
Car Model:2016
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Acceleration, Speed :20
After Applying Brakes, Speed :15
After Applying Brakes, Speed :10
After Acceleration, Speed :50
After Applying Brakes, Speed :30
______CAR 3______
8. Car Make:Honda
Car Model:BRIO
Car Model:2015
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Applying Brakes, Speed :10
After Applying Brakes, Speed :5
After Acceleration, Speed :55
After Applying Brakes, Speed :20
___________________________________________Thank You
Solution
Automobile.java
public class Automobile
{
//Declaring instance variables
private int id;
private String make;
private String model;
private String color;
private int year;
private int vin_number;
private int miles_per_gallon;
private int speed;
//Parameterized constructor
public Automobile(int id, String make, String model, String color,
int year, int vin_number, int miles_per_gallon, int speed) {
super();
//Calling the setters methods by passing the parameters
setId(id);
setYear(year);
setMiles_per_gallon(miles_per_gallon);
10. public void setColor(String color) {
this.color = color;
}
public int getYear() {
return year;
}
public void setYear(int year) {
if(year<2000 || year>2017)
{
this.year=0;
}
else
{
this.year = year;
}
}
public int getVin_number() {
return vin_number;
}
public void setVin_number(int vin_number) {
this.vin_number = vin_number;
}
public int getMiles_per_gallon() {
return miles_per_gallon;
}
public void setMiles_per_gallon(int miles_per_gallon) {
if(miles_per_gallon<10 || miles_per_gallon>60)
{
this.miles_per_gallon=0;
}
else
{
this.miles_per_gallon = miles_per_gallon;
}
}
public int getSpeed() {
11. return speed;
}
public void setSpeed(int speed) {
this.speed =speed;
}
//accelerate() method is used to increase the speed of the car by 5
public int accelerate()
{
setSpeed(getSpeed()+5);
return getSpeed();
}
//brake() method is used to decrease the speed of the car by 5
public int brake()
{
setSpeed(getSpeed()-5);
return getSpeed();
}
/*accelerate() method is used to increase
* the speed of the car based on the supplied speed value
*/
public int accelerate(int speed)
{
setSpeed(getSpeed()+speed);
return getSpeed();
}
/*brake() method is used to decrease
* the speed of the car based on the supplied speed value
*/
public int brake(int speed)
{
//calling the setters method
12. setSpeed(getSpeed()-speed);
return getSpeed();
}
}
____________________________________________
TestAutomobiles.java
public class TestAutomobiles {
public static void main(String[] args) {
System.out.println("______CAR 1______");
//Creating the Automobile Class Object by passing the parameters as input
Automobile am1=new Automobile(111,"Ford","Ford Fiesta","Black",2014,233,20,50);
//Displaying the make of the car
System.out.println("Car Make:"+am1.getMake());
//Displaying the Model of the car
System.out.println("Car Model:"+am1.getModel());
//Displaying the year
System.out.println("Car Model:"+am1.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am1.accelerate());
System.out.println("After Acceleration, Speed :"+am1.accelerate());
System.out.println("After Acceleration, Speed :"+am1.accelerate());
//Displaying the Speed of the car after applying the brakes
System.out.println("After Applying Brakes, Speed :"+am1.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am1.accelerate(30));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am1.brake(15));
13. System.out.println(" ______CAR 2______");
//Creating the Automobile Class Object by passing the parameters as input
Automobile am2=new Automobile(222,"Honda","Verna","White",2016,450,18,30);
//Displaying the make of the car
System.out.println("Car Make:"+am2.getMake());
//Displaying the Model of the car
System.out.println("Car Model:"+am2.getModel());
//Displaying the year
System.out.println("Car Model:"+am2.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
System.out.println("After Acceleration, Speed :"+am2.accelerate());
//Displaying the Speed of the car after applying the brakes
System.out.println("After Applying Brakes, Speed :"+am2.brake());
System.out.println("After Applying Brakes, Speed :"+am2.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am2.accelerate(40));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am2.brake(20));
System.out.println(" ______CAR 3______");
Automobile am3=new Automobile(333,"Honda","BRIO","GREY",2015,347,15,45);
//Displaying the make of the car
System.out.println("Car Make:"+am3.getMake());
14. //Displaying the Model of the car
System.out.println("Car Model:"+am3.getModel());
//Displaying the year
System.out.println("Car Model:"+am3.getYear());
//Displaying the Speed of the car after applying the acceleration
System.out.println("After Acceleration, Speed :"+am3.accelerate());
System.out.println("After Acceleration, Speed :"+am3.accelerate());
System.out.println("After Acceleration, Speed :"+am3.accelerate());
//Displaying the Speed of the car after applying the brakes
System.out.println("After Applying Brakes, Speed :"+am3.brake());
System.out.println("After Applying Brakes, Speed :"+am3.brake());
//Displaying the Speed of the car after applying the acceleration by passing the speed value
System.out.println("After Acceleration, Speed :"+am3.accelerate(50));
//Displaying the Speed of the car after applying the brakes by passing the speed value
System.out.println("After Applying Brakes, Speed :"+am3.brake(35));
}
}
___________________________________________
Output:
______CAR 1______
Car Make:Ford
Car Model:Ford Fiesta
Car Model:2014
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Applying Brakes, Speed :10
After Acceleration, Speed :40
After Applying Brakes, Speed :25
15. ______CAR 2______
Car Make:Honda
Car Model:Verna
Car Model:2016
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Acceleration, Speed :20
After Applying Brakes, Speed :15
After Applying Brakes, Speed :10
After Acceleration, Speed :50
After Applying Brakes, Speed :30
______CAR 3______
Car Make:Honda
Car Model:BRIO
Car Model:2015
After Acceleration, Speed :5
After Acceleration, Speed :10
After Acceleration, Speed :15
After Applying Brakes, Speed :10
After Applying Brakes, Speed :5
After Acceleration, Speed :55
After Applying Brakes, Speed :20
___________________________________________Thank You