PART I: Define a Java class named “ Book ” in a package named “ BOOK ”. This cla
ID: 3593292 • Letter: P
Question
PART I:
Define a Java class named “Book” in a package named “BOOK”. This class has 4 attributes:
ISBN number (an integer of 10 digits)
Title
Author (assumed that each book is authored by only one author )
Edition (e.g.: 1st Edition, 2nd Edition, etc. )
All of them should be defined as private. Class Book definition provides a default constructor, get-methods and set-methods of the attributes, and a method to display book data.
PART II
Write a Java program that can read data of a book from the console. The program should check to be sure that the ISBN input is a positive value and input values of title, author, and edition are not empty strings.
The Java program should be another Java class named “BookDisplayer” in the same package, i.e. BOOK. The program creates an object of class Book. After reading the inputs, the program sets the values of the attributes of the Book object and invokes the public method of class Book to display the book data.
Explanation / Answer
Book.java
public class Book {
//Declaring instance variables
private long ISBN;
private String title;
private String author;
private String edition;
//Zero argumented constructor
public Book() {
}
//Parameterized constructor
public Book(long iSBN, String title, String author, String edition) {
super();
ISBN = iSBN;
this.title = title;
this.author = author;
this.edition = edition;
}
//getters and setters
public long getISBN() {
return ISBN;
}
public void setISBN(long iSBN) {
ISBN = iSBN;
}
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;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Book [ISBN=" + ISBN + ", title=" + title + ", author=" + author + ", edition=" + edition + "]";
}
}
____________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
long ISBN;
String title, author, edition;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
/* This while loop continues to execute
* until the user enters a valid ISBN number
*/
while (true) {
//Getting the input entered by the user
System.out.print("Enter the ISBN :");
ISBN = sc.nextLong();
if (ISBN < 0) {
System.out.println("** Invalid .Must be positive **");
continue;
} else
break;
}
sc.nextLine();
/* This while loop continues to execute
* until the user enters a title
*/
while (true) {
//Getting the input entered by the user
System.out.print("Enter title :");
title = sc.nextLine();
if (title.isEmpty()) {
System.out.println("** Must no be empty **");
continue;
} else
break;
}
/* This while loop continues to execute
* until the user enters author name
*/
while (true) {
//Getting the input entered by the user
System.out.print("Enter author :");
author = sc.nextLine();
if (author.isEmpty()) {
System.out.println("** Must no be empty **");
continue;
} else
break;
}
/* This while loop continues to execute
* until the user enters edition
*/
while (true) {
//Getting the input entered by the user
System.out.print("Enter Edition :");
edition = sc.nextLine();
if (edition.isEmpty()) {
System.out.println("** Must no be empty **");
continue;
} else
break;
}
Book b = new Book();
b.setISBN(ISBN);
b.setTitle(title);
b.setAuthor(author);
b.setEdition(edition);
System.out.println(b.toString());
}
}
____________________
Output:
Enter the ISBN :-4567
** Invalid .Must be positive **
Enter the ISBN :1234567898
Enter title :
** Must no be empty **
Enter title :The C programming Language
Enter author :
** Must no be empty **
Enter author :Dennis Ritchie
Enter Edition :
** Must no be empty **
Enter Edition :1t Edition
Book [ISBN=1234567898, title=The C programming Language, author=Dennis Ritchie, edition=1t Edition]
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.