An Array of Objects and ArrayList – Examples (Java)

Page 3 of 4 · Object-oriented programming – examples

When there are many objects, we keep them in an array or in an ArrayList. These tasks combine classes with loops: finding the best one, the average, searching and sorting.

An array of objects and a list of objects:

Student[] schoolClass = new Student[3];      // an array of 3 PLACES – the objects do not exist yet!
schoolClass[0] = new Student("Ana", 5);      // every object is created separately

ArrayList<Student> list = new ArrayList<>();  // a list changes its size by itself
list.add(new Student("Ivan", 4));
for (Student s : list) {                       // for-each through all elements
    System.out.println(s);
}
Before you start: read the lessons Arrays in Java, Objects and methods.
How to practise: write the class and the program on your own, run it on the example from the task, and only then open the hint, and finally the solution. All solutions are in Java; the helper classes are in the same file (without the word public), so the program runs as one file.
TASK 01Difficulty: easy

A class of students: the tallest student

Read the number of students n, and then for each of them a name and a height in cm. Keep the data in an array of objects of the class Student. Print the average height (one decimal place) and the name of the tallest student.

Example

Input

4
Ana 152
Ivan 160
Luka 149
Mara 158

Output

Average height: 154.8
Tallest: Ivan (160)
TASK 02Difficulty: medium

A library: searching by author

The class Book has a title, an author and a year of publication. Read n books (every piece of data in its own line) into an ArrayList, then an author's name, and print all of that author's books sorted by year (from the oldest). If there are none, print "No books".

Example

Input

3
Na Drini cuprija
Ivo Andric
1945
Prokleta avlija
Ivo Andric
1954
Seobe
Milos Crnjanski
1929
Ivo Andric

Output

1945 Na Drini cuprija
1954 Prokleta avlija

(The books in the example are classics of Serbian literature: "The Bridge on the Drina", "The Damned Yard" and "Migrations".)

TASK 03Difficulty: medium

A competition ranking list

For each of n contestants a name and the points on three tasks are read. Make a class Contestant with a method total(). Print the ranking list: place, name and total points, from the best. Contestants with the same number of points share a place, and they are ordered by name.

Example

Input

4
Luka 100 50 30
Ana 80 100 0
Ivo 90 80 0
Mia 100 80 0

Output

1. Ana 180
1. Luka 180
1. Mia 180
4. Ivo 170
TASK 04Difficulty: easy

A shopping basket

The class Item has a name, a price per piece and a quantity, and a method value(). Read items (name price quantity) until the name "end" is entered, keep them in a list, and then print the bill: every item with its value and, at the end, the total. If the total is greater than 5000, there is a 10% discount.

Example

Input

bread 80 2
milk 150 3
cheese 900 5
end

Output

bread 160
milk 450
cheese 4500
Total: 5110
With discount: 4599
TASK 05Difficulty: harder

A phone book: add, find, remove

Make a class Contact (name, phone) and a class PhoneBook that contains an ArrayList<Contact> and the methods add, find(name) and remove(name). The program reads the commands: A name phone (add), F name (find), R name (remove), E (end) and prints the result of every search.

Example

Input

A Ana 0641112233
A Ivan 0601234567
F Ana
R Ana
F Ana
F Ivan
E

Output

Ana: 0641112233
No contact: Ana
Ivan: 0601234567
Common mistakes and tips
  • new Student[5] creates only 5 empty places (null); accessing schoolClass[0].name before new Student(...) gives a NullPointerException.
  • A list is created with the type in angle brackets: ArrayList<Book>; you need import java.util.ArrayList;.
  • For the size: for an array array.length, for a list list.size().
  • To sort a list of objects use list.sort(Comparator.comparing(...)) or write the comparison yourself.

Check your knowledge

Open the quiz

Mini quiz: arrays of objects

1. What is in the array after Book[] b = new Book[3];?




2. How is an element added to an ArrayList?




3. How do you get the number of elements of a list?