I'm a junior in high school. Having a tough time figuring out how to use money format. I'm doing an exercise in A Guide to Programming in Java (Second Edition) where I have to prompt the employees for the number of burgers, fries, and sodas.
Fries are $1.09, burgers are $1.69, and sodas are $0.99.
Here is my code:
import java.util.Scanner;
/**
* Order pg. 101
*
* Garret Mantz
* 2/10/2016
*/
public class Order {
public static void main(String[]args) {
final double pburgers=1.69;
final double pfries=1.09;
final double psodas=0.99;
final double ptax=0.065;
double burgers;
double fries;
double sodas;
double totaltax;
double total;
double tax;
double tendered;
double change;
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount of burgers: ");
burgers = input.nextDouble();
System.out.print("Enter the amount of fries: ");
fries = input.nextDouble();
System.out.print("Enter the amount of sodas: ");
sodas = input.nextDouble();
System.out.print("Enter the amount tendered: ");
tendered = input.nextDouble();
totaltax = (burgers*pburgers)+(fries*pfries)+(sodas*psodas);
tax = totaltax*ptax;
total = totaltax + tax;
change = tendered - total;
System.out.println("Your total before tax is: \n" + totaltax);
System.out.println("Tax: \n" + tax);
System.out.println("Your final total is: \n" + total);
System.out.println("Your change is: \n" + change);
}
}
I just want to use the money format, but I'm not sure how. I'm sure it's a dumb question, but thank you for helping out!
Change your println to these, and see if that helps:
System.out.format("Your total before tax is: $%-5.2f\n", totaltax);
System.out.format("Tax: $%-5.2f\n", tax);
System.out.format("Your final total is: $%-5.2f\n", total);
System.out.format("Your change is: $%-5.2f\n", change);
There is also this:
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String totalTaxString = formatter.format(totaltax);
String taxString = formatter.format(tax);
String totalString = formatter.format(total);
String changeString = formatter.format(change);
System.out.format("Your total before tax is: %s\n", totalTaxString);
System.out.format("Tax: %s\n", taxString);
System.out.format("Your final total is: %s\n", totalString);
System.out.format("Your change is: %s\n", changeString);
Output:
Your total before tax is: $8.53
Tax: $0.55
Your final total is: $9.08
Your change is: $10.92
Related
The meal before tax and tip is 12.00, the tax percentage of the meal is 20% and the tip of the meal is 8%.
You need the use Scanner class to receive input from the user.
12.00
20
8
The expected output is:
15
I tried different ways especially with the code below but I'm getting different result. I can't get 15 as the expected out put.
enter public class MealSolution {
private static final Scanner scanner = new Scanner(System.in);
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Enter cost of meal: ");
double meal_cost = scanner.nextDouble();
System.out.print("Enter the tip percent: ");
int tip_percent = scanner.nextInt();
System.out.print("Enter tax of meal: ");
int tax_percent = scanner.nextInt();
double tip = meal_cost * tip_percent/100;
double tax = meal_cost * tax_percent/100;
double cost = meal_cost + tip + tax;
long total_cost = Math.round(cost);
System.out.println(total_cost);
scanner.close();
}
}
To get the total cost, take the meal cost and add the tip and the tax.
double cost = meal_cost + tip + tax;
This is a loan calculator program. I'm having trouble with the math. Everything else seems to be correct except for the value of the beginning balance after 2 months. Notice that the beginning balance of the 3rd month is different from the ending balance of the 2nd month. Same thing for the succeeding months. I've been trying to fix it but everything didn't work out. I need them to be the same so the ending balance of the last month will be 0.
This is a sample output of the program:
Personal Loan Payment Calculator
Enter a loan amount: 1000
Enter the loan term (months): 6
Enter the interest rate (% per year): 9
Loan Payment and Amortization Table
Months Beginning Monthly Principal Interest Ending
Balance Payment Paid Paid Balance
1 1000.00 171.07 163.57 7.50 836.43
2 836.43 171.07 164.80 6.27 671.64
3 670.41 171.07 166.04 5.03 504.37
4 501.88 171.07 167.30 3.76 334.57
5 330.78 171.07 168.59 2.48 162.19
6 157.06 171.07 169.89 1.18 -12.83
Summary:
========
Loan Amount: $1,000.00
Monthly Payment: $171.07
Number of Payments: 6
Total Interest Paid: $24.00
Annual Interest Rate: 9.00%
This is the program:
public class LoanCalculator {
public static void main(String[] args) {
System.out.println("Personal Loan Payment Calculator"); // print the name of the program
System.out.println("================================");
Scanner keyboard = new Scanner (System.in); // define a Scanner object attached to a keyboard
String badInput; // assign non-integer or non-double inputs to badInput
System.out.print("Enter a loan amount: "); // prompt the user to enter loan amount
while ( ! keyboard.hasNextDouble()) // is the first input value a double?
{
badInput = keyboard.next();
System.out.println("Error: expected a Double, encountered: " + badInput);
System.out.println("Please enter a loan amount in Double: ");
}
double loanAmount = keyboard.nextDouble(); // assign the first input to loanAmount
System.out.print("Enter the loan term (months): "); // prompt the user to enter number of months
while ( ! keyboard.hasNextInt()) // is the second input value an int?
{
badInput = keyboard.next();
System.out.println("Error: expected an Integer, encountered: " + badInput);
System.out.println("Please enter a loan term in Integer: ");
}
int loanTerm = keyboard.nextInt(); // assign the second input to loanTerm
System.out.print("Enter the interest rate (% per year): "); // prompt the user to enter the interest rate
while ( ! keyboard.hasNextDouble()) // is the first input value a double?
{
badInput = keyboard.next();
System.out.println("Error: expected an integer, encountered: " + badInput);
System.out.println("Please enter a loan amount in Double: ");
}
double interestRate = keyboard.nextDouble(); // assign the third input to interestRate
System.out.println(); // skip a line
System.out.println(" Loan Payment and Amortization Table");
System.out.printf("%s", "=============================================================");
System.out.println();
System.out.printf("%5s %10s %10s %10s %10s %10s", "Months" ,"Beginning", "Monhtly", "Principal", "Interest", "Ending");
System.out.println();
System.out.printf(" %5s %10s %10s %10s %10s %10s", "#","Balance", "Payment", "Paid", "Paid", "Balance");
System.out.println();
System.out.printf("%s ", "=============================================================");
System.out.println();
double monthlyRate = (interestRate / 100.0) / 12.0;
double monthlyPayment = (monthlyRate * loanAmount) / ( 1 - (Math.pow( 1 + monthlyRate, - loanTerm)));
double beginningBalance = loanAmount;
double interestPaid = beginningBalance * monthlyRate;
double principalPaid = monthlyPayment - interestPaid;
int total_interest_paid = 0;
for (int monthCount = 0 ; monthCount < loanTerm ; ++monthCount)
{
int months = 1 + monthCount;
beginningBalance = loanAmount - principalPaid * monthCount;
interestPaid = beginningBalance * monthlyRate;
principalPaid = monthlyPayment - interestPaid;
double endingBalance = beginningBalance - principalPaid;
System.out.printf(" %5d %10.2f %10.2f %10.2f %10.2f %10.2f\n", months, beginningBalance, monthlyPayment, principalPaid, interestPaid, endingBalance);
total_interest_paid += interestPaid;
}
System.out.printf("%s ", "=============================================================");
System.out.println();
NumberFormat currency = NumberFormat.getCurrencyInstance();
DecimalFormat percentFormat = new DecimalFormat ("0.00");
System.out.println("\nSummary:");
System.out.println("========");
System.out.println("Loan Amount: " + currency.format(loanAmount));
System.out.println("Monthly Payment: " + currency.format(monthlyPayment));
System.out.println("Number of Payments: " + loanTerm);
System.out.println("Total Interest Paid: " + currency.format(total_interest_paid));
System.out.println("Annual Interest Rate: " + percentFormat.format(interestRate) + "%");
}
}
The error is very simple:
beginningBalance = loanAmount - principalPaid * monthCount;
Remember that "principalPaid" increases every month. The total principal paid is not the last principalPaid * mouthCount but the sum of the principal paid in all months.
You could create a running total for principalPaid like you did for interest paid.
But it would be much easier to do beginningBalance = previous month endingBalance.
I have created a simple tax calculator that will calculate Federal and Provincial taxes for incomes less than or equal to $41,536 and this worked fine.
Now to create a method that will calculate and print the tax, this isn't working for me
Here is the code..
package lab4;
import java.util.Scanner;
/**
*
* #author demo
*/
public class Lab4 {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
// Q4. Tax Calculation
Scanner sc= new Scanner(System.in);
System.out.print("Enter your income: ");
String userIncome=sc.nextLine();
calculateAndPrintTax();
}
static double calculateAndPrintTax(double userIncome)
{
double uIncome= Double.parseDouble(userIncome);
double federalExemption= 11327.0;
double provincialExemption= 9863.0;
double federalTax = (uIncome- federalExemption) * 0.15;
double provincialTax= (uIncome - provincialExemption) * 0.0505;
double totalTax= federalTax + provincialTax;
System.out.println("Your payable Federal tax is: " + federalTax);
System.out.println("Your payable Provincial tax is: "+ provincialTax);
System.out.println("Total payable tax is: "+ totalTax);
}
}
I changed some stuff and was able to compile and run the program successfully.
public static void main(String[] args)
{
// Q4. Tax Calculation
Scanner sc= new Scanner(System.in);
System.out.print("Enter your income: ");
double userIncome=sc.nextDouble();
calculateAndPrintTax(userIncome);
}
static void calculateAndPrintTax(double userIncome)
{
double federalExemption= 11327.0;
double provincialExemption= 9863.0;
double federalTax = (userIncome- federalExemption) * 0.15;
double provincialTax= (userIncome - provincialExemption) * 0.0505;
double totalTax= federalTax + provincialTax;
System.out.println("Your payable Federal tax is: " + federalTax);
System.out.println("Your payable Provincial tax is: "+ provincialTax);
System.out.println("Total payable tax is: "+ totalTax);
}
}
When you call the calculateAndPrintTax method, you have to enter parameters too, because your method asks for them. So instead of calculateAndPrintTax(); you should have calculateAndPrintTax(userIncome); because that's what the method uses to do all of its calculations. I also changed userIncome to a double to simplify the process.
You must also make calculateAndPrintTax void, and remove the double, because instead of returning any values, it prints them out instead.
Hope I could help.
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter your income: ");
String userIncome=sc.nextLine();
calculateAndPrintTax(userIncome);
}
static void calculateAndPrintTax(String userIncome)
{
double uIncome= Double.parseDouble(userIncome);
double federalExemption= 11327.0;
double provincialExemption= 9863.0;
double federalTax = (uIncome- federalExemption) * 0.15;
double provincialTax= (uIncome - provincialExemption) * 0.0505;
double totalTax= federalTax + provincialTax;
System.out.println("Your payable Federal tax is: " + federalTax);
System.out.println("Your payable Provincial tax is: "+ provincialTax);
System.out.println("Total payable tax is: "+ totalTax);
}
Assignment is to:
Display any welcome message at the top of the output screen
Create variables to hold the values for the price of a cup of lemonade.
Display the price per glass.
Ask the user for their name, and store it as a String object. Refer to the user by name, whenever you can.
Ask the user how many glasses of lemonade they would like to order. Save this as a variable with the appropriate data type.
Store the San Diego tax rate of 8% as a constant variable in your program.
Calculate the subtotal, total tax, and total price, and display it on the screen.
Ask the user how they would like to pay for the lemonade, and save the input as a char variable.
Ask the user to enter either 'm' for money, 'c' for credit card, or 'g' for gold
Using the DecimalFormat class, make all currency data printed to the screen display 2 decimal places, and also a '$" sign.
Need help figuring out how to get tax rate of 8% as a constant variable in my program
that way I can calculate the subtotal, total tax, and total price, and display it on the screen
So far this is what I have:
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class FirstProgram {
public static void main(String[] args) {
double cost = 7.55;
double amount = 7.55;
final double CA_SALES_TAX = 0.08;
int tax, subtotal, total;
subtotal = (int) (amount * cost);
tax = (int) (subtotal * CA_SALES_TAX);
total = tax + subtotal;
Scanner input = new Scanner(System.in);
double fnum = 7.55, tax1 = fnum * 0.08, answer = tax1 + fnum;
System.out.println("Welcome to the best Lemonade you'll ever taste! ");
System.out.println("My lemonade would only cost you a measly: $" + amount);
System.out.println("What is your name?");
String first_name;
first_name = input.nextLine();
System.out.println("Hi " +first_name+ ", how many glasses of lemonade would you like?");
fnum = input.nextDouble();
System.out.println("Subtotal: $" + (amount * fnum));
System.out.println("Tax: $" + (tax1 * CA_SALES_TAX));
tax1 = input.nextDouble();
Any help is appreciated
It looks like you already have the sales tax set as constant that is what the "final" keyword is being used for. As for your code i see some redundancies and am not sure as to why you are casting to integers. I made some mods for what I think you want it to do.
public static void main(String[] args) {
double cost = 7.55;
final double CA_SALES_TAX = 0.08;
double subtotal,tax,total;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the best Lemonade you'll ever taste! ");
System.out.println("My lemonade would only cost you a measly: $" + cost);
System.out.println("What is your name?");
String first_name = input.nextLine();
System.out.println("Hi " +first_name+ ", how many glasses of lemonade would you like?");
int fnum = input.nextInt();
//calc subtotal, tax, total
subtotal = fnum * cost;
tax = subtotal *CA_SALES_TAX;
total = tax + subtotal;
// print them all out
System.out.println("Subtotal: $" + (subtotal));
System.out.println("Tax: $" + (tax));
System.out.println("Total Price: $" + (total));
}
import java.util.Scanner;
public class ParadiseInfo2
{
public static void main(String[] args)
{
double price;
double discount;
double savings;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter cutoff price for discount >>");
price = keyboard.nextDouble();
System.out.print("Enter discount rates as a whole number >> ");
discount = keyboard.nextDouble();
System.out.println("Special this week on any service over " +
price);
System.out.println("Discount of " + discount + " percent");
System.out.println("That's a savings of at least $" + savings);
displayInfo();
savings = computeDiscountInfo(price, discount);
}
public static void displayInfo()
{
System.out.println("Paradise Day Spa wants to pamper you.");
System.out.println("we will make you look good.");
}
public static double computeDiscountInfo(double pr, double dscnt)
{
double savings;
savings = pr * dscnt / 100;
return savings;
}
}
I keep getting the following error on the above code. I can't figure out how to fix it. Thanks for all of your help!
ParadiseInfo2.java:18: error: variable savings might not have been
initialized
System.out.println("That's a savings of at least $" + savings);
The last line of main sets savings but you use it before then to print it. Change it to something like
// System.out.println("That's a savings of at least $" + savings);
displayInfo();
savings = computeDiscountInfo(price, discount);
System.out.println("That's a savings of at least $" + savings);