I need help with this please in Java Two files are required

I need help with this please... 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. Sample Output: Book title=Java Programming, author=Liang, pages=1320, price= $145.00

Solution

Book.java:

class Book {
    private String title;
    private String author;
    private int pages;
    private double price;
    public static int numBooks = 0;
  
    Book() {
        title = author = null;
        price = pages = 0;
        numBooks++;
    }
  
    Book(String title, String author, int pages, double price){
        this.title = title;
        this.author = author;
        this.pages = pages;
        this.price = price;
        numBooks++;
    }
  
    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;
    }
  
    @Override
    public String toString(){
        String str = \"Book title: \" + title
                + \"Book author: \" + author
                + \"Book pages: \" + pages
                + \"Book price: \" + price;
        return str;
    }
}

TestBook.java:

public class TestBook{

    public static void main(String[] args){
        Book[] bookArray = new Book[6];
        bookArray[0] = new Book(\"Harry Poter\", \"JKR\", 1054, 5.99);
        bookArray[1] = new Book(\"Gitanjali\",\"RT\", 2110, 9.45);
        bookArray[2] = new Book(\"Guide to Java\", \"SB\", 562, 3.75);
        bookArray[3] = new Book(\"B747 flying guide\", \"Boeing\", 6149, 20);
        bookArray[4] = new Book();
        bookArray[5] = new Book();
        for(Book b:bookArray)
            System.out.println(b.toString());
        finishArray(bookArray);
        Book expensive = reduceBooks(bookArray);
        for(Book b:bookArray)
            System.out.println(b.toString());
        System.out.println(\"Most expensive book: \" + expensive.toString());
    }
  
    static void finishArray(Book[] array){
        array[4].setTitle(\"Shortcut to life\");
        array[4].setAuthor(\"Papa Lima\");
        array[4].setPages(10);
        array[4].setPrice(51.51);
      
        array[5].setTitle(\"Algorithm\");
        array[5].setAuthor(\"Cormen\");
        array[5].setPages(1510);
        array[5].setPrice(4.44);
    }
  
    static Book reduceBooks(Book[] array){
        Book mostExpensiveBook = null;
        for (int i=0; i<6; i++){
            array[i].setPrice(array[i].getPrice()* 0.60);
            if (mostExpensiveBook == null)
                mostExpensiveBook = array[i];
            else if (mostExpensiveBook.getPrice() < array[i].getPrice())
                mostExpensiveBook = array[i];
        }
        return mostExpensiveBook;
    }
}


This program will solve your problem! Cheers!

I need help with this please... in Java - Two files are required, a data class named Book and an executable class named TestBook. Class Book has instance data m
I need help with this please... in Java - Two files are required, a data class named Book and an executable class named TestBook. Class Book has instance data m
I need help with this please... in Java - Two files are required, a data class named Book and an executable class named TestBook. Class Book has instance data m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site