Exercise 86

• November 28th, 2013, 01:26 PM
Exercise 86
Hi, I'm having a little trouble with exercise 86. I could totally use some help. Anyone think I'm off to a good start? What do I need to accomplish? I think it has to do with rounding...

Here are the instructions:

Code Java:

```  Complete the following definitions for the overloaded one-argument static method money that returns a String representing its argument as an amount of money. For example,   if the input is the double 2.5, then it returns the String "\$2.50";   if the input is the int 6, then it returns the String "\$6.00"; and   if the input is the String "6.125" then it returns the String "\$6.13".   Hint: Define the money( double d ) version first. Then define the other versions by changing their argument into a double and returning the result of applying the double version to the transformed argument.   Note: When the input is a String you may need to use Double.parseDouble.```

Here is my code:

Code Java:

```  public static String money( int n )       {   // ...   }       public static String money( double d )       {   money *= 100;   money += 5;   money /= 100;   // ...   }       public static String money( String s )       {   // ...   }       public static void main( String[] args )       {   System.out.println( money( 2.538 ) );   }```

Thanks for the help! :D I really appreciate it! :cool:
• November 28th, 2013, 01:36 PM
Norm
Re: Exercise 86
Can you explain what the problems are?
• November 28th, 2013, 07:07 PM
dicdic
Re: Exercise 86
try to parse your input to double (Double.parseDouble(input)) if input is string,, cast it to double if input is int..
then after that,, use DecimalFormat class,, it can format your double value the way you want,, in your case
you want to format your double variable into 2 decimal only,, I hope it helps you :)

--- Update ---

here is a sample code,, it will satisfy your conditions, i believe..
i just made it static,, but please avoit using static if its not necessa.. tnx :)

Code :

```private static String money(String input) { DecimalFormat df = new DecimalFormat("\$0.00"); return df.format(Double.parseDouble(input)); }   private static String money(int input) { DecimalFormat df = new DecimalFormat("\$0.00"); return df.format((double) input); }   private static String money(double input) { DecimalFormat df = new DecimalFormat("\$0.00"); return df.format(input); }```
• November 28th, 2013, 07:33 PM
Norm
Re: Exercise 86
@dicdic Please do NOT do the OPs homework.
• November 29th, 2013, 05:54 PM
Re: Exercise 86
@dicdic

I tried it your way and got this error:
--------------------------------------------------------

"cannot find symbol"

--------------------------------------------------------

But that doesn't mean that your code won't work. I think it might work fine, but I didn't really want someone to GIVE me the answer. Now that I already have the code though, I think it would make sense to modify it a little further. I really wanted clues and hints as opposed to a definite answer since this is for school.

Thanks anyways though. Your code doesn't completely work. But thanks.

--- Update ---

@Norm:

It's giving me errors that say "cannot find symbol. I also think there are compilation issues.
• November 29th, 2013, 06:04 PM
Norm
Re: Exercise 86
Please copy the full text of the error message and paste it here. It has important info about the error.
• November 29th, 2013, 11:58 PM
Re: Exercise 86
Alright, I lost my old code, which I should have emailed to myself. But when I put it back together, here is the error message. This is pretty much dicdic's code, but I'm going to modify it again anyways, so whatever.

Here is the error message:

Code Java:

```  TC1.java:7: error: cannot find symbol   DecimalFormat df = new DecimalFormat("0.00");```

--- Update ---

I will. Thanks.

--- Update ---

I changed the first part of the code a little bit. Tell me if this is on the right track:

Code Java:

```  public static String money(String money, double dollars)   {       double money = Math.round(dollars * 100) / 100;       }       public static String money(int input)   {       DecimalFormat df = new DecimalFormat("0.00");   return ("\$" + df.format((double) input));       }       public static String money(double input)   {       DecimalFormat df = new DecimalFormat("0.00");   return df.format(input);       }       public static void main( String[] args )   {   System.out.println( money( 2.538 ) );   }```

I think I've been editing it to look like this part:

Code Java:

```  public static String money(String money, double dollars)   {       double money = Math.round(dollars * 100) / 100;       }```

Is there a consistent way of using that method throughout the whole code?

As is I get this, but I THINK (I'm not sure) that means that I'm going to have to come up with new arguments:

Code Java:

```  TC1.java:7: error: variable money is already defined in method money(String,double)   double money = Math.round(dollars * 100) \ 100;```
• November 30th, 2013, 02:36 AM
GregBrannon
Re: Exercise 86
Quote:

Alright, I lost my old code . . .
Dog ate it?
Quote:

Is there a consistent way of using that method throughout the whole code?
What does that mean? But I think the answer's "Yes."
Quote:

I THINK (I'm not sure) that means that I'm going to have to come up with new arguments
No, it means you should remove the 'double' declaration from the line in the body of the method. That's also a method defined to return a String object with no return, so that'll be the next problem with that method. Maybe your practice of posting methods with huge gaps of blank lines indicates there's more code there, but I'm not sure.
• November 30th, 2013, 03:58 PM
Re: Exercise 86
Quote:

Originally Posted by GregBrannon
Dog ate it?

No, I forgot to email it to myself. This AP online course won't let me save my code usually.

Quote:

Originally Posted by GregBrannon
What does that mean? But I think the answer's "Yes."

I mean, can I use just the math.round method to do this throughout the entire code?

Quote:

Originally Posted by GregBrannon
No, it means you should remove the 'double' declaration from the line in the body of the method. That's also a method defined to return a String object with no return, so that'll be the next problem with that method. Maybe your practice of posting methods with huge gaps of blank lines indicates there's more code there, but I'm not sure.

I think I fixed that. Here's my new code so far. I don't know what to put in the middle:

Code Java:

```  public static String money(String money, double dollars){   public static String money(int dollars){   money = Math.round(dollars * 100) / 100.0; } public static String money(input){   } public static void main( String[] args ) { System.out.println( money( 2.538 ) ); } }```
• December 1st, 2013, 06:38 PM
dicdic
Re: Exercise 86
Quote:

"cannot find symbol"
did you import DecimalFormat?? if not,, it will obviously not known when you compile it..
you need to import it first from import java.text.DecimalFormat..
the "Math" class youve been using came from the core package, that is why you
dont need to import it..