Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Exercise 86

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool 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:

     
    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:

     
    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! I really appreciate it!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise 86

    Can you explain what the problems are?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default 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

    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);
        }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise 86

    @dicdic Please do NOT do the OPs homework.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exercise 86

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

     
    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:

     
    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:

     
    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:

     
    TC1.java:7: error: variable money is already defined in method money(String,double)
     
        double money = Math.round(dollars * 100) \ 100;

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Exercise 86

    Alright, I lost my old code . . .
    Dog ate it?
    Is there a consistent way of using that method throughout the whole code?
    What does that mean? But I think the answer's "Yes."
    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.

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 86

    Quote Originally Posted by GregBrannon View Post
    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 View Post
    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 View Post
    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:

     
    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 ) );
     }
    }

  10. #10
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Exercise 86

    "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..
    please import..
    import java.text.DecimalFormat

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Exercise 86

    Why do you have a method inside another method?
    Why do you have a parameter without a type?
    Why do you have a method with 2 parameters (String, double)?
    Instructions state there should only be two methods: one takes a double and the other takes an int (which calls the double method).
    Improving the world one idiot at a time!

  12. #12
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 86

    Okay that makes sense. Thanks. I'll submit some more code pretty soon.

Similar Threads

  1. Exercise
    By keepStriving in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 06:58 PM
  2. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 09:40 PM
  3. Java exercise
    By nhiap6 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 2nd, 2013, 11:04 AM
  4. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM
  5. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM

Tags for this Thread