Book class with title and author fieldsLibrary class with a list to hold Book objectsLibrary to add a BookLibrary to print all books with their titles and authorsJump into concepts and practice - no test required
Book class with title and author fieldsLibrary class with a list to hold Book objectsLibrary to add a BookLibrary to print all books with their titles and authorsBook with two String fields: title and author. Add a constructor that sets these fields.Think of a book as having a name and a writer. Use a constructor to set these when you create a book.
Library with a field books that is an ArrayList<Book>. Initialize this list in the constructor.Use ArrayList<Book> to hold many books. Initialize it inside the constructor.
Library class, add a method called addBook that takes a Book object as a parameter and adds it to the books list.Use the add method of ArrayList to add the book.
Library class, add a method called printBooks that loops over the books list and prints each book's title and author in the format: Title: [title], Author: [author]. Then, in the main method, create a Library object, add two Book objects with titles "1984" and "Animal Farm" by author "George Orwell", and call printBooks.Use a for-each loop to go through each book and print its details. Then create the library and books in main.
What does it mean when two objects in Java interact?
Choose the best explanation.
Which of the following is the correct syntax to call a method start() on an object car in Java?
objectName.methodName();.Consider the following Java code:
class Light {
boolean isOn = false;
void toggle() {
isOn = !isOn;
}
boolean status() {
return isOn;
}
}
class Room {
Light light = new Light();
void switchLight() {
light.toggle();
}
boolean lightStatus() {
return light.status();
}
}
public class Main {
public static void main(String[] args) {
Room room = new Room();
System.out.println(room.lightStatus());
room.switchLight();
System.out.println(room.lightStatus());
}
}What is the output when this program runs?
isOn is false, so room.lightStatus() prints false.room.switchLight() toggles isOn to true. Then room.lightStatus() prints true.Find the error in this Java code snippet involving object interaction:
class Printer {
void print(String message) {
System.out.println(message);
}
}
public class Main {
public static void main(String[] args) {
Printer printer;
printer.print("Hello World");
}
}You have two classes, BankAccount and Customer. A Customer has a BankAccount object. You want to add a method transferTo in BankAccount that transfers money to another BankAccount. Which of the following best shows how objects interact to perform this transfer?
class BankAccount {
double balance;
void deposit(double amount) { balance += amount; }
void withdraw(double amount) { balance -= amount; }
void transferTo(BankAccount other, double amount) {
// Fill in this method
}
}
class Customer {
BankAccount account = new BankAccount();
}withdraw(amount) on this object and deposit(amount) on the other object shows interaction.