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);
}
Related
this is my first question on the site. I am a fresh CS student needing some help with something that is probably really simple. The code as is will compile. When I enter in the values as the program asks, it stores the values wrong. It will store the right values for gross pay and savings rate but the IRA rate comes back as 100% even when entered at 6.9 and it seems it stores the IRA rate in saveAmount. Please halp me figure out what I am doing wrong here.
import java.text.DecimalFormat;
import java.util.*;
public class CollinDunn_1_05 {
static Scanner console = new Scanner(System.in);
static DecimalFormat formatCash = new DecimalFormat("#,###.00");
static double iraTotal = 0.0;
static double saveAmount = 0.0;
static double totalSave = 0.0;
static String line = "";
public static void main (String [] args) {
// Input variables
double grossPay = 0.0; // The gross pay from a users paycheck
double saveRate = 0.0; // This is the user entered savings rate
double iraRate= 0.0; // The IRA investment rate
String whichOne = ""; // A temp variable to pass a string type into UserInput
printInfo();
grossPay = userInput("gross pay");
saveRate = userInput("savings rate");
iraRate = userInput("IRA rate");
iraTotal = iraAmount(grossPay, iraRate);
saveAmount = savingsAmount(grossPay, saveRate);
outputResults(grossPay, saveRate, saveAmount, iraRate, iraTotal);
return;
} // End Main
public static void printInfo() {
System.out.println ("This program uses methods to calculate \n"
+ "savings amounts and IRA investment amounts \n"
+ "from user input consisiting of their gross pay, \n"
+ "their desired savings rate and IRA rate, made by "
+ " Collin Dunn");
return;
} // End ProgramInfo
public static double userInput(String whichOne) {
double saveMe = 0.0;
System.out.print("Please enter your " + whichOne + ": ");
saveMe = console.nextDouble();
return saveMe;
} // End userInput
public static double iraAmount(double grossPay, double iraRate) {
iraTotal = grossPay * (iraRate / 100.0);
return iraTotal;
} // End iraAmount
public static double savingsAmount(double grossPay, double saveRate) {
saveAmount = grossPay * (saveRate / 100.0);
return saveAmount;
} // End savingsAmount
public static void outputResults(double grossPay, double saveRate, double iraRate,
double saveAmount, double iraTotal) {
totalSave = saveAmount + iraTotal;
System.out.print ("With a gross pay of $" + formatCash.format(grossPay)
+ ", a savings rate of %" + formatCash.format(saveRate)
+ " and a IRA rate of %" +formatCash.format(iraRate)
+ ".\n Your savings amount will be $" + formatCash.format(saveAmount)
+ ", with a investment amount of $" + formatCash.format(iraTotal)
+ ".\n Which leaves you with a total savings of $" +
+ totalSave + ". Way to go for paying yourself!" );
return;
} // End outputResults
} //End Class
Your only issue is the order of arguments you pass to or have set on the outputResults() method.
Change the signature of the method to:
public static void outputResults(double grossPay, double saveRate, double saveAmount, double iraRate, double iraTotal) {
Which now matches how you call the method:
outputResults(grossPay, saveRate, saveAmount, iraRate, iraTotal);
Let me make a couple of additonal suggestions:
1) You are consistently naming arguments in your method signatures the same names as global variables, which makes it confusing which is which when accessing the variable in the method. Either avoid using the same names for the method input variables, or use something like this.amount = amount to make it more obvious of your intention.
2) Avoid static unless you have a valid reason to use it (which is pretty rare).
Instead, take advantage of Java's Object oriented nature and create an instance of your class in the main method and call methods on that instance. This will make your code more readable, reliable, and reusable.
3) In a method that returns type 'void', you don't need to add the empty return; statement.
To correct your issue and also demonstrate the points I listed, I have refactored your code and provided it below. By the way, you have a lot of potential. Despite the fact there are a few details you can improve, for being a first year CS student, your code is well written and thought out. Good job!
import java.text.DecimalFormat;
import java.util.*;
public class CollinDunn_1_05 {
DecimalFormat formatCash = new DecimalFormat("#,###.00");
double iraTotal = 0.0;
double saveAmount = 0.0;
double totalSave = 0.0;
double grossPay = 0.0; // The gross pay from a users paycheck
double saveRate = 0.0; // This is the user entered savings rate
double iraRate= 0.0; // The IRA investment rate
public CollinDunn_1_05(double gross, double saveRt, double iraRt){
this.grossPay = gross;
this.saveRate = saveRt;
this.iraRate = iraRt;
}
public void calculate(){
calcIraAmount();
calcSavingsAmount();
}
public static void main (String [] args) {
Scanner scanner = new Scanner(System.in);
printInfo();
CollinDunn_1_05 program = new CollinDunn_1_05(
userInput("gross pay", scanner),
userInput("savings rate", scanner),
userInput("IRA rate", scanner)
);
program.calculate();
program.outputResults();
} // End Main
public static void printInfo() {
System.out.println ("This program uses methods to calculate \n"
+ "savings amounts and IRA investment amounts \n"
+ "from user input consisiting of their gross pay, \n"
+ "their desired savings rate and IRA rate, made by "
+ " Collin Dunn");
return;
} // End ProgramInfo
public static double userInput(String whichOne, Scanner console) {
double saveMe = 0.0;
System.out.print("Please enter your " + whichOne + ": ");
saveMe = console.nextDouble();
return saveMe;
} // End userInput
public void calcIraAmount() {
iraTotal = grossPay * (iraRate / 100.0);
} // End iraAmount
public void calcSavingsAmount() {
saveAmount = grossPay * (saveRate / 100.0);
} // End savingsAmount
public void outputResults() {
totalSave = saveAmount + iraTotal;
System.out.print ("With a gross pay of \$" + formatCash.format(grossPay)
+ ", a savings rate of %" + formatCash.format(saveRate)
+ " and a IRA rate of %" +formatCash.format(iraRate)
+ ".\n Your savings amount will be \$" + formatCash.format(saveAmount)
+ ", with a investment amount of \$" + formatCash.format(iraTotal)
+ ".\n Which leaves you with a total savings of \$" +
+ totalSave + ". Way to go for paying yourself!" );
} // End outputResults
} //End Class
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
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);
public static void main(String args [])
{
double dimes;
double quaters;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Dimes:");
dimes = input.nextDouble();
System.out.print("Enter Number of Quaters:");
quaters = input.nextDouble();
double dollars= dollar_amount(dimes,quaters);
System.out.println("Dollar Amount total: $" + dollars);
public static double dollar_amount(dimes,quaters);
dollars= dollar_amount(dimes,quaters);
System.out.println("Total dollar amount: $" + dollars);
}
double dollar_amount(double dimes, double quaters);
{
dollars = (0.10 * dimes)+(0.25 * quaters);
}
return dollars;
}
}
}
I have a question on how to call a method. I had follow the hints that I was given, but somehow I don't quite get on calling a method.
hint:
input dimes
input quarters
call the method dollar_amout(dimes, quarters)
dollars = dollar_amount(dimes, quarters)
end main
double dollar_amount(dimes, quarters)
dollars = 0.10 x dimes + 0.25 x quarters
return dollars
end method
I think your method body is wrong
Double dollar_amount(double dimes, double quaters);
{
Double dollars = (0.10 * dimes)+(0.25 * quaters);
return dollars;
}
And your calling it right place. No problem with that
What you are doing is trying to set the value of a variable using a method. You must define the method dollar_amount(dimes,quaters)properly before you can call it.
Paste this code over your current code, into your main class.
public static void main(String args []){
double dimes;
double quaters;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Dimes:");
dimes = input.nextDouble();
System.out.print("Enter Number of Quaters:");
quaters = input.nextDouble();
double dollars= dollar_amount(dimes,quaters);
System.out.println("Dollar Amount total: $" + dollars);
}
public static double dollar_amount(dimes,quaters){
dollars = (0.10 * dimes)+(0.25 * quaters);
return dollars;
}
You have to make some changes, try like this.
public static void main(String args[]) {
double dimes;
double quaters;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Dimes:");
dimes = input.nextDouble();
System.out.print("Enter Number of Quaters:");
quaters = input.nextDouble();
double dollars = dollar_amount(dimes, quaters);
System.out.println("Dollar Amount total: $" + dollars);
dollars = dollar_amount(dimes, quaters);
System.out.println("Total dollar amount: $" + dollars);
}
public static double dollar_amount(double dimes, double quaters) {
double dollars = (0.10 * dimes) + (0.25 * quaters);
return dollars;
}