Write a Java program to calculate your cell phone bill given the number of minut
ID: 3870174 • Letter: W
Question
Write a Java program to calculate your cell phone bill given the number of minutes used. Customers pay a basic of $19.99 a month. The basic includes 200 peak minutes and 500 evening minutes. The charge for going over the peak is S0.30/minute and the evening minutes is S0.15/minute. Your program should input the peak minutes and evening minutes used from the command-line arguments. A sample run of the program is as follow: Command Prompt D: MyJava> java PhoneBil1 300 600 Monthly bill = $64.99 D: MyJava> java PhoneBil1 100 300 Monthly bill = $19.99Explanation / Answer
import java.io.*;
public class PhoneBill
{
public static void main(String args[])
{
//if arguments provided less than 2 exit printing error message
if(args.length < 2)
{
System.out.println("Please provide peak minutes and evening minutes");
System.exit(1);
}
//variables for Peak Minutes and Evening Minutes
int peakMinutes, eveningMinutes;
//basic bill
double bill = 19.99;
//try to parse peak and evening minutes from argumets
//if fails show an error
try {
// Parse the string arguments into an integer value.
peakMinutes = Integer.parseInt(args[0]);
eveningMinutes = Integer.parseInt(args[1]);
//if peakmiutes greater than 200 these should be included in bill
//else not
if (peakMinutes > 200) peakMinutes = peakMinutes - 200;
else peakMinutes = 0;
//if evening minutes are more than 500 should be counted in bill
//else not
if (eveningMinutes > 500) eveningMinutes = eveningMinutes - 500;
else eveningMinutes = 0;
//calculate the bill with extra minutes
bill = bill + peakMinutes * 0.30 + eveningMinutes * 0.15;
//Print the calculated bill
System.out.println("Monthly bill = $" + bill);
}
catch (NumberFormatException nfe) {
//if arguments are not integers print error message and exit
System.out.println("Both arguments must be integers.");
System.exit(1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.