• Post Reply Bookmark Topic Watch Topic
  • New Topic

beginner java here having trouble with homework  RSS feed

 
Gus Flores
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
were asked to create a mileage caclulator to calculate the amount of money that should be paid to employees. The mileage is computed as follows


i'm not asking for anyone to finish the homework for me since i do want to learn but having trouble with the getcurrency format

An amount of .25 for each mile up to 100 miles

An amount of .15 for every mile above 100.

So 115 miles would be

(.25 * 100) + (.15 * 15)

This can all be coded using a mathematical solution but I want you to use an if / else statement. Here is how you are to implement it:

If the total miles input is less than or equal to 100 then simply calculate the miles * .25 and output the amount

otherwise compute the total amount for all miles mathematically.

Input: Total number of miles
Process: dollar amount owed for miles driven
Output: Amount of money due
Formatting Output

Use the getCurrencyInstance method of the NumberFormat class to output the value in dollars and cents. Use the directions in lab 3 as your guide

* Please note that you should simply do calculations within the if and else statements. This mean no input or output within if or else. Do it afterward.

i have this down so far


 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find things work much better if you don't use fractions. Instead of using $0.25, use 25 cents. Computers have a really hard time with decimal fractions, and you will eventually see rounding errors.

But beyond that, do you have a specific question we can help answer?
 
Gus Flores
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was having trouble understanding how the numberFormat worked and when i tried to put 25 cents in the line and it doesn't seem to work i then tried out this

NumberFormat fmt = NumberFormat.getCurrencyInstance();
String rate1 = fmt.format(25);
String rate2 = fmt.format(15);

Scanner in = new Scanner(System.in);
System.out.println("Enter Miles");
int miles = in.nextInt();
cents = fmt.format;

i tried to put cents in there but i get errors and i always get errors in the System.out.println("The Amount owed= " + (String rate1 * miles));
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get errors? If you want help with those errors, it's going to help if you tell us about them. So... what do they say? When do they happen? What's the context when they happen? It helps if you copy them and then paste them here, rather than trying to retype them or (worse) providing a vague paraphrase. Have a look at our FAQ page HowToAskQuestionsOnJavaRanch for a lot more about how to ask good questions.
 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gus Flores,

I've added code tags to your original post. You should add yourself next time you post code. See how better it looks. Please check how to UseCodeTags (<- link).

Nevertheless, your code indentation and formatting are quite poor. You're missing quite a lot "curly" braces around "if" and "else" statements, you shouldn't do that.

Other problems:
1. variable "miles" declared outside "main" method, I presume it happened by mistake, since you're declaring it within "main" method. So the one which is outside method is not used and not necessary.
2. lines 19, lines 22, you're trying to multiply String data type by int. It is illegal.
3. Once you fix those lines, on line 22, you'd need to use extra parenthesis around (rate1 * miles) + (rate2 * miles), otherwise you'd get concatenated string.
Example: "Text" + (4+4) + (4+4) gets printed out as "Text88", and if it were "Text" + ((4+4) + (4+4)) it would get printed out as "Text16".

Once you fix these problems, you might get what you expect. Not sure, likely could be a more problems.

Maybe you'd like also to have a look to Oracle's tutorial about formats (<- link).
 
Gus Flores
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the new code. thanks for the code tag.

yes, the int miles outside of the main was a problem and removed it and it worked after.
I have a red ! on line 38

if its <=100 miles it works fine with the if statement but if its >= 100 miles on the else statement i get 27.516.5

 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You didn't improve your code indentation and formatting.
2. When you enter more than 100 miles you're getting unexpected results, because of my mentioned reasons in my above post.

You should read more carefully.
 
Gus Flores
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm stumped i have no idea what i'm doing wrong. i found out about the indent problems i had. i wasn't pressing tab. i don't know why the 2nd line when entered greater than 100 miles isn't working. i believe it has something to do with the extra miles after the 100 but i don't know how to write the code out.
if my teacher ask for this does it mean i shouldn't have the miles * r1 in the system out print ? that i should have it in the if () ? Please note that you should simply do calculations within the if and else statements. This mean no input or output within if or else. Do it afterward.


 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, code indentation and formatting looks decent. But still one problem exist what I mentioned before. Lets try again.

I'm talking about this part:

In Java operators has precedences. Some of them has higher precedences than others. What that means, it means, that expressions which using higher precedence operators being evaluated first.
Example: 1+2 * 3. So, first multiplication happens, then addition.
BUT, apart from that, expressions, except assignments are being started evaluate from LEFT to RIGHT. So, an example:
1 +2 + 3. First, 1 + 2, then result + 3.

What happens in your case in "else" code block.
"Your text here" + (miles * r1) + (miles * r2) and here you have the order of evaluation:
1. miles * r1 = Result1
2. miles * r2 = Result2
3. "Your text here " + Result1 = "Your text here Result1" (becomes all string after string concatenation)
4. "Your text here Result1" + Result 2 = "Your text here Result1Result2" (all string)

So how to fix that?
In order to make your calculations happen first, you need to enclose all calculation part with an extra "parentheses" example: "Text " + ((num1 * num2) + (num3 * num4)), because everything what is in "parentheses" being evaluated first. So, how it would be evaluated:
1. miles * r1 = Result 1
2. miles * r2 = Result 2
3. Result1 + Result2 = Result 3
4. "Your text here " + Result 3 = "Your text here Result 3
 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gus Flores wrote:Please note that you should simply do calculations within the if and else statements. This mean no input or output within if or else. Do it afterward.

This part means, that, within "if" and "else" blocks you should do calculations and get result, and only after these blocks you should print result. Example:
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using number format classes? Since the printf method and similar were introduced nearly 11 years ago, most people have regarded number format classes as obsolete. Anyway, get the calculation sorted before you display the result.
 
Liutauras Vilda
Sheriff
Posts: 4913
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, check your used formula in "else" block, I'm not sure it is correct, you have to decide.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!