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

Thread: How to make calls to methods

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to make calls to methods

    New here, I'm a compsci student taking a java programming class. Pretty basic suff but I'm getting caught here and there wrapping my head around some stuff.

    My question right now invoves making a method that takes a single int parameter and then prints all of the positive even perfect squares less than n.
    This is what I have so far:

    public class evenSquares {
     
      public void evenSquares(int n){
        for(int j = 2; j*j <= n; j += 2)
          System.out.println(j*j);
      }
    }

    public class evenSquaresDriver {
      public static void main(String[] args){
     
        evenSquares testsquare = new evenSquares(1000);
      }
    }

    This is giving me an error that "The constructor evenSquares(int) is undefined"


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to make calls to methods

    Ok, in your evenSquares class, you have no constructor. What you do have is a method. So when you say new evenSquares(1000);, it is looking for a constructor that accepts an int.

    So, there are a few ways to fix this with what you already have, depending on what you want to do:
    If you want to create a constructor for your evenSquares class:
    public class evenSquares {
     
      public evenSquares(int n){     //NOTICE: I took out the return type to make it a constructor
        for(int j = 2; j*j <= n; j += 2)
          System.out.println(j*j);
      }
    }

    OR

    If you want to have an evenSquares class, with an evenSquares method (poor programming habit, but it will work):
    public class evenSquaresDriver {
      public static void main(String[] args){
     
        evenSquares testsquare = new evenSquares();
        testsquare.evenSquares(1000);     //Using the empty constructor, and then calling the evenSquares(int) method
      }
    }

    You seem to not fully understand the difference between a method and a constructor. I suggest you do a little bit of reading about them to better understand how it works.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    ksahakian21 (April 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to make calls to methods

    Another question with similiar idea.

    The method stringRoot is passed a string of digits as a paramater and returns the square root of the number represented by those digits.

      public double stringRoot(String str){
        double k = Integer.parseInt(str);
        return Math.sqrt(k);
      }

    stringRoot testDouble = new stringRoot();
    testDouble.stringRoot("1600");

    It's telling me "stringRoot cannot be resolved to a type"
    Correct me if I'm wronge but this is a method correct? I also just threw the codes into my evenSquares and evenSquaresDriver classes

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to make calls to methods

    What class is the stringRoot(...) method in?

    You are trying to create a new Object, of type stringRoot, and calling the new Object's stringRoot(...) method, which is clearly not what you are wanting to do.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #5
    Junior Member lunix's Avatar
    Join Date
    Apr 2012
    Location
    England
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to make calls to methods

    ermmm, just do classname.stringRoot("1600")

    don't think you need to make an object to use a classes method?
    Feels good man

Similar Threads

  1. drawImage() causes endless calls to paintComponent()
    By repaint_forever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 10th, 2011, 12:15 PM
  2. Identifying invalid calls
    By joshft91 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 26th, 2011, 05:38 PM
  3. Case statements to Method calls???
    By Java Neil in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 06:17 AM
  4. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 08:18 AM
  5. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 04:37 AM