Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By TeacherZ

Thread: Passing arguments to method

  1. #1
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Passing arguments to method

    I am following one of the newer tutorials (published in the later half of this century).
    But I am running into a problem as I try to play with the examples. For me, playing with
    the code helps me see what I can and cannot do and helps me set it in my brain. Forgive me
    for what is probably a really dumb thing...

    Here is my class definition that works:

    public class FermatCheck {
    boolean fermat;
    void fermat(){
    if (fermat == true) {
    System.out.println("Fermat was Wrong!!");
    }
    else {
    System.out.println("Fermat was right - this time!");
    }
    }

    This was not copied from the book, something I made up to solve the exercise

    Now when I try to add other "methods" (is this the right nomenclature?) I get errors.

    This is one method I tried to insert into my class definition:

    int sum;
    int sum(int a, int b, n) {
    sum = a^n + b^n;
    }

    I get the error:

    FermatCheck.java:20:12: error: ';' expected
    int sum(int a, int b, n) {
    ^
    I'm guessing I cannot pass arguments to this method, but that seems rather limiting.
    Other than letting me know why this is illegal, is there a way to pass arguments or
    should I just wait out the tutorial to get that piece of knowledge?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Passing arguments to method

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Java Code:
    int sum(int a, int b, n) {
    The n needs a data type like a and b have.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Passing arguments to method

    And

    Java Code:
    sum = a^n + b^n;
    ^ is an exclusive OR, not exponentiation. Check out the Math.pow() method.

    Regards,
    Jim
    Last edited by jim829; 03-04-2017 at 05:00 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Passing arguments to method

    you get the error, because your method sum() declares that it returns an Integer, but there is no return statement in it.
    Suggestion: rename the method to something more unique.

  5. #5
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Passing arguments to method

    OK... Since I am not sure how to edit my initial post, let me attempt to get this into the proper format with new questions

    HEre is my class definition that works:
    Java Code:
    public class FermatCheck {
    boolean fermat;
    void fermat(){
    if (fermat == true) {
    System.out.println("Fermat was Wrong!!");
    }
    else {
    System.out.println("Fermat was right - this time!");
    } 
    }
    Now when I try to introduce new methods into this class I get errors:

    Java Code:
    public class FermatCheck {
          boolean fermat;
          void fermat(){
       if (fermat == true) {
           System.out.println("Fermat was Wrong!!");
              }
       else {
          System.out.println("Fermat was right - this time!");
        } // end of if-else     
        int sum;
        void sum(double a, double b, double n) {
           sum = Math.pow(a,n) + Math.pow(b,n);
        }
        
        int difference;
        void difference(double c, double n, int sum) {
          difference = Math.pow(c,n) - FermatCheck.sum(); 
          }  
    }
    I will only give you a few errors since they repeat:

    Compiliere C:\Users\Mark\Desktop\FermatCheck.java mit Java-Compiler
    FermatCheck.java:20:5: error: illegal start of expression
    void sum(double a, double b, double n) {
    ^
    FermatCheck.java:20:13: error: ';' expected
    void sum(double a, double b, double n) {

    I do not understand the errors. The code is meaningless except as a vehicle to learn and be comfortable with java...

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Passing arguments to method

    You have declared a method inside another method. Check your curly braces. What your code does is this:
    Java Code:
    void fermat() {
       void sum() {
       }
       void difference() {
       }
    }
    This is due to the lack of formatting and whitespace. Make it a habit to use plenty, PLENTY whitespace. Even though my code below is only 4 lines longer, it makes it readable, and braces errors are nearly not there due to proper indenting. This is your code, corrected and whitespaced:
    Java Code:
    public class FermatCheck {
        boolean fermat;
        int sum;
        int difference;
        
        void fermat(){
            if (fermat) {
                System.out.println("Fermat was Wrong!!");
            }
            else {
                System.out.println("Fermat was right - this time!");
            }
        }
        
        void sum(double a, double b, double n) {
           sum = Math.pow(a,n) + Math.pow(b,n);
        }
        
        void difference(double c, double n, int sum) {
            difference = Math.pow(c,n) - FermatCheck.sum(); 
        }  
        
    }
    Last edited by SurfMan; 03-06-2017 at 11:10 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Passing arguments to method

    You might also notice from SurfMan's version that this has changed:
    Java Code:
    if (fermat == true)
    to this:
    Java Code:
    if (fermat)
    That is because 'fermat' is already a boolean, so there is no need for the '==' comparison. You can just use the variable directly.
    And if you want 'not fermat' you can just do '!fermat'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Passing arguments to method

    Quote Originally Posted by Tolls View Post
    You might also notice from SurfMan's version that this has changed:
    Java Code:
    if (fermat == true)
    to this:
    Java Code:
    if (fermat)
    Yeah, I forgot to mention that. Thanks for the catch.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Passing arguments to method

    Thank you Tolls and SurfMan. I appreciate the patience and the tips...
    Java Code:
    public class Gratitude {
    
         boolean grateful;
              if (grateful) {
                   System.out.println("Thanks very much!");
              }
    
    }
    Tolls likes this.

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Passing arguments to method

    Quote Originally Posted by TeacherZ View Post
    Thank you Tolls and SurfMan. I appreciate the patience and the tips...
    Java Code:
    public class Gratitude {
    
         boolean grateful;
              if (grateful) {
                   System.out.println("Thanks very much!");
              }
    
    }
    Two observations:
    - Variable "grateful" is uninitialised, which means it's still false.
    - You can't have an if-statement in your class outside a method.

    I'm just teasing you ;) I like your attempt. You're welcome anytime.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Use of passing arguments in main method
    By REICHKONIG in forum New To Java
    Replies: 1
    Last Post: 01-01-2013, 04:50 PM
  2. passing arguments in ActionListener class
    By sandeshyadav in forum AWT / Swing
    Replies: 4
    Last Post: 02-19-2012, 06:32 PM
  3. passing arguments to other methods
    By popeyito18 in forum New To Java
    Replies: 2
    Last Post: 07-02-2011, 11:01 PM
  4. Problem passing arrayed arguments to a method
    By vcl in forum New To Java
    Replies: 1
    Last Post: 06-27-2011, 07:04 PM
  5. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-07-2010, 11:30 PM

Posting Permissions

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