You are to develop the class Book that has: 1. The following instance variables:
ID: 3710754 • Letter: Y
Question
You are to develop the class Book that has:
1. The following instance variables:
private String title;
private String author;
private int pages;
private String category;
private boolean read;
private double cost;
2. The following getter (accessor) methods (Use the following headings - you are to complete the body of the method as described in my comment)
//Returns the title of the instance of the class Book that called the method
public String getTitle()
//Returns the author of the instance of the class Book that called the method
public String getAuthor()
//Returns the number of pages of the instance of the class Book that
//called the method
public int getPages()
//Returns the category of the instance of the class Book that called the
//method
public String getCategory()
//Returns the value of read of the instance of the class Book that called
//the method
public boolean getRead()
//Returns the cost of the instance of the class Book that called the method
public double getCost()
3. The following setter (mutator) methods (Use the following headings - you are to complete the body of the method as described in my comment)
//Sets the title of the instance of the class Book that called the method
//to the value passed to the method
public void setTitle(String titlePassed)
//Sets the author of the instance of the class Book that called the method
//to the value passed to the method
public void setAuthor(String authorPassed)
//Sets the number of pages of the instance of the class Book that called the
//method to the value passed to the method. If the value passed to the
//method is 0 or less ask the user to reenter the amount, trapping
//the user (through a while loop) until the user enters a value > 0.
// HINT: you will have to declare an instance of the class Scanner within
//the method
public void setPages(int pagesPassed)
//Sets the cost of the instance of the class Book that called the
//method to the value passed to the method. If the value passed to the
//method is < 0 asks the user to reenter the amount, trapping
//the user (through a while loop) until the user enters a value >= 0.
// HINT: you will have to declare an instance of the class Scanner within
//the method
public void setCost(double costPassed)
//Sets the variable read of the instance of the class Book that called the
// method to the value passed to the method
public void setRead(boolean readPassed)
//Sets the variable category of the instance of the class Book that called
//the method to the value passed to the method. If the value passed is not
//“Biography”, "Children's","Cooking", "Fantasy", "Fiction", or "Technical"
//set the value of variable category to “Invalid”
public void setCategory(String categoryPassed)
Test your program using the code I have developed BookTest.java
Explanation / Answer
Book class:
import java.util.Scanner;
//BookTest class which has main method to test the Book
public class BookTest
{
public static void main(String[] args)
{
//creating an instance of Book to test
Book book = new Book();
/*asking user to input the values for book
/Scanner class to read inputs */
Scanner in = new Scanner(System.in);
System.out.print("Enter the book title:");
String bookTitle=in.nextLine();
//calling setTitle() method of Book class
book.setTitle(bookTitle);
System.out.print("Enter the author of the book:");
String author=in.nextLine();
book.setAuthor(author);
System.out.print("Enter the number of pages of the book:");
int pages=in.nextInt();
book.setPages(pages);
System.out.print("Enter the category of the book:");
String category=in.next();
book.setCategory(category);
//skips the next line
//in.nextLine();
System.out.print("is book read?(true or false):");
boolean read=in.nextBoolean();
book.setRead(read);
System.out.print("Enter the cost of the book:");
double cost=in.nextDouble();
book.setCost(cost);
//displaying the book details
System.out.println("Book Details:");
System.out.println("Book Title:"+book.getTitle());
System.out.println("Book Author:"+book.getAuthor());
System.out.println("Number of Pages:"+book.getPages());
System.out.println("Book read?:"+book.isRead());
System.out.println("Book Cost:"+book.getCost());
}
}
There is not BookTest.java file in the question. I have developed the test class and tested Book class
BookTest class:
import java.util.Scanner;
//BookTest class which has main method to test the Book
public class BookTest
{
public static void main(String[] args)
{
//creating an instance of Book to test
Book book = new Book();
/*asking user to input the values for book
/Scanner class to read inputs */
Scanner in = new Scanner(System.in);
System.out.print("Enter the book title:");
String bookTitle=in.nextLine();
//calling setTitle() method of Book class
book.setTitle(bookTitle);
System.out.print("Enter the author of the book:");
String author=in.nextLine();
book.setAuthor(author);
System.out.print("Enter the number of pages of the book:");
int pages=in.nextInt();
book.setPages(pages);
System.out.print("Enter the category of the book:");
String category=in.next();
book.setCategory(category);
//skips the next line
//in.nextLine();
System.out.print("is book read?(true or false):");
boolean read=in.nextBoolean();
book.setRead(read);
System.out.print("Enter the cost of the book:");
double cost=in.nextDouble();
book.setCost(cost);
//displaying the book details
System.out.println("Book Details:");
System.out.println("Book Title:"+book.getTitle());
System.out.println("Book Author:"+book.getAuthor());
System.out.println("Number of Pages:"+book.getPages());
System.out.println("Book read?:"+book.isRead());
System.out.println("Book Cost:"+book.getCost());
}
}
Sample output:
Enter the book title:Let us C
Enter the author of the book:Yeshwanth Kanethkar
Enter the number of pages of the book:-92
Re-enter the number of pages:1400
Enter the category of the book:Technical
is book read?(true or false):true
Enter the cost of the book:-193
Re-enter the cost:1500
Book Details:
Book Title:Let us C
Book Author:Yeshwanth Kanethkar
Number of Pages:1400
Book read?:true
Book Cost:1500.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.