Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are these questions answered currently but none of them work eithe they ar

ID: 3739052 • Letter: T

Question

There are these questions answered currently but none of them work eithe they are not compiling or they compile but then show an error about missing a public static void main Strong args []

Create an Automobile class for a dealership. Include fields for an ID number, make, model, color, year, vin number, miles per gallon, and speed. Include get and set methods for each field. Do not allow the ID to be negative or more than 9999; if it is, set the ID to 0. Do not allow the year to be earlier than 2000 or later than 2017; if it is, set the year to 0. Do not allow the miles per gallon to be less than 10 or more than 60; if it is, set the miles per gallon to 0. Car speed should be initialized as 0. Include a constructor that accepts arguments for each field value and uses the set methods to assign the values. Also write two methods, Accelerate () and Brake (). Whenever Accelerate () is called, increase the speed by 5, and whenever Brake () is called, decrease the speed by 5. To allow users to specify the speed to increase (or decrease), create two overloading methods for Accelerate () and Brake () that accept a single parameter specifying the increasing (or decreasing) speed. Write an application that declares several Automobile objects and demonstrates that all the methods work correctly. Save the files as Automobile.java and TestAutomobiles.java.

Explanation / Answer

/**
* The java class test program that test the
* methods of the Automobile class and
* print results to console
* */
//TestAutomobiles.java
public class TestAutomobiles {
   public static void main(String[] args) {  
       //Create an instance of Automobile object
       Automobile obj1=new Automobile(1111, "Honda", "Swift", "Red", 2010, 5245, 10);
      
       System.out.println("Automobile - obj1");
       System.out.println("id_number "+obj1.getid());
       System.out.println("Make "+obj1.getmake());
       System.out.println("Model "+obj1.getmodel());
       System.out.println("Color "+obj1.getcolor());
       System.out.println("year "+obj1.getyear());
       System.out.println("vin number "+obj1.getvin());
       System.out.println("mpg "+obj1.getmpg());
      
       obj1.Accelerate();
       System.out.println("Accelerate ,Speed : "+obj1.getspeed());

       obj1.Accelerate(10);      
       System.out.println("Accelerate ,10,Speed : "+obj1.getspeed());
      
       System.out.println("Apply Brake()");
       obj1.Brake();
       System.out.println("speed : "+obj1.getspeed());
      
       System.out.println("Apply Brake(10)");
       obj1.Brake(10);
       System.out.println("speed : "+obj1.getspeed());

      
   }

}

--------------------------------------------------------------------------

//Automobile.java
public class Automobile {

   //declare variables
   private int id_number;
   private String make;
   private String model;
   private String color;
   private int year;
   private int vin_number;
   private int miles_per_gallon;
   private int speed;
   //Create a constructor that sets the instance variabels of class
   public Automobile(int id_number,String make,String model,
           String color, int year, int vin_number, int miles_per_gallon) {
       setid(id_number);
       setmake(make);
       setmodel(model);
       setcolor(color);
       setyear(year);
       setvin(vin_number);
       setmpg(miles_per_gallon);
       this.speed=0;
   }

   //Setter and Getter methos of the instance varaibles
   private void setid(int id_number) {
       if(id_number<0 || id_number>9999)
           this.id_number=0;
       else
           this.id_number=id_number;      
   }
  
   public int getid()
   {
       return id_number;
   }

   private void setmake(String make) {
       this.make=make;
   }
  
   public String getmake()
   {
       return make;
   }


   private void setmodel(String model) {
       this.model=model;
   }
  
   public String getmodel()
   {
       return model;
   }

   private void setcolor(String color) {
       this.color=color;
   }
  
   public String getcolor()
   {
       return color;
   }

   private void setyear(int year) {
       if(year<2000 || year>2017)
           this.year=0;
       else
           this.year=year;
   }
  
   public int getyear()
   {
       return year;
   }
  

   private void setvin(int vin_number) {
       this.vin_number=vin_number;      
   }
  
   public int getvin()
   {
       return vin_number;
   }

   private void setmpg(int miles_per_gallon) {
       if(miles_per_gallon<10 || miles_per_gallon>60)
           miles_per_gallon=0;
       else
           this.miles_per_gallon=miles_per_gallon;

   }
  
   public int getmpg()
   {
       return miles_per_gallon;
   }
  
   public int getspeed()
   {
       return speed;
   }
  
   public void Accelerate()
   {
       speed=speed+5;
   }
  
   public void Accelerate(int acceleration)
   {
       speed=speed+acceleration;
   }
  

   public void Brake()
   {
       speed=speed-5;
   }
  
   public void Brake(int brake)
   {
       speed=speed-brake;
   }
  
  

}

--------------------------------------------------------------------------

Sample Output:

Automobile - obj1
id_number 1111
Make Honda
Model Swift
Color Red
year 2010
vin number 5245
mpg 10
Accelerate ,Speed : 5
Accelerate ,10,Speed : 15
Apply Brake()
speed : 10
Apply Brake(10)
speed : 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote