· Declare an array as Car with size 10.
· Take 10 Car’s information from user and store them in specified array.
· Call findCarList method from Main class to get all cars information related to a given model name and display then within this method.
· Model name should be taken from Main class and pass to findCarList method as argument as well as Car array (with size 10).
Design findCarList method in Car classas follows:
· it will take a model(car model) as parameter and array of cars (with size 10)
· displays List of cars for the given model from the list .
· If there are no cars, then shows “No cars found”.
import java.util.Scanner;
class App {
static void findCarList(String model, String cars[]) {
int c = 0;
for (int i = 0; i < cars.length; i++) {
if (model.compareToIgnoreCase(cars[i]) == 0) {
System.out.print(model);
c++;
}
}
if (c == 0) {
System.out.println("No cars found");
}
}
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
String cars[] = new String[10];
for (int i = 0; i < cars.length; i++) {
System.out.print("Enter model name " + (i + 1) + ": ");
cars[i] = keyBoard.nextLine();
}
System.out.print("Enter model name to search: ");
String model = keyBoard.nextLine();
findCarList(model, cars);
keyBoard.close();
}
}
Comments
Leave a comment