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

This should contain a private array of Car objects Write a constructor that acce

ID: 3829593 • Letter: T

Question

This should contain a private array of Car objects Write a constructor that accepts an integer to set the size of the array. Write the following public methods isFull () - returns a boolean value indicating if array is full. readFile () - accepts a String parameter (the file name) and uses a Scanner to read in all the lines, create Car objects, and put them in the array, displayMake () - accepts a string parameter and prints all cars with that make. mpgRange () - accepts two double parameters and prints all the cars whose mpg falls in that range, weightRange () - like mpg, but for weight. displayAll () - display all the cars in the database.

Explanation / Answer


public class Car {
  
   double mpg;
   double weight;
   String make;
   String carName;
  
   public Car(double mpg, double weight, String make, String carName) {
       super();
       this.mpg = mpg;
       this.weight = weight;
       this.make = make;
       this.carName = carName;
   }

   public double getMpg() {
       return mpg;
   }

   public void setMpg(float mpg) {
       this.mpg = mpg;
   }

   public double getWeight() {
       return weight;
   }

   public void setWeight(float weight) {
       this.weight = weight;
   }

   public String getMake() {
       return make;
   }

   public void setMake(String make) {
       this.make = make;
   }

   public String getCarName() {
       return carName;
   }

   public void setCarName(String carName) {
       this.carName = carName;
   }
  
   public void print(){
       System.out.println(carName+", Make: "+make+", MPG: "+mpg+", Weight: "+weight);
   }
  

}

//----------------------------------------------------------------------------------------------------------

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CarDatabase {
  
   private Car[] c;
   private int n;

   public CarDatabase(int n) {
       super();
       this.c = new Car[n];
       this.n = n;
   }
  
   public boolean isFull(){
       return c.length==n ? true: false;
   }
  
   public void readFile(String f){
       File file = new File(f);
       int i=0;
       try {

       Scanner sc = new Scanner(file);
       //assume each line in the file has the name,make,weight,mpg as
       //comma separated value in that order
       while (sc.hasNextLine() && i<n) {
       String s = sc.nextLine();
       String[]tmp =s.split("'");
       float w = Float.parseFloat(tmp[2]);
       float mpg = Float.parseFloat(tmp[3]);
       c[i] = new Car(mpg,w,tmp[1],tmp[0]);
       i++;
      
       }
       sc.close();
       }
       catch (FileNotFoundException e) {
       e.printStackTrace();
       }
   }
  
   public void mpgRange(double a, double b){
       for(Car k:c){
           if(k.getMpg()>=a && k.getMpg()<=b)
               k.print();
       }
   }
  
   public void weightRange(double a, double b){
       for(Car k:c){
           if(k.getWeight()>=a && k.getWeight()<=b)
               k.print();
       }
   }
  
   public void displayAll(){
       for(Car k:c){
               k.print();
       }
   }

}

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