3.20 Tutorial 3
Task 7
Using ArrayList
from java.util
, define a class Catalog
that:
- Maintains a list of books.
- Allows adding and removing books.
- Prints all books in the catalog.
import java.util.ArrayList;
import java.util.Iterator;
public class Catalog
{
private ArrayList<String> books;
public Catalog()
{
books = new ArrayList<>();
}
public void addBook(String name)
{
books.add(name);
}
public void removeBook(String name)
{
books.remove(name);
}
public void printBookFor()
{
for(String bookName : books)
{
System.out.println(bookName);
}
}
public void printBookWhile()
{
int index = 0;
while(index < books.size())
{
System.out.println(books.get(index));
index++;
}
}
public void printBookIerator()
{
Iterator<String> it = books.iterator();
while(it.hasNext())
{
String t = it.next();
System.out.println(t);
}
}
}
Task 8
Complete all methods in the temperature measurements project provided with this guide.
Task 9
Extend the temperature measurements project by implementing:
- A method
countExtremeTemperatures()
to count temperatures above 39°C or below -15°C.
Task 10
Further extend the temperature measurements project by adding:
- A method that computes the number of consecutive measurements with the same temperature.
Task 11
Add a method countExtremeTemperatureChanges()
that detects extreme temperature changes, defined as:
-
A non-extreme temperature followed by an extreme one.
-
An extreme temperature followed by a non-extreme one.
-
A cold extreme followed by a hot extreme.
-
A hot extreme followed by a cold extreme.
Task 14
In music-organizer-v3, implement a method that allows playing all songs by a specific artist.
- Analyze the
MusicPlayer
class. - Use the
for-each
iteration pattern for selective processing.