0% found this document useful (0 votes)
22 views4 pages

ArrayList of Objects in Java (Car Class)

The document describes a Java program that creates an ArrayList to store objects of the Car class. The program reads data from several cars from the user and saves them in the ArrayList. Then, the program uses the ArrayList to display the entered cars, the cars of a specific brand, the cars with less than a certain number of kilometers, the car with the most kilometers, and the cars sorted by the number of kilometers from least to most.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

ArrayList of Objects in Java (Car Class)

The document describes a Java program that creates an ArrayList to store objects of the Car class. The program reads data from several cars from the user and saves them in the ArrayList. Then, the program uses the ArrayList to display the entered cars, the cars of a specific brand, the cars with less than a certain number of kilometers, the car with the most kilometers, and the cars sorted by the number of kilometers from least to most.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

29/3/2015 Java Programming: ArrayList of Objects in Java

ArrayList of Objects in Java

In this entry we are going to write a Java program that creates an ArrayList of Objects of type Car.
the program prompts for the car data through the keyboard and stores it in the array. It will then use the
ArrayList to display the following on screen:
- All the cars introduced.
- All cars of a certain brand.
- All cars with less than a certain number of kilometers.
- The car with the highest number of kilometers.
- All cars sorted by number of kilometers from least to most.
First we create the Car class:
//Car Class
public class Car {
private String enrollment;
private String brand;
private String model;
private int Km;

public int getKm() {


return Km;
}

public void setKm(int Km) {


this.Km = Km;
}

public String getBrand() {


return brand;
}

public void setBrand(String brand) {


this.brand = brand;
}

public String getMatricula() {


return enrollment;
}

public void setRegistration(String registration) {


this.matricula = matricula;
}

public String getModel() {


return model;
}

public void setModel(String model) {


model
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
data:text/html;charset=utf-8,<div class="fauxcolumn-outer fauxcolumn-left-outer" style="position: absolute;… 1/4
March 29, 2015 Java Programming: ArrayList of Objects in Java

Registration:
sb.append(matricula);
Brand:
sb.append(brand);
Model:
sb.append(model);
Km:
sb.append(Km);
return sb.toString();
}
}

Next, we create the main class of the project:

//main class
public class Basic1 {

An ArrayList is created to store objects of type Car.


static ArrayList<Car> cars = new ArrayList<Car>();
static Scanner sc = new Scanner(System.in);
//main method
public static void main(String[] args) {
readCars()
Cars introduced:
showCars();
showByBrand();
showByKm();
Car with the highest number of Km:
Cars sorted from lowest to highest number of Km
showOrderedByKm();
}//end main method

Method to read cars and introduce them into the array


public static void readCars() {
//Variable declaration to read car data
String enrollment;
String brand;
String model;
int Km;
//Auxiliary variable that will contain the reference to each new car.
Car to;
int i, N;
The number of cars to read is requested via keyboard.
do {
System.out.print("Number of cars? ");
N = sc.nextInt();
} while (N < 0);
sc.nextLine(); //clear the input
reading of N cars
for (i = 1; i <= N; i++) {
read data from each car
System.out.println("Car " + i);
Enrollment:
registration = sc.nextLine();
System.out.print("Brand: ");
brand = sc.nextLine();
System.out.print("Model: ");
model = sc.nextLine();
System.out.print("Number of Kilometers: ");

data:text/html;charset=utf-8,<div class="fauxcolumn-outer fauxcolumn-left-outer" style="position: absolute;… 2/4


March 29, 2015 Java Programming: ArrayList of Objects in Java

Km = sc.nextInt();
sc.nextLine(); //clear the input
aux = new Car(); //A Car object is created and
its reference is assigned to aux
values are assigned to the attributes of the new
object
aux.setRegistration(matricula);
aux.setBrand(brand);
aux.setModel(model);
aux.setKm(Km);

the object is added to the end of the array


cars.add(aux);
}
}//end of readCars() method

We can graphically represent the contents of the ArrayList as the objects are introduced:

Method to show all cars


public static void showCars() {
for(int i = 0; i < cars.size(); i++)
System.out.println(cars.get(i)); // the toString method of the Car class is invoked
}

//Method to show all the cars of a brand requested via keyboard


March 29, 2015 Java Programming: ArrayList of Objects in Java

public static void showByBrand() {


String brand;
System.out.print("Enter brand: ");
brand = sc.nextLine();
System.out.println("Cars of the brand " + brand);
for(int i = 0; i < cars.size(); i++){
if(cars.get(i).getBrand().equalsIgnoreCase(brand))
System.out.println(cars.get(i));
}
}

Method to show all cars with a mileage lower than


to the number of Km requested by keyboard
public static void showByKm() {
int Km;
System.out.print("Enter number of kilometers: ");
Km = sc.nextInt();
Cars with less than
for(int i = 0; i < cars.size(); i++){
if(cars.get(i).getKm() < Km)
System.out.println(cars.get(i));
}
}

//Method that returns the Car with the highest number of Km


public static Car showHighestKm() {
Car mayor = cars.get(0);
for(int i = 0; i < cars.size(); i++){
if(cars.get(i).getKm() > greater.getKm())
mayor = cars.get(i);
}
return mayor;
}

Method that shows the cars sorted by number of Km from lowest to highest
public static void showSortedByKm() {
int i, j;
Car aux;
for(i = 0; i < cars.size() - 1; i++)
for(j=0;j<cars.size()-i-1;j++)
if(cars.get(j+1).getKm() < cars.get(j).getKm()){
aux = cars.get(j + 1);
cars.set(j+1, cars.get(j));
cars.set(j, aux);
}
showCars();
}
} // end of the main class

data:text/html;charset=utf-8,%3Cdiv%20class%3D%22fauxcolumn-outer%20fauxcolumn-left-outer%22%20style%3D%22position%3A%20absolute%3B%… 4/4

You might also like