This homework will give you practice using user input with the Scanner class and
ID: 3754301 • Letter: T
Question
This homework will give you practice using user input with the Scanner class and the 'if stmt. You are writing a program for a cell phone provider that offers three different plans: Option 1: Unlimited Calling and Texting. Price $64.99 per month. Option 2: Unlimited Calling and Pay per text. Base Price: $59.00 per month plus 5 cents per text. Option 3: $35.00 base price for up to 50 minutes of calling, and then 3 cents per minute This option does not allow texting. Write a program that calculates a monthly bill. The program should ask the user to enter: 1. the plan chosen 2. the number of minutes used 3. and if appropriate, the number of text messages. For Option 3, if the user exceeds the number of free minutes (50) a message should display telling the user how many minutes will be charged (hint use nested-if) The program should neatly print the price for the month along with the method used to calculate the price. Run your program with at least 5 different inputs. Be sure to test each possible case (If you use the switch statement, then give in 2 versions of your program, one with the if else-if and one with the switch) Please use javaExplanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you want to change anything (make a loop for multiple entries). Thanks
// MonthlyBillCalculator.java
import java.util.Scanner;
public class MonthlyBillCalculator {
public static void main(String[] args) {
//defining Scanner object
Scanner scanner = new Scanner(System.in);
//displaying plans
System.out.println("1. Unlimited calling and texting ($64.99)");
System.out.println("2. Unlimited calling and pay per text ($59.00 + 5 cent per text)");
System.out.println("3. 50 minutes calling,"
+ " 3 cents per extra minute, no text (base price $35.00)");
System.out.println("Enter your plan: ");
//getting selected plan
int plan = scanner.nextInt();
double bill = 0; //to store the final bill
//checking plan
if (plan == 1) {
//unlimited plan
bill = 64.99;
System.out.printf("Your monthly bill is $%.2f ", bill);
} else if (plan == 2) {
//getting number of text messages sent
System.out.print("Enter number of text messages sent: ");
int textCount = scanner.nextInt();
//adding 5 cents for each messages sent to the base price 59.00
bill = 59.00 + (0.05 * textCount);
System.out.printf("Your monthly bill is $%.2f ", bill);
} else if (plan == 3) {
//getting number of minutes used
System.out.print("Enter number of minutes used: ");
int minutes = scanner.nextInt();
//checking if minute exceeds 50
if (minutes <= 50) {
//base price only
bill = 35.00;
} else {
//base price plus 3 cents extra for each minute
bill = 35.00 + (minutes - 50) * 0.03;
}
System.out.printf("Your monthly bill is $%.2f ", bill);
}else{
System.out.println("Invalid plan!");
}
}
}
/*OUTPUT (multiple runs)*/
1. Unlimited calling and texting ($64.99)
2. Unlimited calling and pay per text ($59.00 + 5 cent per text)
3. 50 minutes calling, 3 cents per extra minute, no text (base price $35.00)
Enter your plan:
1
Your monthly bill is $64.99
1. Unlimited calling and texting ($64.99)
2. Unlimited calling and pay per text ($59.00 + 5 cent per text)
3. 50 minutes calling, 3 cents per extra minute, no text (base price $35.00)
Enter your plan:
2
Enter number of text messages sent: 38
Your monthly bill is $60.90
1. Unlimited calling and texting ($64.99)
2. Unlimited calling and pay per text ($59.00 + 5 cent per text)
3. 50 minutes calling, 3 cents per extra minute, no text (base price $35.00)
Enter your plan:
3
Enter number of minutes used: 45
Your monthly bill is $35.00
1. Unlimited calling and texting ($64.99)
2. Unlimited calling and pay per text ($59.00 + 5 cent per text)
3. 50 minutes calling, 3 cents per extra minute, no text (base price $35.00)
Enter your plan:
3
Enter number of minutes used: 70
Your monthly bill is $35.60
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.