Question

In: Computer Science

its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...

its java language.

1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”); cars[4] = new Car(2010, “dodge”, null); (b) The other constructor takes one parameter: a Car, and puts into the array as the first (and only) element. 3. Create a method public void addCar(Car car) which adds the new Car to the first empty element in the array; if there is no empty element just System.out.println(“no room”) instead. 4. Create a method public Car getCar(int index) which returns a reference to the car at the specified array index; if the index is not valid (i.e. not 0 to 4) return null instead. 5. Create a method public Car getOldestCar() which returns a reference to the oldest car in the array. Use your Car class from the previous question. 2. Create a class called UsedCarLot which has an ArrayList of Car references. 3. The constructor takes parameter: the name of the UsedCarLot (e.g. “Jason’s Used Cars”)…store this in the instance variable String CarLotName and has an accessor too: public String getName(). Also the constructor does this: It simply populates the ArrayList with these cars: cars.add(new Car(2016, “honda”, “civic”)); cars.add(new Car(2017, “Lamborghini”, “aventador”)); cars.add(new Car(2000, null, “caravan”)); cars.add(new Car(2010, “dodge”, null)); 4. Create a method public void addCar(Car car) which adds the Car parameter to the ArrayList. 5. Create a method public void removeCarsBetween(int firstYear, int lastYear) which removes all Cars from the list which were manufactured between firstYear and lastYear (inclusive). Use an iterator. 6. Create a method public Car[] getCarsMadeBy(String maker) which returns an array of Car references…all of the Cars which are made by the parameter maker.

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Car

public class Car {
  
   private int year;
   private String make;
   private String model;
  
   public Car(int year, String make, String model) {
       this.year = year;
       this.make = make;
       this.model = model;
   }

   public int getYear() {
       return year;
   }

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

   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;
   }
}

CarLot

public class CarLot {

   private Car[] cars = new Car[5];

   public CarLot() {
       cars[0] = new Car(2016, "honda", "civic");
       cars[2] = new Car(2017, "Lamborghini", "aventador");
       cars[3] = new Car(2000, null, "caravan");
       cars[4] = new Car(2010, "dodge", null);
   }

   public CarLot(Car car) {
       cars[0] = car;
   }

   public void addCar(Car car) {
       boolean check = true;
       for (int i = 0; i < cars.length; i++) {
           if (cars[i] == null) {
               cars[i] = car;
               check = false;
               break;
           }
       }
       if (check)
           System.out.println("no room");
   }

   public Car getCar(int index) {
       if (index <= cars.length)
           return cars[index];
       return null;
   }

   public Car getOldestCar() {
       int oldYear = cars[0].getYear();
       Car oldCar = cars[0];

       for (int i = 1; i < cars.length; i++) {
           if (oldYear > cars[i].getYear()) {
               oldYear = cars[i].getYear();
               oldCar = cars[i];
           }
       }
       return oldCar;
   }
}

UsedCarLot

import java.util.ArrayList;
import java.util.Iterator;

public class UsedCarLot {

   private ArrayList<Car> cars = new ArrayList<>();
   private String carLotName;

   public UsedCarLot(String carLotName) {
       this.carLotName = carLotName;
       cars.add(new Car(2016, "honda", "civic"));
       cars.add(new Car(2017, "Lamborghini", "aventador"));
       cars.add(new Car(2000, null, "caravan"));
       cars.add(new Car(2010, "dodge", null));
   }
  
   public String getName() {
       return carLotName;
   }
  
   public void addCar(Car car) {
       cars.add(car);
   }
  
   public void removeCarsbetween(int firstYear, int lastYear) {
       Iterator<Car> itr = cars.iterator();
       Car temp;
       while(itr.hasNext()) {
           temp = itr.next();
           if(temp.getYear()>=firstYear && temp.getYear()<=lastYear)
               cars.remove(temp);
       }
   }
  
   public Car[] getCarsMadeBy(String maker) {
       int size=0;
       for(Car i: cars) {
           if(i.getMake().equalsIgnoreCase(maker))
               size++;
       }
      
       Car[] carAr = new Car[size];
       int index=0;
       for(Car i: cars) {
           if(i.getMake().equalsIgnoreCase(maker))
               carAr[index++] = i;
       }
      
       return carAr;
      
   }

}


Related Solutions

in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
1) Create a class called Employee that includes three instance variables — a first name (type...
1) Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. 2)Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating five...
1) Create a class called Employee that includes three instance variables — a first name (type...
1) Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. 2) Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
JAVA Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours,...
JAVA Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours, which represents the hours worked, and wage, which represents the employee's pay per hour. (Both are doubles.) Create a constructor that takes the arguments first name, last name, social security number, hourly wage, and the number of hours worked. Also create accessors, mutators, an earnings method that returns the money earned by the employee this week, and a toString method that returns information about...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT