You can use this assignment in your class!
Mastery Progress(100.0%)
i

Unit 5: Writing Classes - Review Assignment

Create multiple getter methods for private fields

Create a class named Book with two private fields: title of type String and author of type String. Write getter methods getTitle and getAuthor to retrieve the values of title and author respectively.

You answeredJUST NOW
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Create a Book object
Book book = new Book(in.nextLine(), in.nextLine());
// Use the getters to print the title and author
System.out.println(book.getTitle());
System.out.println(book.getAuthor());
}
}
// Write your Book class here
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You passed all test cases!
STDIN
To Kill a Mockingbird
Harper Lee
Expected STDOUT
To Kill a Mockingbird
Harper Lee
Your STDOUT
To Kill a Mockingbird
Harper Lee
STDIN
1984
George Orwell
Expected STDOUT
1984
George Orwell
Your STDOUT
1984
George Orwell
STDIN
The Great Gatsby
F. Scott Fitzgerald
Expected STDOUT
The Great Gatsby
F. Scott Fitzgerald
Your STDOUT
The Great Gatsby
F. Scott Fitzgerald
STDIN
Moby Dick
Herman Melville
Expected STDOUT
Moby Dick
Herman Melville
Your STDOUT
Moby Dick
Herman Melville

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Alec KretchAPR 22, 2024, 6:54:39 PM
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Create a Book object
Book book = new Book(in.nextLine(), in.nextLine());
// Use the getters to print the title and author
System.out.println(book.getTitle());
System.out.println(book.getAuthor());
}
}
class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return this.title;
}
public String getAuthor() {
return this.author;
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Was this helpful?

(Voting helps us personalize your learning experience!)
Think you've got it?

How many getter methods should a class have?

Select one of the following options:
  • A.

    None, private fields should not be accessed.

  • B.

    Only one, which returns all the private fields.

  • C.

    One for each private field that needs to be accessed.

  • D.

    As many as there are public fields.

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

You may exit out of this review and return later without penalty.