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...
USE JAVA PLEASE A Phone class has 4 instance variables Model name ("iPhone12","Galaxy") RAM (number of...
USE JAVA PLEASE A Phone class has 4 instance variables Model name ("iPhone12","Galaxy") RAM (number of gigs such as 8 and12) Storage (number of gigs such as 128 and 512) Screen of type class Screen as described below.    A Screen has 3 instance variables: Screen type ("AMOLED", "LCD") Screen size (decimal number such as 6.2 and 10.1) Screen ppi (448, 630, 300)    Q1) Code two class-shell for both classes. The names and types of the instance variables have...
Develop the following class: Class Name: ImprovedArrayBasedStack Access Modifier: public Implements: ImprovedStackInterface Instance variables Name: top...
Develop the following class: Class Name: ImprovedArrayBasedStack Access Modifier: public Implements: ImprovedStackInterface Instance variables Name: top Access modifier: private Data type: int Name: stack Access modifier: private Data type: T[] (an array of parameterized type) Constructors Name: ImprovedArrayBasedStack Access modifier: public Parameters: none (default constructor) Task: sets the value of top to -1 sets the stack to refer to an array of Objects with 100 elements which are type cast to T[] Name: ImprovedArrayBasedStack Access modifier: public Parameters: size (data...
in java Design and implement a class called Dog that contains instance data that represents the...
in java Design and implement a class called Dog that contains instance data that represents the dog’s name and age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in “person years” (seven times the dog’s age). Include a toString method that returns a one-line description of the dog. Create a Tester class called Kennel, whose main...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and address (String). Implement the initialization constructor . Implement the setters and getters for all attributes. Implement the toString() method to display all attributes. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. Implement the compareTo (Entry other) method that returns 0 if the two...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT