Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Example
- {
- public static void main (String[] args){
- Scanner input= new Scanner(System.in);
- // to declare an array of objects
- //declare a regular array: int [] nums = new int[10];
- Car [] cars = new Car[2];// really array of reference variables not really objects
- String temp,temp2;
- //assigning values
- //cars[0] = new Car(); // we could do the same for every reference in the array
- //instead for loop
- for (int i=0; i<cars.length; i++){
- System.out.println("enter car model");
- temp=input.next();
- cars[i]=new Car();
- cars[i].setModel(temp);
- System.out.println("enter car colour");
- temp2= input.next();
- cars[i].setColour(temp2);
- }
- for (int i =0; i<cars.length; i++){
- System.out.println("Model: "+cars[i].getModel());
- System.out.println("Colour: "+cars[i].getColour());
- }
- System.out.println("Search for a car colour");
- String search=input.next();
- for (int x=0; x<cars.length; x++){
- String res = cars[x].getColour();
- if (res.equals(search)){
- System.out.println("A "+cars[x].getColour()+" "+cars[x].getModel()+" found");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment