Question
I need some help ASAP. I have all the code written but my calculations are off. In my file TestBook I have a method named
I need some help ASAP. I have all the code written but my calculations are off. In my file TestBook I have a method named reduceBooks where I have to take the books that are in an the array and take 40% off of them. With what I have right now the calculations that are being printed are way off and I can't figure out how to fix them. Here are the instructions on what I am supposed to do. Also this is in Java
Two files are required, a data class named Book and an executable class named TestBook.
Class Book
has instance data members (all private) String title, String author, int pages, double price.
has a public static int variable named numBooks with an initial value of zero.
has a parameterized constructor that will be used to make a Book object and assign values to its data members, and increment numBooks.
has a no-arg constructor that increments numBooks.
has getters and setters for all instance data members.
has a toString() method that returns a string displaying the state of a Book instance.
Use the numBooks variable to report the number of books instantiated.
Class TestBook This class needs a main method and two more methods. In main:
create an array capable of holding six Book objects.
use the parameterized constructor to specify the data in the first four elements of this array
use the no-arg constructor to create the two remaining books in the array.
process the array with a foreach loop to display the array at this point.
call the finishArray() method with the array as the only argument.
call the reduceBooks() method with the array as the sole argument.
repeat the code needed by Step 4 above.
display the most expensive book after the discounts.
In finishArray():
this is a void method.
use the setter methods to specify the data in all fields of the last two books in the array.
In reduceBooks():
this method returns a Book instance.
use a loop (any type) to reduce the price of every book in the array by 40%.
determine the most expensive book after the discounts and return this book to main.
What the code is suppose to look like
This is what my code is outputting, and as you can see its not even close to what its supposed to be
Book title:Java Programming, author:Liang, pages:1320, price: $145.00 Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $19.99 Book title:The Hobbit, author:Tolkien, pages:320, price: $9.25 Book title:Born a Crime, author:Noah, pages:304, price: $17.33 Book title:null, author:null, pages:0, price: $0.00 Book title:null, author:null, pages:0, price: $0.00
Books after completing library and 40% discount Book title:Java Programming, author:Liang, pages:1320, price: $58.00 Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $8.00 Book title:The Hobbit, author:Tolkien, pages:320, price: $3.70 Book title:Born a Crime, author:Noah, pages:304, price: $6.93 Book title:Dark Territory, author:Kaplan, pages:352, price: $4.50 Book title:Born to Run, author:Springsteen, pages:508, price: $4.87
Here is the most expensive book after discounts Book title:Java Programming, author:Liang, pages:1320, price: $58.00 Size of library 6
Here is my code
//Book.Java
public class Book {
//instance data members
private String title;
private String author;
private int pages;
private double price;
//public static int set to 0
public static int numBooks = 0;
//parameterized constructor
Book(String title, String author, int pages, double price){
this.title=title;
this.author=author;
this.pages=pages;
this.price=price;
numBooks++;
}
//default constructor
Book(){
numBooks++;
}
//getters and setters
void setTitle(String title) {
this.title=title;
}
void setAuthor(String author) {
this.author=author;
}
void setPages(int pages) {
this.pages=pages;
}
void setPrice(double price) {
this.price=price;
}
String getTitle() {
return title;
}
String getAuthor() {
return author;
}
int getPages() {
return pages;
}
double getPrice() {
return price;
}
//toString method to return string showing state of a book instance
@Override
public String toString() {
String str = "Book title:" + title + ", author:" + author +
", pages:" + pages + ", price: " + String.format("$%.2f", price);
return str;
}
}
//TestBook.Java
public class TestBook {
public static void main(String[] args) {
// array to hold 6 book objects specify data in first 4
Book[] bookArray=new Book[6];
bookArray[0]=new Book("Java Programming","Liang",1320,145.00);
bookArray[1]=new Book("Horton Hears a Who!","Dr.Seuss",72,19.99);
bookArray[2]=new Book("The Hobbit","Tolkien",320,9.25);
bookArray[3]=new Book("Born a Crime","Noah",304,17.33);
bookArray[4]=new Book();
bookArray[5]=new Book();
for(Book b:bookArray)
System.out.println(b.toString());
System.out.println(" Books after completing library and 40% discount");
finishArray(bookArray);
Book expensive = reduceBooks(bookArray);
for(Book b:bookArray)
System.out.println(b.toString());
System.out.println(" Here is the most expensive book after discounts " + expensive.toString());
System.out.println("Size of library " +Book.numBooks);
}
//void method, use setter method to fill in last 2 books in array
static void finishArray(Book[]array) {
array[4].setTitle("Dark Territory");
array[4].setAuthor("Kaplan");
array[4].setPages(352);
array[4].setPrice(11.24);
array[5].setTitle("Born to Run");
array[5].setAuthor("Springsteen");
array[5].setPages(508);
array[5].setPrice(12.17);
}
public static Book reduceBooks(Book[]array) {
Book mostExpensiveBook = null;
for (int i=0; i
array[i].setPrice(array[i].getPrice()* 0.40);
if (mostExpensiveBook == null)
mostExpensiveBook = array[i];
else if (mostExpensiveBook.getPrice()
mostExpensiveBook = array[i];
}
return mostExpensiveBook;
}
}
public static Book reduceBooks(Book[]array) is where my calculations are and where I am running into a problem. Please help!!!!
Book title-Java Programming, author=Liang, pages-1320, price = $145.00 Book title-Horton Hears a Who', author-Or . Seuss, pages=72, price = $19.99 Book title-The Hobbit, author-Tolkien, pages 320, price -$9.25 Book title-Born a Crime, author=Noah, pages-304, price = $17.33 Book title-null, author-null, pages-0, price $0.00 Book title-null, author-null, pages-0, price -$0.00 Books after completing library and 40% discount Book title-Java Programming, author=Liang, pages=1320, price $87.00 Book title-Horton Hears a Who', author-Or . Seuss, pages=72, price = $11.99 Book title-The Hobbit, author-Tolkien, pages 320, price -$5.55 Book title-Born a Crime, authorNoah, pages 304, price -$10.40 Book title-Dark Territory, author-Kaplan, pages-352, price-$11.24 Book title-Born to Run, author-Springsteen, pages:508, price $12.17 Here is the most expensive book after the discounts Book title-Java Programming, author Liang, pages-1320, price-$87.00 Size of library: 6 booksStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started