200k views
3 votes
Create an application for a library and name it FineForOverdueBooks. TheMain() method asks the user to input the number of books checked out and the number of days they are overdue. Pass those values to a method named DisplayFine that displays the library fine, which is 10 cents per book per day for the first seven days a book is overdue, then 20 cents per book per day for each additional day.

The library fine should be displayed in the following format:

The fine for 2 book(s) for 3 day(s) is $0.60

(The numbers will vary based on the input.)

1 Answer

2 votes

Answer:

//The Scanner class is imported which allow the program to receive user input

import java.util.Scanner;

//Class Solution is defined to hold problem solution

public class Solution {

// The main method which signify the begining of program execution

public static void main(String args[]) {

// Scanner object 'scan' is defined to receive input from user keyboard

Scanner scan = new Scanner(System.in);

// A prompt is display asking the user to enter number of books

System.out.println("Please enter number of books: ");

// the user response is assigned to numberOfBook

int numberOfBook = scan.nextInt();

// A prompt is displayed asking the user to enter the number of days over due

System.out.println("Please enter number of days over due: ");

// the user response is assigned to numberOfDaysOverDue

int numberOfDaysOverDue = scan.nextInt();

//displayFine method is called with numberOfBook and numberOfDaysOverDue as arguments

displayFine(numberOfBook, numberOfDaysOverDue);

}

//displayFine method is declared having two parameters

public static void displayFine(int bookNumber, int daysOverDue){

// fine for first seven days is 10cent which is converted to $0.10

double firstSevenDay = 0.10;

// fine for more than seven days is 20cent which is converted to $0.20

double moreThanSevenDay = 0.20;

// the fine to be paid is declared

double fine = 0;

// fine is calculated in the following block

if(daysOverDue <= 7){

//the fine if the over due days is less than or equal 7

fine = bookNumber * daysOverDue * firstSevenDay;

} else{

// the extra days on top of the first seven days is calculated and assigned to extraDays

int extraDays = daysOverDue - 7;

//fine for first seven days is calculated

double fineFirstSevenDays = bookNumber * 7 * firstSevenDay;

// fine for the extradays is calculated

double fineMoreThanSevenDays = bookNumber * extraDays * moreThanSevenDay;

// the total fine is calculated by adding fine for first seven days and the extra days

fine = fineFirstSevenDays + fineMoreThanSevenDays;

}

// The total fine is displayed to the user in a nice format.

System.out.printf("The fine for " + bookNumber + " book(s) for " + daysOverDue + " day(s) is: $%.02f", fine);

}

}

Step-by-step explanation:

The program first import Scanner class to allow the program receive user input. Then the class Solution was defined and the main method was declared. In the main method, user is asked for number of books and days over due which are assigned to numberOfBook and numberOfDaysOverDue. The two variable are passed as arguments to the displayFine method.

Next, the displayFine method was defined and the fine for the first seven days is calculated first if the due days is less than or equal seven. Else, the fine is calculated for the first seven days and then the extra days.

The fine is finally displayed to the user.

User Vitaly Kravtsov
by
4.8k points