In: Computer Science
JAVA
Lab 9: Phone Call
Write a program that will calculate the cost of a phone call as follows:
Include all Prologue information with your program
1. 9 minutes
2. 10 minutes
3. 11 minutes
4. 35 minutes
5. 0 minutes
NOTE 1:
NOTE 2:
1. Declare all variables within the data
declaration section of each class and
method. (-.1)
2 Do not get input on the same line as a variable
declaration. (-.1)
3. Do not place an equation for computation on the same line as
declaring a variable. (-.1)
4. Do not place an equation for computation on the
same line as an input statement. (-.1)
5. Do not place any
computations within an output statement.
(-.1)
PFB PhoneCall java class source code.
Solution summary :
Program ask the user to enter call minutes and print the cost
($0.99/min for first 10 minutes , after that $0.10/min)
Displays error if , minutes entered is equal to or less than 0.
Finally it displays the call minutes and call cost.
Note : Program is tested for all asked test cases.
--------------
PhoneCall.java
--------------
import java.util.Scanner;
public class PhoneCall{
//driver method
public static void main(String[] args){
//Input the number of minutes talked as an integer.
int callTimeInMinutes;
//To store total call cost
double totalCallCost=0.0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the call time in minutes : ");
callTimeInMinutes = s.nextInt();
//Checking if minutes entered is not less than or equal to 0.
//output an error message stating that no minutes were
entered.
if(callTimeInMinutes<=0){
System.out.println("\nNo minutes were entered!");
}else{
//The first 10 minutes are charged at a flat rate of $0.99 or 99
cents
//Every minute after the initial 10 minutes will be charged at
$0.10 per minute.
if(callTimeInMinutes>10){
totalCallCost=totalCallCost+(10*0.99) +
((callTimeInMinutes-10)*(0.10));
}else{
//calculate if minutes are less than or equal to 10
totalCallCost=totalCallCost+(callTimeInMinutes*0.99);
}
}
//Displaying call minutes ans call cost
System.out.println("\nCall minutes : " + callTimeInMinutes);
//The price will be formatted with 2 decimal places.
System.out.println("Call cost : " + String.format("%.2f",
totalCallCost));
s.close();
}
}
--------------
Output of asked inputs :
------------------------
------------------------
Let me know, if any further help is required.