How do I Write 3 different method Types.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlock784
    New Member
    • Feb 2010
    • 5

    How do I Write 3 different method Types.

    I don't know how to start this program and I don't know what I am doing. The purpose of this assignment is to practice writing the three different types of methods. These three methods are very simple in their purpose.
    Here are the three methods, their names, and their purpose:

    noParametersNoR eturn - This method will simply display the words "I am here"
    twoParametersNo Return - This method will have two parameters which are both integers. The method will multiply the two parameters and display the result.
    twoParametersOn eReturn - This method will have two parameters which are both integers. It will add the two integers together and return the result of that addition.

    In your main method you will call each method. Be sure that you display the results of your call to the third method.

    This is what I have so far.

    Code:
    /** This program is a practice to write three different types of methods.
    */
    
    public class MethodTypes
    {
      public static void main(String[] args)
      {
      public static void noParametersNoReturn; 
      {
         System.out.println("I am here");
      }
        
    /** The twoParametersNoReturn will have two parameters which are both intergers. It will multiply the two parameters and 
    display the results.
    */
      
      public static void twoParametersNoReturn;
      {
    I am new to this stuff and I really need help.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Please use code tags, [ code ] [ / code ] (without spaces) in the future.
    You'd want your main function to do the calling, which is simply passing the method name, enclosing the parameters in parentheses. If there are no arguments, you still need the parentheses.
    Also, since one of your functions returns a value, you'd want to store that value, say in int result.
    Code:
    public static void main(String[] args)
    {
        int i = 0;
        int j = 0;
        int result;
    
        MethodOne();   
        MethodTwo(i, j);
        result = MethodThree(i, j);
    }
    Then you can write your functions. Note, that you're missing parentheses in your function declarations. Method declarations are usually like:
    [public/private] [static?] [return type] [MethodName] ([method parameters])

    return type: void, if nothing is returned. Otherwise could be: int, boolean, double, char, etc..
    Method parameters: [type][name] seperated by commas. eg:
    int x, int y
    eg:
    Code:
    public static void MethodOne(){
    //function code goes here
    }
    
    public static void MethodTwo(int x, int y){
    //more code
    }
    Try this out first, and let us know if you run into problems.

    Comment

    • jlock784
      New Member
      • Feb 2010
      • 5

      #3
      Help

      Can any one explain to me further how to do this assignment. I am new to programming and I really need help. I don't understand any of this and I have read the assigned chapter twice, and I am meeting with a tutor on Friday to also help me.The purpose of this assignment is to practice writing the three different types of methods. These three methods are very simple in their purpose.
      Here are the three methods, their names, and their purpose:

      noParametersNoR eturn - This method will simply display the words "I am here"
      twoParametersNo Return - This method will have two parameters which are both integers. The method will multiply the two parameters and display the result.
      twoParametersOn eReturn - This method will have two parameters which are both integers. It will add the two integers together and return the result of that addition.

      In your main method you will call each method. Be sure that you display the results of your call to the third method.

      This is what I have so far.
      Code:
      public class MethodTypes
      {
        public static void main(String[] args)
        {
          int i = 0;
          int j = 0;
         int results;
      	 
      	 MethodOne();
      	 MethodTwo(i,j);
      	 result = MethodThree (i,j);
        }
      }
      Last edited by Frinavale; Mar 18 '10, 08:23 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Jlock784, did you try what Jkmyoung has suggested?

        This seems pretty straight forward, just create the three methods and then call them in your main method.

        It looks like you're on the right path here...you are calling the methods in the main method.
        But I can't see where you've implemented the actual methods that you're calling them.

        Remember that you cannot implement a method inside another method. This means that your program code is going to have to have something similar to the following layout:

        Code:
        public class MethodTypes
        {
          public static void main(String[] args)
          {
            int i = 0;
            int j = 0;
            int results;
         
             MethodOne();
             MethodTwo(i,j);
             result = MethodThree (i,j);
          }
          public static void MethodOne()
          {
          }
          public static void MethodTwo(int i, int j)
          {
          }
          public static int MethodThree(int i, int j)
          {
          }
        }
        Notice how the methods: MethodOne, MethodTwo, and MethodThree are all outside of the main method but are still within the MethodTypes class.
        -Frinny

        Comment

        Working...