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 10 of 10

Thread: Where am I going wrong? :)

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Where am I going wrong? :)

    I'm trying to figure out this whole java thing... maybe I should gone into agriculture.. :-)

    public class TipCalculator
     
    {
     
       public static void main(String[] args)
     
       {
     
          double myCheck = 50.00;
     
          double yourCheck = 19.95;
     
          double RATE = 0.15;
     
          double bill = 50;
     
          System.out.println("Tips are" );
     
          calcTip(myCheck);
     
          calcTip(yourCheck);
     
       }
     
          public double calcTip(double bill, double RATE)
     
          {
     
             double tip;
     
             tip = bill * RATE;
     
             System.out.println("The tip should be at least " + tip);
     
          }
     
    }

    Getting error codes on the two calcTip calls?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Where am I going wrong? :)

    make them
    calcTip(myCheck,RATE);

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Where am I going wrong? :)

    Still throwing errors?

          calcTip(myCheck,RATE);
          calcTip(yourCheck,RATE);

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Where am I going wrong? :)

    add return tip; to your function

  5. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Where am I going wrong? :)

    I fixed some of the problems; still getting some errors:

    public class DebugThree1
    {
       public static void main(String[] args)
       {             
          double myCheck = 50.00;
          double yourCheck = 19.95;
          double RATE = 0.15;
          final double bill = 50;
          System.out.println("Tips are" );
          calcTip(myCheck,RATE);
          calcTip(yourCheck,RATE);
       }
     
          public static double calcTip(double bill, double RATE)
          {
             double tip;
             tip = bill * RATE;
             System.out.println("The tip should be at least " + tip);
          }
    }

    Gettoing error on second method, saying I have no return statement.. but it's right there?
    Last edited by helloworld922; October 17th, 2009 at 09:35 PM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Where am I going wrong? :)

    change the System.out.println("The tip should be at least " + tip); to return tip;. Then print out the returned value in your main method.

  7. #7
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where am I going wrong? :)

    or make it a void method instead of double

    public static void calcTip(double bill, double RATE)

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Where am I going wrong? :)

    technically, yes that would work. However, that's bad programming practice. The reason is any calling method has no way to access that value. Not to say all side effects are bad, in fact it's quite necessary sometimes. However, in this case it's much better to return the value.

  9. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Where am I going wrong? :)

    another knowledge here... so declaring a method void is NOT NECESSARY AT ALL TIMES..(but sometimes it is). hmmm
    the hardest part of that is what am i going to return? if i dont know what to return?..

    i remember what chris told me about
     void main()

  10. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Where am I going wrong? :)

          public static double calcTip(double bill, double RATE)
          {
             double tip;
     
             tip = bill * RATE;
     
             System.out.println("The tip should be at least " + tip);
     
             return tip; // try to return the value of the tip
          }

    try to return the value of the tip because your method is calculating for the tip right?
    and you need its value.. (unless you will make another method that will return the tip) something like
    getCalculatedTip() method. that wil lreturn the value of the tip...

    so after calculating all the bills and rate.. and assigining it to the tip,then
    return the value of the tip



    i agree with them.. in this case you must declare a return value for the method calcTip..
    if you make that void.. that will be limitted in use(almost not usefull) because you are just calculating the tip..

    you have to get the value of the tip right? so you have to return it...

Similar Threads

  1. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM
  2. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  3. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM
  4. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM