Can anyone help fit this java program. Trying to get it to run without the (goto
ID: 3547504 • Letter: C
Question
Can anyone help fit this java program. Trying to get it to run without the (goto: readBooks)portion in the class BookDemo . Need help replacing the (goto: readBooks)
This program will create and populate an array of instances of the class Book using user input. The user will enter a number n, your program will declare and create an array of size n of Books. The user will be asked to enter the information for each instance that is to be created. The following exceptions will be thrown if the user enters information that is not appropriate to the request:
Explanation / Answer
Many errors are there and so Modifications are done. Goto statement is removed.
DO RATE if satisfied
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
public class BookDemo {
public static void main(String[] args) {
int n = 0, i = 0;
//int n;
Book temp = null;
Book[] bookArray = null;
System.out.println("Enter the number of books");
Scanner reader = new Scanner(System.in);
n=reader.nextInt();
bookArray = new Book[n];
for (; i < n; i++) {
try {
temp = new Book();
System.out.println("Enter Title of the Book");
temp.setTitle(reader.next());
System.out.println("Enter name of the Author of Book");
temp.setAuthor(reader.next());
System.out.println("Enter the number of pages in the Book");
temp.setPages(reader.nextInt());
System.out.println("Enter the Category of the Book (category should be any of Fiction, Non-Fiction, Biography, Technical, Children or Other)");
temp.setCategory(reader.next());
System.out.println("Enter Weather the Book is read");
temp.setRead(reader.nextBoolean());
System.out.println("Enter the cost of Book");
temp.setCost(reader.nextDouble());
bookArray[i] = temp;
temp = null;
} catch (PageException ex) {
System.out.println(ex.getMessage());
i--;
} catch (CostException ex) {
System.out.println(ex.getMessage());
i--;
} catch (CategoryException ex) {
System.out.println(ex.getMessage());
i--;
}
}
System.out.println("The details of books are");
for (int j = 0; j < n; j++) {
System.out.println(bookArray[j].toString());
}
}
}
public class Book {
private String title;
private String author;
private int pages;
private String category;
private boolean read;
private double cost;
//default constructor
public Book() {
title = "Not yet assigned";
author = "Not yet assigned";
pages = 0;
category = "Not yet assigned";
read = false;
cost = 0.0;
}
//non default constructor
public Book(String titlePassed, String authorPassed, int pagesPassed, String categoryPassed,
boolean readPassed, double costPassed) {
title = titlePassed;
author = authorPassed;
pages = pagesPassed;
category = categoryPassed;
read = readPassed;
cost = costPassed;
}//end non default constructor
//getters
public String getTitle() {
return title;
}//end getTitle
public String getAuthor() {
return author;
}//end getAuthor
public int getPages() {
return pages;
}//end get Pages
public String getCategory() {
return category;
}//end getCategory
public boolean getRead() {
return read;
}//end getRead
public double getCost() {
return cost;
}//end getCost
//setters
public void setTitle(String titlePassed) {
title = titlePassed;
}//end setTitle
public void setAuthor(String authorPassed) {
author = authorPassed;
}//end setAuthor
public void setPages(int pagesPassed) throws PageException {
if (pagesPassed < 0) {
throw new PageException("Pages should be >0 . Enter Details of book again");
} else {
pages = pagesPassed;
}
}//end setPages
public void setCategory(String categoryPassed) throws CategoryException {
//Fiction, Non-Fiction, Biography, Technical, Children or Other
int i;
String [] cats = { "Fiction", "Non-Fiction", "Biography",
"Technical", "Children", "Other" };
for(i=0; i<cats.length; i++)
if( cats[i].equalsIgnoreCase(categoryPassed))
break;
if( i==cats.length )
throw new CategoryException("Invalid Category. Enter Details of book again");
category = categoryPassed;
}//end setCategory
public void setRead(boolean readPassed) {
read = readPassed;
}//end setRead
public void setCost(double costPassed) throws CostException {
if (costPassed < 0) {
throw new CostException("Cost should be >0 . Enter Details of book again");
} else {
cost = costPassed;
}
}//end setCost
//toString method
public String toString() {
return " Title: " + title
+ " Author: " + author
+ " Pages: " + pages
+ " Category: " + category
+ " Read: " + read
+ " Cost: " + cost;
}//end toString
//equals method
public boolean equals(Book bookPassed) {
return (this.title.equals(bookPassed.getTitle())
&& this.author.equals(bookPassed.getAuthor())
&& this.pages == bookPassed.getPages()
&& this.category.equals(bookPassed.getCategory())
&& this.read == bookPassed.getRead()
&& this.cost == bookPassed.getCost());
}//end method equals
}//end class
import java.util.Scanner;
public class CategoryException extends Exception {
public CategoryException() {
super("Length Exception of unknown specificity.");
getAnotherCategory();
}
public CategoryException(String messagePassed) {
super("Exception Caught:" + messagePassed);
}
public String getAnotherCategory() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Category must be a Fiction, Non-Fiction, Biography, Technical, Children or Other");
return keyboard.nextLine();
}
}//end class CategoryException
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.