Back to the Program-A-Day homepage
Problem
Write a program that will use Scanner to read in the price of a sandwich and a drink. Output the amounts. Calculate and output tax (assume 5% on everything). Then output a receipt as shown.
Sample output with sandwich 5.99 and drink 1.99:
Cost of Sandwich:
[DrJava Input Box]
Cost of Drink:
[DrJava Input Box]
Food cost: 5.99
Drink cost: 1.99
Taxes: 0.399
Total: 8.379000000000001
Programmed by A. Student
**End of Program**
Extra: Once you get this program working, try rounding the taxes to two decimal places.
|
|
Using Scanner for input
|
Solution
|
Code
Solution Code
import java.util.Scanner;
/*****************************************************************************************
Title: Food and Drink
*****************************************************************************************/
public class FoodAndDrink
{ //opens public class
public static void main (String []args)
{ //opens main method
//variables declared here
double foodCost; //food amount
double drinkCost; //drink amount
double tax; //taxes (at 5%) * This really should be a final--next week!
//
Scanner keyboard = new Scanner (System.in);
//get input
System.out.println("Cost of Sandwich:");
foodCost = keyboard.nextDouble();
System.out.println("Cost of Drink:");
drinkCost = keyboard.nextDouble();
//calculate taxes
tax = .05 * (foodCost + drinkCost);
//output
System.out.println("Food cost: " + foodCost + "\nDrink cost: " + drinkCost + "\nTaxes: " + tax
+ "\nTotal: " + (foodCost + drinkCost + tax));
System.out.println("Programmed by A. Student");
System.out.println("**End of Program**");
}//close main
}//close public class
|
Back to the Program-A-Day homepage