Programming Goal Write a class to represent a book, then write a driver program
ID: 3673749 • Letter: P
Question
Programming Goal
Write a class to represent a book, then write a driver program that demonstrates your class.
Book class
Your book class should contain the following fields and methods.
Fields
title (String)
author (String)
isbn (String)
subject (String)
fiction (boolean)
editionNumber (int)
Methods
3 constructors
One constructor that has a parameter for each field
One constructor that has a parameter for the ISBN (all other fields should be initialized to 0, null or false)
One copy constructor that has a Book parameter
Accessor (getter) and mutator (setter) for each field
A toString method that displays each field on a line, along with a heading for each field. For example, if title = "Introduction to Computing Systems", then this method would print Title: Introduction to Computing Systems for the title field
A copy method that takes a Book parameter
2 equal methods
One that has a Book parameter and returns true if each attribute of each object is the same
One that has a String parameter for the ISBN and returns true if the ISBN stored in the String parameter is the same as the Book object's ISBN
Driver Program
The driver program will demonstrate the use of the Book class.
Your program should begin by creating Book objects for the following 2 books (call one of the objects book1 and the other book2). Use constructor #1 to create one object and use constructor #2 to create the other object.
Title: Introduction to Computing Systems
Author: Yale Patt
ISBN: 9780087463
Subject: Computer Science
Fiction?: No
Edition: 2
Title: Crime and Punishment
Author: Fyodor Dostoevsky
ISBN: 9002322134
Subject: Literature
Fiction?: Yes
Edition: 1
Print each object's state.
For the object that was created with constructor #2, call the proper setter method on any field that is currently set to 0, null or false, and set it to its proper value. Print this object's new state.
Test to see if your objects are equal. Print the result of this test.
Create a new object, book3, to represent the following book. Use constructor #1.
Title: Essential Matlab for Engineers and Scientists
Author: Brian Hahn
ISBN: 9883740001
Subject: Engineering
Fiction?: No
Edition: 3
Create an object called book4 that is a copy of book3. Use the copy constructor to do this.
Test to see if book3 and book4 are equal. Print the result of this test.
Test to see if book2 is equal to the book with the ISBN: 9002322134. Print the result of this test.
For each work of fiction, print out the book's edition. For each work of non-fiction, print out the book's subject.
Specific Coding Requirements
Name your class file Book.java and name your driver BookDriver.java
Make all fields private and all methods public
Explanation / Answer
Hi, I have added Both Book and Driver Class.
I have created one Book object in Driver. You can add any number of test code in Driver.
Just add according to your need.
public class Book {
private String title, author;
private String isbn, subject;
private int editionNumber;
private boolean fiction;
public Book(String isbn) {
this.isbn = isbn;
subject = "";
title ="";
author = "";
editionNumber = 0;
fiction = false;
}
// copy constructor
public Book(Book book){
this.isbn = book.isbn;
this.title = book.title;
this.author = book.author;
this.subject = book.subject;
this.editionNumber = book.editionNumber;
this.fiction = book.fiction;
}
public Book(String title, String author, String isbn, String subject,
int editionNumber, boolean fiction) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.subject = subject;
this.editionNumber = editionNumber;
this.fiction = fiction;
}
@Override
public String toString() {
String fiction = this.fiction == true ? "Yes" : "No";
return "Title: " +title+" Author: "+author +" Subject: "+subject+
" ISBN: "+isbn+" Edition Number: "+editionNumber+" Fiction?: "+fiction;
}
// equal method
@Override
public boolean equals(Object obj) {
if(obj instanceof Book){
Book b = (Book)obj;
return this.title.equalsIgnoreCase(b.title) &&
this.editionNumber == b.editionNumber &&
this.author.equalsIgnoreCase(b.author) &&
this.subject.equalsIgnoreCase(b.subject) &&
this.isbn.equalsIgnoreCase(b.isbn) &&
this.fiction == b.fiction;
}
return false;
}
public boolean equals(String isbn){
return this.isbn.equalsIgnoreCase(isbn);
}
// copy method
public void copy(Book book){
this.isbn = book.isbn;
this.title = book.title;
this.author = book.author;
this.subject = book.subject;
this.editionNumber = book.editionNumber;
this.fiction = book.fiction;
}
}
class Driver{
public static void main(String[] args) {
Book b1 = new Book("Algorithm", "Coreman", "ISBN1234", "Computer", 2, true);
System.out.println(b1);
}
}
/*
Output:
Title: Algorithm
Author: Coreman
Subject: Computer
ISBN: ISBN1234
Edition Number: 2
Fiction?: Yes
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.