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

How to search through collections to find car brand and car model (in this case)

ID: 3822181 • Letter: H

Question

How to search through collections to find car brand and car model (in this case).

The two classes out of the four I am working with include Cars and CarsCollection (ignore next in series for now).

You can tell I tried to figure it out but its saying brand is not resolved or a field.

=============================================================

public class Cars {
   /********************************************************************
   * ATTRIBUTES *
   ******************************************************************/
   private String brand;
   private int year;
   private String color;
   private String model;
   private String type;
   private String trans;
   private String drive;
  
     
   /********************************************************************
   * CONSTRUCTORS *
   ******************************************************************/
  
   //all necessary parameters for attributes
   public Cars(String brand_,
   int year_,
   String color_,
   String model_,
   String type_,
   String trans_,
   String drive_)
                     
   {
   this.brand = brand_;
   this.year = year_;
   this.color = color_;
   this.model = model_;
   this.type = type_;
   this.trans = trans_;
   this.drive = drive_;
  
   }
  
   //transmission, drivetrain, and top speed omitted
   public Cars(String brand_,
   int year_,
   String color_,
   String model_)
   {
   this.brand = brand_;
   this.year = year_;
   this.color = color_;
   this.model = model_;
   this.type = null;    //type omitted
   this.trans = null; // transmission omitted
   this.drive = null; // drivetrain omitted
     
   }
  
   //only brand name and year
   public Cars(String brand_, int year_)
   {
   this.brand = brand_;
   this.year = year_;
   this.color = null; // color omitted
   this.model = null; // model omitted
   this.type = null; // type omitted
   this.trans = null; // transmission omitted
   this.drive = null; // drivetrain omitted
  
   }

   /********************************************************************
   * GET and SET METHODS *
   ******************************************************************/
  
   public void setBrand(String brand_)
   {
   this.brand = brand_;
   }
   public String getBrand()
   {
   return this.brand;
   }
  
   public void setYear(int year_)
   {
   this.year = year_;
   }
   public int getYear()
   {
   return this.year;
   }

   public void setColor(String color_)
   {
   this.color = color_;
   }
   public String getColor()
   {
   return this.color;
   }
     
   public void setModel(String model_)
   {
   this.model = model_;
   }
   public String getModel()
   {
   return this.model;
   }
  
   public void setType(String type_)
   {
   this.type = type_;
   }
   public String getType()
   {
   return this.type;
   }

   public void setTrans(String trans_)
   {
   this.trans = trans_;
   }
   public String getTrans()
   {
   return this.trans;
   }

   public void setDrive(String drive_)
   {
   this.drive = drive_;
   }
   public String getDrive()
   {
   return this.drive;
   }
     

   /********************************************************************
   * OUTPUT METHODS *
   ******************************************************************/

   //Return a string with only the brand and the year
   public String description()
   {
   return this.brand + " -- " + "Year: " + this.year;
   }

   //Return the entire object as a single String
   public String toString()
   {
   String result = this.brand+" -- Year: " + this.year;
   if (this.color!=null)
   {
   result += " Color: "+color;
   result += " Model: "+this.model;
   }
   if (this.type!=null)
   {
   result += " Type: "+this.type;
   }
   if (this.trans!=null)
   {
   result += " Transmission: "+this.trans;
   }
   if (this.drive!=null)
   {
   result += " Drivetrain: "+this.drive;
   }
   result += " ";
   return result;
   }
   /********************************************************************
   * OTHER METHODS *
   ******************************************************************/

   /********************************************************************
   * createNextInSeries *
   * Creates a new car with the same entries, but with the year *
   * incremented. *
   ******************************************************************/
   public Cars createNextInSeries(){
       return new Cars(this.brand,this.year+1,this.color,this.model,this.type,
               this.trans,this.drive);
              
   }
}

====================================================================

public class CarsCollection
   {
   /********************************************************************
   * ATTRIBUTES *
   ******************************************************************/
   private String name;
   private Cars[] cars;
   private int numCars;
     
   /********************************************************************
   * CONSTRUCTORS *
   ******************************************************************/
   // all necessary parameters for attributes
   public CarsCollection(String name_, int size)
   {
   this.name = name_;
   cars = new Cars[size];
   numCars=0;
   }

   // missing collection size
   public CarsCollection(String name_)
   {
   this.name = name_;
   cars = new Cars[50];
   numCars=0;
   }

   // missing collection name
   public CarsCollection(int size)
   {
   this.name = "My car collection";
   cars = new Cars[size];
   numCars=0;
   }

   // no initial parameters
   public CarsCollection()
   {
   cars = new Cars[5];
   numCars=0;
   }
     
   /********************************************************************
   * GET and SET METHODS *
   ******************************************************************/
     
   // return a car given the index
   public Cars getCars(int index)
   {
   return cars[index];
   }
     
   // return the remaining space available in the cars array
   public int spaceAvailable()
   {
   return cars.length-numCars;
   }
  
   // return the number of cars in the array
   public int getnumCars()
   {
   return numCars;
   }
     

   /********************************************************************
   * OUTPUT METHODS *
   ******************************************************************/
   // print the entire collection, with title and summary information
   public void printCollection()
   {
   System.out.println(" Collection: "+this.name+" ");
   System.out.println("--------------------------------------------");
   for (int i = 0; i < numCars; i++)
   {
   System.out.println((i+1)+": "+this.getCars(i).toString());
   }
   System.out.println(numCars+" in collection.");
   System.out.println("--------------------------------------------");
   }
  
   // print a summary of the collection
   public void printSummary()
   {
   for (int i = 0; i < numCars; i++)
   {
   System.out.println(this.getCars(i).description());
   }
   }

   public void SearchByBrand()
   {
       for(int i = 0; i < numCars; i++)
       if(getCars(i).getBrand() == this.brand_)
       {
       System.out.println(brand + " found at index " + i);
       return;
       }
       System.out.println(brand + " is not part of collection.");
   }
     
   public void SearchByModel()
   {
     
   }
     
   /********************************************************************
   * OTHER METHODS *
   ******************************************************************/

   public void addCars(Cars newCars)
   {
   cars[numCars] = newCars;
   numCars++;
}

}

Explanation / Answer

Here is the code for CarsCollection.java:

public class CarsCollection
{
/********************************************************************
* ATTRIBUTES *
******************************************************************/
private String name;
private Cars[] cars;
private int numCars;

/********************************************************************
* CONSTRUCTORS *
******************************************************************/
// all necessary parameters for attributes
public CarsCollection(String name_, int size)
{
this.name = name_;
cars = new Cars[size];
numCars=0;
}
// missing collection size
public CarsCollection(String name_)
{
this.name = name_;
cars = new Cars[50];
numCars=0;
}
// missing collection name
public CarsCollection(int size)
{
this.name = "My car collection";
cars = new Cars[size];
numCars=0;
}
// no initial parameters
public CarsCollection()
{
cars = new Cars[50];
numCars=0;
}

/********************************************************************
* GET and SET METHODS *
******************************************************************/

// return a car given the index
public Cars getCars(int index)
{
return cars[index];
}

// return the remaining space available in the cars array
public int spaceAvailable()
{
return cars.length-numCars;
}
  
// return the number of cars in the array
public int getNumComics()
{
return numCars;
}

/********************************************************************
* OUTPUT METHODS *
******************************************************************/
// print the entire collection, with title and summary information
public void printCollection()
{
System.out.println(" Collection: "+this.name+" ");
System.out.println("--------------------------------------------");
for (int i = 0; i < numCars; i++)
{
System.out.println((i+1)+": "+this.getCars(i).toString());
}
System.out.println(numCars+" in collection.");
System.out.println("--------------------------------------------");
}
  
// print a summary of the collection
public void printSummary()
{
for (int i = 0; i < numCars; i++)
{
System.out.println(this.getCars(i).description());
}
}
public void SearchByBrand(String brandName)
{
    for(int i = 0; i < numCars; i++)
        if(getCars(i).getBrand() == brandName)
        {
            System.out.println(brandName + " found at index " + i);
            return;
        }
    System.out.println(brandName + " is not part of collection.");      
           
}

public void SearchByModel(String model)
{
    for(int i = 0; i < numCars; i++)
        if(getCars(i).getModel() == model)
        {
            System.out.println(model + " found at index " + i);
            return;
        }
    System.out.println(model + " is not part of collection.");
}

/********************************************************************
* OTHER METHODS *
******************************************************************/
public void addCars(Cars newCars)
{
cars[numCars] = newCars;
numCars++;
}
  
  
}

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