Results 1 to 12 of 12

Thread: Java methods

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Time Lord Member The_Doctor's Avatar
    Join Date
    Oct 2004
    Location
    The TARDIS
    Posts
    2,040

    Default Java methods

    This is my first ever method, is it alright?

    //Area of Squares
    public double Area_of_Squares(double Square_Side_Length, int Further_Number_of_Squares, int Square_Size_Increment, double Square_Area, int Counter)
    {
    System.out.println("Enter square side length");
    Square_Side_Length = data_input.nextDouble();
    System.out.println("Enter further number of squares to be calculated");
    Further_Number_of_Squares = data_input.nextInt();
    System.out.println("Enter size increment");
    Square_Size_Increment = data_input.nextInt();

    for(Counter=1; Counter==Further_Number_of_Squares; Counter++)
    {
    Square_Area = Square_Side_Length*Square_Side_Length;
    System.out.printf("Area of Square is f%" , Square_Area);
    Square_Side_Length = Square_Side_Length + Square_Size_Increment;
    }
    return 1;
    }

    Also, how do I call this method.
    Last edited by The_Doctor; 03-14-2007 at 17:27.

  2. #2
    zombologist Senior Member doc_bean's Avatar
    Join Date
    Oct 2004
    Location
    Riding Shai-Hulud
    Posts
    5,346

    Default Re: Java methods

    Quote Originally Posted by The_Doctor
    This is my first ever method, is it alright?

    //Area of Squares
    public double Area_of_Squares(double Square_Side_Length, int Further_Number_of_Squares, int Square_Size_Increment, double Square_Area, int Counter)
    {
    System.out.println("Enter square side length");
    Square_Side_Length = data_input.nextDouble();
    System.out.println("Enter further number of squares to be calculated");
    Further_Number_of_Squares = data_input.nextInt();
    System.out.println("Enter size increment");
    Square_Size_Increment = data_input.nextInt();

    for(Counter=1; Counter==Further_Number_of_Squares; Counter++)
    {
    Square_Area = Square_Side_Length*Square_Side_Length;
    System.out.printf("Area of Square is f%" , Square_Area);
    Square_Side_Length = Square_Side_Length + Square_Size_Increment;
    }
    return 1;
    }

    Also, how do I call this method.
    just type a=Area_of_Squares(ss,fn,ss,sa,c) with proper numbers (or variabels) as arguments, that should do the trick iirc.

    EDIT: in your for command, shouldn't you use <= instead of == ? It's the continue condition, not ending condition you should use iirc
    EDIT2: why do you return 1 instead of the actual area ?
    Last edited by doc_bean; 03-14-2007 at 17:34.
    Yes, Iraq is peaceful. Go to sleep now. - Adrian II

  3. #3

    Default Re: Java methods

    Since the user gives you the input you don't need any of those parameters.

    So:

    public AreaOfSquares(){

    *get info*

    *calculate area*

    }

    You would have to declare squareArea somewhere else. The other variables will have to be declared somewher as well, either locally or globally.

    for loops are usually done like so:

    for(int i = 0; i<*some number; i++){
    *stuff*

    }

    Yours doesn't make any sense though. If you want the user to be able to keep entering squares you need to put the user input stuff inside the for loop. Then you can set the initial value of FurtherSquares to one, and ask them on the first time through the loop what the value should be, update the value, and have a boolean telling whether you asked already so that you don't ask again.

  4. #4

    Default Re: Java methods

    Well, congrats on your first Java method! :)
    Also, just a remark - as far as style is concerned, C/C++ and Java have different "rules" for naming methods and such... the former use the underscores, the latter uses capitalization.
    That's not to say underscores are forbidden in Java, but the capitalized notation is preferable.

    For example, nextCount is typical Java notation. It's C/C++ equivalent would be next_count.
    Sasaki's notation is more appropriate for a Java method name.

    It's a minor nitpick.

    The other guys' comments stand, too.
    One more thing. About calling the method - since it's not static, then it will be invoked *for* some object. So, a.AreaOfSquares(...).
    Therapy helps, but screaming obscenities is cheaper.

  5. #5
    Time Lord Member The_Doctor's Avatar
    Join Date
    Oct 2004
    Location
    The TARDIS
    Posts
    2,040

    Default Re: Java methods

    EDIT: in your for command, shouldn't you use <= instead of == ? It's the continue condition, not ending condition you should use iirc
    I am inexperienced.

    EDIT2: why do you return 1 instead of the actual area ?
    I am inexperienced.

    Yours doesn't make any sense though. If you want the user to be able to keep entering squares you need to put the user input stuff inside the for loop. Then you can set the initial value of FurtherSquares to one, and ask them on the first time through the loop what the value should be, update the value, and have a boolean telling whether you asked already so that you don't ask again.
    Its not my design. For my coursework I was given a design and told to create the code for it and to test it.

  6. #6

    Default Re: Java methods

    Oh I see. The size of the square goes up in standard increments?

  7. #7
    zombologist Senior Member doc_bean's Avatar
    Join Date
    Oct 2004
    Location
    Riding Shai-Hulud
    Posts
    5,346

    Default Re: Java methods

    Quote Originally Posted by The_Doctor

    Its not my design. For my coursework I was given a design and told to create the code for it and to test it.
    What was the exact design ? It can't be very long considering it's only one method, so you might as well share it with us

    Since you're doing a relatively simple program, I'd avoid using input commands in the method, and would instead either decalre a seperate method for getting the inputs or just put it in the main code, but that might not work and is just a personal preference I assume (I'm not really experienced either).

    Do listen to Sasaki's comment though, you only need to 'give' a method variables that have a specific value, like

    public double multiply(double x,double y){return x*y;}

    if you're going to input them within the method you can declare them als local variables.
    Yes, Iraq is peaceful. Go to sleep now. - Adrian II

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Single Sign On provided by vBSSO