Results 1 to 14 of 14
  1. #1
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default Do not know how to solve this

    Hi I'm a new student of java programming. I ran this code, there's a problem but I do not know how to solve it. Please help.

    Java Code:
    public class Q3 {
    
    	double length=5, width=8, height=9, radius=6;
    
        public static double volume(double length, double width, double height) {
    
        	double box= length * width * height;
    
        	return box;
        }
    
        public static void main(String[] args){
    
        	System.out.println("The volume of rectangular box is "+ volume());
        }
    }

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Pass the parameters to your volume method.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Hi I've made amendments to it and it works, thanks Goldest!

    System.out.println("The volume of rectangular box is "+ volume(5,8,9));

    Initially I put the parameters as

    System.out.println("The volume of rectangular box is "+ volume(length,width,height));

    Question, is there a way I can pass English words as parameters instead of numbers?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jaylimix View Post
    Hi I've made amendments to it and it works, thanks Goldest!
    System.out.println("The volume of rectangular box is "+ volume(5,8,9));
    Initially I put the parameters as
    System.out.println("The volume of rectangular box is "+ volume(length,width,height));

    Question, is there a way I can pass English words as parameters instead of numbers?
    Sure you can as long as you define those English words to be visible variables; here you define three local variables in your main( ... ) method:

    Java Code:
    double length= 5.0;
    double width= 8.0;
    double height= 9.0;
    Next you can call your method as:

    Java Code:
    System.out.println("The volume of rectangular box is "+ volume(length,width,height));
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by jaylimix View Post
    System.out.println("The volume of rectangular box is "+ volume(length,width,height));
    This should be the proper way. What happened when you tried this?
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by goldest View Post
    This should be the proper way. What happened when you tried this?
    No, those variables are instance variables but there is no instance.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Goldest and Jos, when I tried this, it has errors:

    Java Code:
    System.out.println("The volume of rectangular box is "+ volume(length, width, height));
        	                                                                              ^
    3 errors
    What does this mean?
    Last edited by jaylimix; 05-30-2011 at 05:22 PM.

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by JosAH View Post
    No, those variables are instance variables but there is no instance.
    Yeah, I know. I actually wanted the OP to come up with "Cannot make a static reference to the non-static field" error. That way he would have learned how to do it. Nothing else.

    And yes, having those variables local would resolve the things in first place.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    Yeah, I know. I actually wanted the OP to come up with "Cannot make a static reference to the non-static field" error. That way he would have learned how to do it. Nothing else.

    And yes, having those variables local would resolve the things in first place.

    Goldest
    Yes true it does comes up with that

    non-static variable length cannot be referenced from a static context
    System.out.println("The volume of rectangular box is "+ volume(length, width, height));
    ^

  10. #10
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Ya I manage to make it work by adding a static in front of the variable. It's just guessing for me based on the error.

  11. #11
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Default

    Quote Originally Posted by jaylimix View Post
    Yes true it does comes up with that
    Good. Now go back and see what Jos had said in post 4,

    Quote Originally Posted by JosAH View Post
    here you define three local variables in your main( ... ) method:
    I hope that would make it clear.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by jaylimix View Post
    Ya I manage to make it work by adding a static in front of the variable. It's just guessing for me based on the error.
    Aahh, well thats another way.

    Considering that you are new to Java, I would strongly suggest you to have a look at the common java errors here : Top Ten Errors Java Programmers Make.

    Hope that would be beneficial for you.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  13. #13
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Ya I got what you guys mean about the define as local variable Thanks for the help! Thanks for link too.

  14. #14
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    You're welcome!

    Keep coming back here in case of any further problems.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Please help me to solve it(jsp)
    By srengvichet in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-15-2010, 03:52 PM
  2. PLS PLS PLS PLS solve this
    By unagie in forum New To Java
    Replies: 10
    Last Post: 07-11-2009, 09:10 PM
  3. Plz solve this....
    By theone3nu in forum Java 2D
    Replies: 3
    Last Post: 01-08-2009, 05:01 PM
  4. Plz solve this....
    By theone3nu in forum New To Java
    Replies: 9
    Last Post: 12-23-2008, 09:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •