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

Thread: I don't understand how to use methods

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    18
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question I don't understand how to use methods

    im studying from a book called "intro to java programming(10th edition)" it seems easy and all until i try to make a method, i just end up keeping them all in the main method.

    Is there any good videos out there that you guys know of that can help me understand the how to methods?

    Like this program (exercise 7.9):
    letting user input 10 integers and displaying the minimum.
    import java.util.*;
     
    public class Exercise_7_09 {
      public static void main(String[] args) {
     
        double[] minimum = new double[10];
        Scanner input = new Scanner(System.in);
     
        System.out.print("Enter the " + minimum.length + " values: ");
        minimum[0] = input.nextDouble();
        double min = minimum[0];
        for(int i = 1; i < minimum.length; i++) {
          minimum[i] = input.nextDouble();
          if(min > minimum[i]) {
            min = minimum[i];
          }
        }
     
      }
     
      public static double[] min(double[] array) {
        array = new double[10];
        if(min > minimum
        return array;
      }
    }

    i didn't complete it yet but you can see that i can simply place it in main. methods are suppose to help make the code a little easier but i'm confused on how to use them correctly.

    Thanks!

  2. #2
    Junior Member tonya's Avatar
    Join Date
    Feb 2018
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: I don't understand how to use methods

    The java code you have supplied looks a bit confusing with what it is trying to achieve. A simple way to learn to write code, of any type, is to write down what you are trying to achieve in pseudo code.

    Pseudo code helps to get your head around how to get to your result, it can also suggest the order of statements you need to use to get your result. Pseudo code can also help to identify methods or loops to get your result.

    The idea of using a method is to split up tasks you identify to carry out your programs goal, using methods will also stop bloat in your main method.

    You can call a method from the main method, or from any other method, and you can pass information to the called method to help with the process it needs to carry out, Depending on what your method needs to do it may return a result of the process it carries out.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: I don't understand how to use methods

    For the min method you have an array of values and you want to find the minimum of those values. So the method should take the array as an argument and return the min. The code you created to find the min should be placed in the method. You need to fix the method declaration to reflect the argument you are passing and the value type you expect to be returned.

    Regards,
    Jim

Similar Threads

  1. Returning methods to main() and other methods
    By jonmackey22 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 25th, 2014, 04:36 PM
  2. Replies: 1
    Last Post: April 6th, 2014, 02:49 AM
  3. Don't understand void methods, need help!
    By alex067 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 9th, 2012, 07:02 AM
  4. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  5. Help me understand when to use static on functions/methods
    By thenk24 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 1st, 2011, 11:42 AM