- This topic has 27 replies, 6 voices, and was last updated 9 years ago by makecoldplayhistory.
-
Coding help – last one, the deadline's in 2 hours!
-
makecoldplayhistoryFree Member
I’ve written the following code (I know it doesn’t follow best practice).
Can anyone see why, after the values are entered, the code does nothing more?
Thanks
import java.util.Scanner;
/*
so that we can accept inputs from the user; income, business expenses
and tax deductable income
*/public class TaxCalculator {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// constants declared
final double tax_rate_none = 0.0; // zero tax paid userIncome < 10000 used double to avoid confusion for programmer
final double tax_rate_lower = 0.2; // 20% tax between 10000 and 30000
final double tax_rate_mid = 0.4; //40% tax between 30000 and 50000
final double tax_rate_upper = 0.45; //45% tax 50000 and up
//variables declared
double userIncome; //input from the user – their total income
double bus_exp; //input from the user – their business expenses
double deduc_exp; //input from the user – their deductable expenses
double tax_inc; //taxable income = userIncome – bus_exp (calculated by code)
double tax_rate; //calculated by code dependent on value of income – business expenses
double tax_due; //tax_inc – deduc_exp = tax_due (outputted to user)//get input from the user and assign the input to the variables
System.out.print(“Please enter your income before expenses and deductables: (then press enter)”); //ask user’s income
userIncome = reader.nextDouble(); //assign user entered value to userIncomeSystem.out.print(“Please enter your business expenses:”); //asks user to input business expenses
bus_exp = reader.nextDouble();//assign the input to the variableSystem.out.print(“Please enter your deducatable expenses:”); // user inputs deductable expenses
deduc_exp = reader.nextDouble(); //assign input to the variable//if / else assigns a value to tax rate
tax_inc = userIncome – bus_exp; //sets the value of taxable income as th user’s income – their business expenses
tax_due = (tax_inc – deduc_exp); //* tax_rate; //calculates the tax due
if (tax_due <= 50000){
tax_rate = tax_rate_upper;
} else if ((tax_due <= 50000) && (tax_due >= 30000)){
tax_rate = tax_rate_mid;
} else if ((tax_due <= 30000) && (tax_due >= 10000)) {
tax_rate = tax_rate_lower;
} else if (tax_due < 10000) {
tax_rate = tax_rate_none;if (tax_due < 0) { System.out.print(“You are owed” + tax_due); }
else if (tax_due > 0) {System.out.print(“You owe” + tax_due); }
else if (tax_due == 0); {
System.out.print(“We’re all square.”); {
}}
}
}
}mikewsmithFree Memberwell for a start can you not indent…..
Sorry no help 🙁 but it might keep it near the topdudeofdoomFull MemberQuick scan but I think one your closing } is in wrong place …. And your output is inside a condition ..
haven’t got an editor so can’t really check it for ya
MackemFull MemberI think you need to move this code..
if (tax_due < 0) { System.out.print(“You are owed” + tax_due); }
else if (tax_due > 0) {System.out.print(“You owe” + tax_due); }
else if (tax_due == 0); {
System.out.print(“We’re all square.”);to just before the last }
This is a guess as it’s too late to figure out the nesting.
chambordFree MemberYou’ve got a brace in the wrong place (If it’s supposed to do what I think it is).
Make sure it’s all aligned right (What text editor are you using?), you should spot it.
EDIT: Well ^^^ yeah that but he could have got that one with a bit of indentation 😛
EDIT2: For tidyness I would suggest using the println method instead of print as it adds a newline.
makecoldplayhistoryFree Memberedit: The indents are lost in the copy / paste into stw
import java.util.Scanner;
/*
so that we can accept inputs from the user; income, business expenses
and tax deductable income
*/public class TaxCalculator {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// constants declared
final double tax_rate_none = 0.0; // zero tax paid userIncome < 10000 used double to avoid confusion for programmer
final double tax_rate_lower = 0.2; // 20% tax between 10000 and 30000
final double tax_rate_mid = 0.4; //40% tax between 30000 and 50000
final double tax_rate_upper = 0.45; //45% tax 50000 and up
//variables declared
double userIncome; //input from the user – their total income
double bus_exp; //input from the user – their business expenses
double deduc_exp; //input from the user – their deductable expenses
double tax_inc; //taxable income = userIncome – bus_exp (calculated by code)
double tax_rate; //calculated by code dependent on value of income – business expenses
double tax_due; //tax_inc – deduc_exp = tax_due (outputted to user)//get input from the user and assign the input to the variables
System.out.print(“Please enter your income before expenses and deductables: (then press enter)”); //ask user’s income
userIncome = reader.nextDouble(); //assign user entered value to userIncome
reader.nextLine();System.out.print(“Please enter your business expenses:”); //asks user to input business expenses
bus_exp = reader.nextDouble();//assign the input to the variable
reader.nextLine();System.out.print(“Please enter your deducatable expenses:”); // user inputs deductable expenses
deduc_exp = reader.nextDouble(); //assign input to the variable
reader.nextLine();//if / else assigns a value to tax rate
tax_inc = userIncome – bus_exp; //sets the value of taxable income as th user’s income – their business expenses
tax_due = (tax_inc – deduc_exp); //* tax_rate; //calculates the tax due
if (tax_due <= 50000){
tax_rate = tax_rate_upper;
} else if ((tax_due <= 50000) && (tax_due >= 30000)){
tax_rate = tax_rate_mid;
} else if ((tax_due <= 30000) && (tax_due >= 10000)) {
tax_rate = tax_rate_lower;
} else if (tax_due < 10000) {
tax_rate = tax_rate_none;if (tax_due < 0) { System.out.print(“You are owed” + tax_due); }
else if (tax_due > 0) {System.out.print(“You owe” + tax_due); }
else if (tax_due == 0); {
System.out.print(“We’re all square.”); {
}}
}
}
}Which brace where? I’ve been up for 22 hours now. I can barely even see braces any more. Just Reeses cup wrappers!
FlaperonFull MemberAre you using an IDE like Eclipse or just a text editor?
Indenting will help enormously. (You have a {} near the end that breaks your code.)
makecoldplayhistoryFree Memberhow near the end flaperon?
I need to zip and submit!
makecoldplayhistoryFree MemberI removed {} from after the end of we’re all square.”);
still does nothing after inputting data.
I’m using a compiler. It’s all nicely indented here!
chambordFree MemberThe brace on your final if else block is in the wrong place.
EDIT: Sorry that wasn’t very clear.
You’ve left this bit
} else if (tax_due < 10000) { tax_rate = tax_rate_none;
Without closing the brace, so the conditional and print statements that follow are only executed in this case.
You need to put
} else if (tax_due < 10000) { tax_rate = tax_rate_none; }
And then remove the extra closing brace from the bottom of the file.
makecoldplayhistoryFree MemberBut, can anyone see why the code gives the wrong answer?
mikewsmithFree Memberfrom a quick glance no, by wrong answer do you mean the wrong number?
Trying not to be patronising but it’s doing what you told it to do, follow the logic through with the numbers you are using to test.
chambordFree MemberYou haven’t multiplied tax_due by tax_rate.
Does it assume you pay a single rate? Or should the first 10000 be tax free, then up to 30000 be 20% etc? Because it wont do that..
makecoldplayhistoryFree MemberI can’t see how to do that Andy…
edit: it’s a set amount. not first 10000 free etc
mikewsmithFree MemberWhat are you trying to output? If it’s tax owed I can’t see where you multiply tax rate by income
makecoldplayhistoryFree MemberNo, that’s what I’m missing Mike and I cant see how to add it
chambordFree MemberTBH I think you’re definition of tax_due is incorrect.
I’d have thought
tax_inc = userIncome - bus_exp - deduc_exp; // ... // Calculate tax_rate here // ... tax_due = tax_inc * tax_rate // ... // Print how much tax is due here // ...
Something more like that.
EDIT: Although I don’t really know how it all works – do you pay tax on business expenses?! No idea 😛
FlaperonFull MemberSince the deadline has passed, I have no issue posting an example of what you might be aiming for (note – this is “in” Java but won’t compile, it’s just an example of how it can be done. Java may throw a wobbler about implicitly casting an integer to a string, too).
If the deadline hasn’t passed, it’d be immediately obvious that this is someone else’s work anyway.
I’ve only done a basic rate and upper rate calculation, but it could be easily extended.
The most important thing is to have an idea in your head of how it should work before starting. I also discovered that it if looks complicated, there’s almost certainly an easier and clearer way to do it.
In this example, think about how you’d work out how much tax you’d owe if told to do it by hand. If less than £10000, you’d immediately say, “I don’t owe any”, and leave it at that.
Otherwise, you know you’re going to have to pay SOME tax at the basic rate, so you work that bit out next. Etc. Etc.
makecoldplayhistoryFree Memberfrom the spec.
income – business expenses = tacxable income
work out tax due on taxable income
subtract deductable expenses from tax due
there’s 40 mins left. tbh, I’m writing in my commentary now that it failed…
edit: flaperon – it would take me hours to get that to compile! Not going to be able to plagiarize anything there (not that I would, if I could). Believe it or not, in one of the other three courseworks, I referenced a reply to an STW thread I started about coding.
mikewsmithFree MemberHonestly work backwards
Tax Due = Tax Rate * Taxable Earning
Work out the Tax Rate which you have done
Work out the earning which you have doneIs this distance learning? You need to have a chat with your tutors if you are not getting it.
FlaperonFull Member^^ what mike said.
If you look at my example, it works in the reverse order to yours – as I said, it follows the logical path if you were working it out yourself on a bit of paper (or at least the path I would take…). 😉
makecoldplayhistoryFree MemberI worked it out on paper first (taking advice from another thread.
I wrote
Still in pseudo code, the order of code (and calculations will be)
1. Ask user for income (inc)
2. Ask user for business expenses (bus_exp)
3. Ask user for tax deductible expenses (deduc_exp)
4. Inc – bus_exp = taxable income (tax_inc)
5. If tax_inc < 10,000 print out “no tax”
6. If 30,000 > tax_inc <= 10,000 calculate 20% of tax_inc = (inc_tax)
7. If 30,000 =< tax_inc > 50,000 calculate 40% of tax_inc = (inc_tax)
8. If 50,000 =< tax_inc calculate 45% and move value to stage = (inc_tax)
9. Tax due (t_d) = inc_tax – deduc_exp
10. If t_d < 0 return “you are owed”+t_d
11. If t_d > 0 return “you owe” + t_dMike, it’s distance learning but unsupported. No tutors, discussion boards / groups or anything like that.
chambordFree MemberThat isn’t what you’ve written in your code though, you have number (9) straight after number (4).
mikewsmithFree MemberUnsupported distance learning? You need to get into something like stack overflow and some good Google fu.
Where are you doing or planning to do the final calc. What’s the “wrong” answer it’s spitting out? Work back from there to see where you are wrong. If you are building on this going forward you need to understand or stop now.makecoldplayhistoryFree MemberI’ve been awake for 25 hours now (and sat a desk for most of them).
Finished 3 assignments in 6 days (but haven’t left the house).
It’s time to get breakfast and wake my boys up and get them ready for school. A post-mortem can come on Monday!
Thanks to anyone who’s contributed to any of the threads here. I got a massive amount of help.
Yes, lots of stack overflow and I’ve ordered a couple of java trainers from amazon.
The topic ‘Coding help – last one, the deadline's in 2 hours!’ is closed to new replies.