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...
SalaryCalculator in Java. The SalaryCalculator class should have instance variables of: an employee's name, reportID that...
SalaryCalculator in Java. The SalaryCalculator class should have instance variables of: an employee's name, reportID that is unique and increments by 10, and an hourly wage. There should also be some constructors and mutators, and accessor methods.
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
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 Temperature class that has two instance variables: a temperature value (a floating-point number)...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two...
JAVA Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours,...
JAVA Create an HourlyEmployee class that inherits from Employee and has two new instance variables: hours, which represents the hours worked, and wage, which represents the employee's pay per hour. (Both are doubles.) Create a constructor that takes the arguments first name, last name, social security number, hourly wage, and the number of hours worked. Also create accessors, mutators, an earnings method that returns the money earned by the employee this week, and a toString method that returns information about...
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT