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

need help with a java program. this lab is over classes and objects. i have adde

ID: 3797472 • Letter: N

Question

need help with a java program. this lab is over classes and objects. i have added the scope of work for this lab and is in numerical order of steps we need to follow.

1. Define a Car class with three member field, String brand, String model, int year.

2. Define a constructor that takes arguments. For example, public Car(String brand, String model, int year);

3. In the constructor, use three assignment statements to assign three arguments values into three member fields as an initialization.

4. After the constructor, define three member methods (getBrand(), getModel(), getYear()) that return corresponding member field. For example, getBrand() function returns the value of variable Brand.

5. Note that Class Car doesn’t have main method.

6. Define another class names as Lab6 in a different file. For example, public class Lab6 { }

7. In the main method of the class Lab6, declare a Car instance variable, instantiate, and initialize three member field using the constructor. For example, Car myCar = new Car(“Toyota”, “Camry”, “2017”);

8. To verify the initialization, call all three member methods of class Car in the main method of class Lab6, and display the three values using the object and dot operator. For example, System.out.println(“Brand: ” + myCar.getBrand);

Explanation / Answer

Car.java

public class Car {
   //Declaring variables
   private String brand;
   private String model;
   private int year;

   //parameterized constructor
   public Car(String brand, String model, int year) {
       this.brand = brand;
       this.model = model;
       this.year = year;
   }

   //Setters and getters
   public String getBrand() {
       return brand;
   }

   public void setBrand(String brand) {
       this.brand = brand;
   }

   public String getModel() {
       return model;
   }

   public void setModel(String model) {
       this.model = model;
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }

  
}

___________________

Lab6.java


import java.util.Collections;

public class Lab6 {

   public static void main(String[] args) {
       //Creating an array of 6 Car Class Objects
   Car p[]={new Car("Ford","Ford Focus",2015),
           new Car("Honda","Honda Fit",2008),
           new Car("Ford","Ford Fiesta", 2000),
           new Car("Toyota yaris","Toyota",2010),
   new Car("Chevrolet","Chevy Sonic",2002),
   new Car("Hyundai","Hyundai Accent",2009),
   new Car("Toyota", "Camry", 2017)};
  
   //Displaying all the contents of all the Car Class objects
   for(Car car:p)
   {
       System.out.println("Brand :"+car.getBrand()+", Model :"+car.getModel()+", Year :"+car.getYear());
   }
  
   }

}

________________

Output:

Brand :Ford, Model :Ford Focus, Year :2015
Brand :Honda, Model :Honda Fit, Year :2008
Brand :Ford, Model :Ford Fiesta, Year :2000
Brand :Toyota yaris, Model :Toyota, Year :2010
Brand :Chevrolet, Model :Chevy Sonic, Year :2002
Brand :Hyundai, Model :Hyundai Accent, Year :2009
Brand :Toyota, Model :Camry, Year :2017

__________Thank You