How do I search for the Brand \"Chevrolet\" in the collection in Java? Same with
ID: 3817799 • Letter: H
Question
How do I search for the Brand "Chevrolet" in the collection in Java? Same with Model "Sportscar"? It's at the end of the code.
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()
{
}
public void SearchByModel()
{
}
/********************************************************************
* OTHER METHODS *
******************************************************************/
public void addCars(Cars newCars)
{
cars[numCars] = newCars;
numCars++;
}
}
Explanation / Answer
Here is the probable code for you:
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++;
}
}
Hopefully, this works. If not please also give the Cars class so that we can compile the code here.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.