111k views
2 votes
Write a program that calculates the cost of a phone call. The user enters a positive integer that

indicates the length of the call. The first two minutes of a phone call cost a flat $1.50. Minutes 3 to 10
cost 50 cents each. Each minute after 10 costs 25 cents each.
For example:
How many minutes is the call?
13
A 13 minute call costs 6.25

MY CODE SO FAR:
import java.util.Scanner;
public class Third {
public static void main (String[]args) {
Scanner num = new Scanner (System.in);
System.out.println("Enter number of minutes");
int x = num.nextInt();


I have gotten this far but im not sure what the rest is and how I would create the code for adding minutes. I know it has to do with final but not sure where to start. Thanks.

User Pavan
by
8.0k points

1 Answer

2 votes
add the following constants in the class definition, before the main method:

private final static float START_COST = 1.5f;
private final static float HIGH_TARIFF = 0.5f;
private final static float LOW_TARIFF = 0.25f;
private final static int FIXED_MINS = 2;
private final static int HIGH_TARIFF_MINS = 10;

Then add this to the main method you already had:

float price = START_COST;
if (x > FIXED_MINS) {
if (x <= HIGH_TARIFF_MINS) {
price += HIGH_TARIFF*(x-FIXED_MINS);
}
else {
price += HIGH_TARIFF*(HIGH_TARIFF_MINS-FIXED_MINS)
+ LOW_TARIFF*(x-HIGH_TARIFF_MINS);
}
}
System.out.printf("A %d minute call costs %.2f", x, price);

User Tyler Chong
by
7.3k points

No related questions found