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

Thread: Beginner Java question with Methods

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Java question with Methods

    I'm doing a first year computer science course (I'm totally new to this) and we're currently on methods, I have to create two methods and a main program to run them.

    I have the two methods created as posted below (I believe they are fine but feel free to point out if theyre not!) but I cannot seem to understand how to create a program to "test" them? since I can't reference any of the variables I used in the method outside of it! notes / my textbook don't seem to help

    public static double smallest(double x, double y, double z) {
    		double smallest = 0;
    		double x;
    		double y;
    		double z;
    		if (x < y && x < z) {
    			x = smallest;
    		}
    		else if (y < x && y < x) {
    			y = smallest;
    		}
    		else if (z < y && z < x) {
    			z = smallest;
                    return smallest;
     
    }
     
     
     
    	}


    public static double Average(double x, double y, double z) {
    	double sum = x + y + z;
    	double average = sum / 3;
    return average;

    Any help on how to call the methods in I guess a main method to run them would be great!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Beginner Java question with Methods

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic other useful info for newcomers.

    Do the methods above and the main program to run have to be in separate classes?

    If no (the easier of the two): Put the two methods above and a main() method in one class. Call the methods above from the main() method. Pass values to the methods as needed. Do something with the values returned by the methods.

    If yes: Put the methods above in a class by themselves. Remove 'static' from their signatures. Create another class, call it anything you want, maybe "TestMethods," and write a main method that creates an instance of the first class and then calls the methods on that instance. From this point, it's very much like the "no" option above.

    You'll have problems with either approach, errors, and things to learn as you go, so come back with your progress and new questions as they occur.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java question with Methods

    Well first of all you need to start with the basics. Every programm needs start off with a class and then within every class is where you put your methods like a Main method( where you run/test your program or call other methods). It should look something like thins

    public class HelpWithMethods
    {
            public static void main(String [] args)
            {
     
            }
    }

    BODY.... Here is where you will declare your variables, call your methods, and so on
    public static void main(String [] args)
            {
     
            }


    outside the main method but still inside the class you can add other methods, like the ones you created.. so for example...
    public class HelpWithMethods
    {
            public static void main(String [] args)
            {
                // GB: Code deleted
            }
     
            public static double Average(double x, double y, double z) 
            {
                // GB: do average stuff
             }
    }

    This should yeild you a calculation. Just insert what you need and see if it works for you.
    Last edited by GregBrannon; February 25th, 2014 at 05:13 PM. Reason: Specifics deleted.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Beginner Java question with Methods

    @fireeyes2l2: Welcome to the forum! Please read and respect the Forum's policy on spoon feeding.

    You're on the right track, just back off the details a bit. Lead in a positive direction that allows the OP to discover/learn along the way.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java question with Methods

    After I create the methods properly what is it I put in exactly to call/execute them in the main program?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Beginner Java question with Methods

    Write the code, and then come back to post your code with the questions you have about what to do next. For the parts you don't know how to do, either try something, or just leave a comment that says what you'd like to do.

    This material is covered in your textbook and/or class notes. It appears you're not looking very hard.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java question with Methods

    YOu could also go to YouTube and watch vids on your topic.. this case "Calling methods" and "executing programs"...

    Funny about the spoon feeding policy. I guess it won't happen again. I do a little bit of it, to help start a path for someone. I was not going to continue replaying with the correct code.

    And what GregBrannon is stating is true.. Everything is in your textbook or notes or you, since you obvisouly have the internet, you can search practically everywhere. Try out code that you have so far. Use some logic and trial and error.. Come back later with your code if you are still having problems. But honestly, looking forward in your notes/book is not a bad thing.

  8. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java question with Methods

    Thanks for the info guys, sorry if its too many questions, I'm just really new to this, to be honest I never thought of Youtube!

    Ill give it a go.

Similar Threads

  1. Simple question pls help beginner learning java
    By DroidJava in forum Java Theory & Questions
    Replies: 2
    Last Post: November 12th, 2013, 01:04 AM
  2. Affecting Point objects with methods - a beginner's failed attempt
    By thatolkevin in forum Object Oriented Programming
    Replies: 10
    Last Post: July 16th, 2013, 08:50 AM
  3. Java Array Merge Input Beginner Question
    By grumpy_int05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 27th, 2013, 04:03 PM
  4. Quick, beginner-level Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2011, 01:06 PM
  5. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM