Question: JAVA QUESTION You are to develop the class Book that has: 1. The following instance variables: private String title; private String author; private int pages;
JAVA 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 program using this code:
import java.util.Scanner;
public class BookTest
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Book b1 = new Book();
Book b2 = new Book();
String userInputTitle, userInputAuthor, userInputCategory;
int userInputPages;
double userInputCost;
boolean userInputRead;
System.out.println("Please enter the title of the first book:");
userInputTitle = keyboard.nextLine();
b1.setTitle(userInputTitle);
System.out.println("Please enter the author of the first book:");
userInputAuthor = keyboard.nextLine();
b1.setAuthor(userInputAuthor);
System.out.println("Please enter the category of the first book:");
userInputCategory = keyboard.nextLine();
b1.setCategory(userInputCategory);
System.out.println("Please enter the number of pages in the first
book:");
userInputPages = keyboard.nextInt();
b1.setPages(userInputPages);
System.out.println("Please enter the cost of the first book:");
userInputCost = keyboard.nextDouble();
b1.setCost(userInputCost);
System.out.println("Please enter if the first book has been read, true or
false:");
userInputRead = keyboard.nextBoolean();
b1.setRead(userInputRead);
System.out.println("The first book informaton is:");
System.out.println(b1.getTitle());
System.out.println(b1.getAuthor());
System.out.println(b1.getCategory());
System.out.println(b1.getPages());
System.out.println(b1.getCost());
System.out.println(b1.getRead());
keyboard.nextLine();//flush the line
System.out.println("Please enter the title of the second book:");
userInputTitle = keyboard.nextLine();
b2.setTitle(userInputTitle);
System.out.println("Please enter the author of the second book:");
userInputAuthor = keyboard.nextLine();
b2.setAuthor(userInputAuthor);
System.out.println("Please enter the category of the second book:");
userInputCategory = keyboard.nextLine();
b2.setCategory(userInputCategory);
System.out.println("Please enter the number of pages in the second
book:");
userInputPages = keyboard.nextInt();
b2.setPages(userInputPages);
System.out.println("Please enter the cost of the second book:");
userInputCost = keyboard.nextDouble();
b2.setCost(userInputCost);
System.out.println("Please enter if the second book has been read, true
or false:");
userInputRead = keyboard.nextBoolean();
b2.setRead(userInputRead);
System.out.println("The second book informaton is:");
System.out.println(b2.getTitle());
System.out.println(b2.getAuthor());
System.out.println(b2.getCategory());
System.out.println(b2.getPages());
System.out.println(b2.getCost());
System.out.println(b2.getRead());
}//end main method
}//end class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
