Composition Practice: Create two classes First class: Author Author has a first
ID: 3796923 • Letter: C
Question
Composition Practice:
Create two classes
First class: Author
Author has a first name, last name and email
Create two constructors: default and three argument
Create getters and setters
Override toString() to output is: “Author: Mark Twain jsmith@yahoo.com”
Second class: Book
Book has title and author
Create three constructors: default, four argument and two argument
Override toString() to output “Title: Tom Sawyer Author: Mark Twain jsmith@yahoo.com”
Make it run in attached driver:
Output example:
Title: Moby Dick Author: Herman Melville hmellive@hotmail.com
Please use "eclipse neon"
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
########## Author.java #################
public class Author {
// instance variable
private String firstName;
private String lastName;
private String email;
public Author() {
firstName = "";
lastName = "";
email = "";
}
public Author(String email, String firstName, String lastName) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
// getters and setters
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Author: "+firstName+" "+lastName+" "+email;
}
}
########## Book.java #################
public class Book {
// instance variable
private String title;
private Author author;
public Book() {
title = "";
author = new Author();
}
public Book(String title,String email, String firstName, String lastName){
author = new Author(email, firstName, lastName);
this.title = title;
}
public Book(String title, Author author){
this.title = title;
this.author = new Author(author.getEmail(), author.getFirstName(), author.getLastName());
}
// getters and setters
public String getTitle() {
return title;
}
public Author getAuthor() {
return author;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(Author author) {
this.author = author;
}
@Override
public String toString() {
return "Title: "+title+" "+author.toString();
}
}
########## BookDriver.java #################
import java.util.Scanner;
public class BookDriver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter title of the book: ");
String title = sc.nextLine();
System.out.print("Enter Author first name: ");
String firstName = sc.next();
System.out.print("Enter Author last name: ");
String lastName = sc.next();
System.out.print("Enter Author email: ");
String email = sc.next();
// creating a Book object
Book book = new Book(title, email, firstName, lastName);
System.out.println(book);
}
}
/*
Sample run:
Enter title of the book: Moby Dick
Enter Author first name: Herman
Enter Author last name: Melville
Enter Author email: hmellive@hotmail.com
Title: Moby Dick Author: Herman Melville hmellive@hotmail.com
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.