JAVA
Submit your answers to the following questions: 1. Consider the following Java class (note the use of this in the setters): public class Book { private string title; private string author; public string getTitle() { return title; } public void setTitle(String title) { this.title= title; } public string getAuthor() { return author; } public void setAuthor (String author) { this.author = author; } What will the following code print when run? class Main { public static void main(String[] args) { Book b = new Book(); System.out.println(b.getTitle()); System.out.println(b.getAuthor()); 2. Using the same definition of Book as above, write the 3 lines of Java code you need to create a new Book titled "Where's Waldo?" by Martin Handford. 3. Now consider this different definition of the Book class (note the addition of a constructor): public class Book { private string title; private string author; public Book (String title, string author) { this.title = title; this.author = author; } public string getTitle() { return title; public string getAuthor() { return author; } What will the following code do? Why? class Main { public static void main(String[] args) { Book b = new Book(); System.out.println(b.getTitle()); System.out.println(b.getAuthor());
JAVA
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am