Question

In: Computer Science

In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...

In java

Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included:
• Constructor(s), Accessors and Mutators as needed.
• public double computeFine() => calculates the fine due on this item
The fine is calculated as follows:
• If the loanPeriod <= maximumLoanPeriod, there is no fine on the book.
• If loanPeriod > maximumLoanPeriod
o If the subject of the book is "CS" the fine is 10.00 per day.
o If the subject of the book is any other subject area, the fine is SR 5.00
per day.
o If the year of the book is < 2015, there is a 20% reduction in the fine. o If the year of the book is > 2015, the fine is increased by 20%
• toString() method that show description of the object.
Read the information of the attached file books.txt. The first line is the number of books and followed be a book on each line. The book details is separated by “-” Save the file content into array then Print the following.
• the number of books whose subject area is "CS".
• the total fine on all books
• the book with the highest fine.
• the total number of books with no fine.

Notes:
- Use the given books.txt.

——-——————————-

the text is

4

Introduction to Programming - CS - 2016 - 5 - 10

Introduction to Databases - CS - 2012 - 6 - 3

Thermodynamics - Physics - 1999 - 10 - 15

Linear Algebra - Math - 2015 - 12 - 10

Solutions

Expert Solution

// Book.java : Java program to create and implement the class Book

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Book {

       // data members

       private String name;

       private String subject;

       private int year;

       private int maximumLoanPeriod;

       private int loanPeriod;

      

       // default constructor

       public Book()

       {

             this("","",0,0,0);

       }

      

       // parameterized constructor

       public Book(String name, String subject, int year, int loanPeriod, int maximumLoanPeriod)

       {

             this.name = name;

             this.subject = subject;

             this.year = year;

             this.maximumLoanPeriod = maximumLoanPeriod;

             this.loanPeriod = loanPeriod;

       }

      

       // setters

       public void setName(String name)

       {

             this.name = name;

       }

      

       public void setSubject(String subject)

       {

             this.subject = subject;

       }

      

       public void setYear(int year)

       {

             this.year = year;

       }

      

       public void setMaximumLoanPeriod(int maximumLoanPeriod)

       {

             this.maximumLoanPeriod = maximumLoanPeriod;

       }

      

       public void setLoanPeriod(int loanPeriod)

       {

             this.loanPeriod = loanPeriod;

       }

      

       // getters

       public String getName()

       {

             return name;

       }

      

       public String getSubject()

       {

             return subject;

       }

      

       public int getYear()

       {

             return year;

       }

      

       public int getMaximumLoanPeriod()

       {

             return maximumLoanPeriod;

       }

      

       public int getLoanPeriod()

       {

             return loanPeriod;

       }

      

       // method to compute and return the fine

       public double computeFine()

       {

             double fine = 0;

             if(loanPeriod > maximumLoanPeriod)

             {

                    if(subject.equalsIgnoreCase("CS"))

                           fine = (loanPeriod-maximumLoanPeriod)*10;

                    else

                           fine = (loanPeriod-maximumLoanPeriod)*5;

                   

                    if(year < 2015)

                    {

                           fine -= 0.2*fine;

                    }else if(year > 2015)

                    {

                           fine += 0.2*fine;

                    }

             }

                   

             return fine;

       }

      

       // toString method

       public String toString()

       {

             return("Name : "+name+" Subject : "+subject+" Year : "+year+" Loan Period : "+loanPeriod+" Maximum LoanPeriod : "+maximumLoanPeriod);

       }

      

      

       public static void main(String[] args) throws FileNotFoundException {

      

             Scanner fileScan = new Scanner(new File("books.txt")); // open the file, provide full path to file

             int numBooks;

             Book books[];

             String line;

            

             numBooks = fileScan.nextInt(); // read the number of books

             fileScan.nextLine(); // ignore the '\n' at the end

             books = new Book[numBooks];

             int i=0;

             // loop to read till the end of file

             while(fileScan.hasNextLine())

             {

                    line = fileScan.nextLine(); // read the line

                    // The format of the record : name-subject-year-loanPeriod-maximumLoanPeriod

                    String fields[] = line.split("-");// split the read line using "-" as the delimiter

                    // store the book read in the array

                    books[i] = new Book(fields[0],fields[1],Integer.parseInt(fields[2]),Integer.parseInt(fields[3]),Integer.parseInt(fields[4]));

                    i++;

             }

            

             fileScan.close(); // close the file

            

             int csBooks = 0;

             double totalFine = 0;

             int highestFineBook = -1;

             double highestFine = 0;

             int noFineBooks = 0;

            

             // loop to calculate the statistics

             for(i=0;i<numBooks;i++)

             {

                    if(books[i].getSubject().equalsIgnoreCase("CS"))

                           csBooks++;

                    double fine = books[i].computeFine();

                    if(fine == 0)

                           noFineBooks++;

                    else

                    {

                           if(fine > highestFine)

                           {

                                 highestFine = fine;

                                 highestFineBook = i;

                           }

                    }

                   

                    totalFine += fine;

             }

            

             // output the statistics

             System.out.println("The number of books whose subject area is CS : "+csBooks);

             System.out.printf("Total fine on all books : SR %.2f",totalFine);

             if(highestFineBook != -1)

             {

                    System.out.printf("\nBook with the highest fine of SR : %.2f\n",highestFine);

                    System.out.println(books[highestFineBook]);

             }else

                    System.out.println("No books with fine");

            

             System.out.println("Total number of books with no fine : "+noFineBooks);

            

       }

}

//end of program

Output:

Input file:

Output:


Related Solutions

Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and average petrol consumption. Provide 2 constructors, the first takes name and type as parameter, the second takes four parameters for the four instance variables. (2 pt) Provide also a method called distancePerTank that calculate the average distance that a vehicle can travel if the tank is full (multiplies the tank size by the average petrol consumption), then returns the value. (2 pt) Provide a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT