Results 1 to 10 of 10

Thread: Calling methods

  1. #1
    antnas is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Calling methods

    Hello everyone! As you can tell by the question I'm about to ask I am extremely new to programming in general(1 week in).
    I dont really know how to call a method with two parameters in the main method. This is the little bit of code I was given.

    public class maker
    {
    public static void main(String[] args)
    {
    //how do I call the method below here so it does not give me errors?
    }
    public static void printIntArray(int[] myParamVar)
    {
    int LOCATION=0;
    while(LOCATION<myParamVar.length)
    {
    System.out.print("In*LOCATION*"*+*LOCATION*+*"**") ;
    System.out.print("Rosie*has*written*");
    System.out.println(*myParamVar[LOCATION]);
    }
    return;
    //Make the method return AFTER printing the entire array.
    }

  2. #2
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Calling methods

    try to use code block. for your code

    if you wanted to call printIntArray(int[] myParamVar) you would have to pass a int[] to it so example would be

    Java Code:
    public static void main(String[] args){
         int[] myArray = new int[5];
         printIntArray(myArray);
    }
    if this doesn't answer your question let me know ill go into more detail.

    If a mode is reading this is there a escape key so I can show someone how to use code blocks?

  3. #3
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Calling methods

    Java Code:
    public static void main(String[] args)
    	{
    		
    		int testInt = 6;
    		
    		
    		
    		
    		method1();
    		
    		int int1 = method2();
    		
    		method3(testInt);
    		
    		int int2 = method4(testInt);
    		
    	}
    	
    	public static void method1(){
    	}
    	
    	public static int method2(){
    		return 894;
    	}
    	
    	public static void method3(int giveInt){
    		
    	}
    	
    	public static int method4(int giveInt){
    		return 5 + giveInt;
    	}
    look at the following code.

    method1 = doesn't really really need anything special it can be called with method1();
    method2 = is called in a way where it gives you something back so you can take what it give back and use it
    method3 = wants something from you so you have to put it in the () If you don't give it what it wants then its not going to work
    method4 = method that wants something and gives you something back

    hope that helps

  4. #4
    antnas is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Calling methods

    Wow! Thanks for the quick reply. I got my program compiled now and it runs I just have to fix the infinite loop.
    Is this better for the code reading?
    Java Code:
    public class Maker{
      public static void main(String[] args){
        int[] myArray = new int[5];
        printIntArray(myArray);
      }
      public static void printIntArray(int[] myParamVar)
      {
        int LOCATION = 0;
        while(LOCATION<myParamVar.length)
        {
          System.out.print("In LOCATION " + LOCATION + " ");
          System.out.print("Rosie has written ");
          System.out.println(myParamVar[LOCATION]);
        }
        return;
      }
    }
    Its not that I want some random person to do my work for me, but looking at this code with no explanation on what anything really means makes it like im trying to read Russian with no Russian background.
    I have no clue where to start.

  5. #5
    antnas is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Calling methods

    Quote Originally Posted by killutch View Post
    Java Code:
    public static void main(String[] args)
    	{
    		
    		int testInt = 6;
    		
    		
    		
    		
    		method1();
    		
    		int int1 = method2();
    		
    		method3(testInt);
    		
    		int int2 = method4(testInt);
    		
    	}
    	
    	public static void method1(){
    	}
    	
    	public static int method2(){
    		return 894;
    	}
    	
    	public static void method3(int giveInt){
    		
    	}
    	
    	public static int method4(int giveInt){
    		return 5 + giveInt;
    	}
    look at the following code.

    method1 = doesn't really really need anything special it can be called with method1();
    method2 = is called in a way where it gives you something back so you can take what it give back and use it
    method3 = wants something from you so you have to put it in the () If you don't give it what it wants then its not going to work
    method4 = method that wants something and gives you something back

    hope that helps
    Where does that return 894; code come from?

  6. #6
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Calling methods

    Quote Originally Posted by antnas View Post
    Where does that return 894; code come from?
    you could return any number you wanted. or you could do stuff inside the method to come up with a number. all its doing is returning a integer number.

  7. #7
    antnas is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Calling methods

    Fixed infinite loop and gave random number values... So confusing for me but im kind of getting it.
    Java Code:
    public class Maker{
      public static void main(String[] args){
        int[] myArray = new int[5];
        printIntArray(myArray);
      }
      public static void printIntArray(int[] myParamVar)
      {
        int LOCATION = 0;
        while(LOCATION<myParamVar.length)
        {
          System.out.print("In LOCATION " + LOCATION + " ");
          System.out.print("Rosie has written ");
          System.out.println(( ( int) ( 4.9999 * Math.random() ) ) );
          LOCATION = LOCATION + 1;
        }
        return;
      }
    }
    This code prints a random number in the locations like this.
    > run Maker
    In LOCATION 0 Rosie has written 3
    In LOCATION 1 Rosie has written 4
    In LOCATION 2 Rosie has written 0
    In LOCATION 3 Rosie has written 2
    In LOCATION 4 Rosie has written 0
    >
    Which is working and now I need to make code so that it will find the smallest integer in the array and print that integer and what location its in.
    So if I wrote that code it would be like Smallest integer 0 is in location 2,4... or so I assume.
    Anyone have any idea on what I would need to write for that to happen?
    Last edited by antnas; 10-20-2012 at 11:33 PM.

  8. #8
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Calling methods

    you could use Arrays.sort(myArray); and that would put the smallest at index 0

    Java Code:
    int[] array = {8,5,6,9,8,2,1,1};
    		
    		Arrays.sort(array);
    		
    		
    		for(int i=0;i<array.length;i++){
    			if(array[i]==array[0]){
    				System.out.print(i + " ");
    			}
    		}
    Last edited by killutch; 10-21-2012 at 12:21 AM.

  9. #9
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Calling methods

    or something like this

    Java Code:
    public static void main(String[] args)
    	{
    		
    		int[] array = {8,5,6,9,8,2,1,1};
    		
    		int lowNumb = lowNumber(array);
    		
    		for(int i=0;i<array.length;i++){
    			if(array[i]==lowNumb){
    				System.out.print(i + " ");
    			}
    		}
    		
    	}
    	
    	
    	//this will find the lowest number in the array
    	private static int lowNumber(int[] array){
    		int ret = array[0];
    		
    		for(int i=0;i<array.length;i++){
    			if(array[i]<ret){
    				ret = array[i];
    			}
    		}
    		
    		return ret;
    	}

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Calling methods

    Quote Originally Posted by antnas View Post
    looking at this code with no explanation on what anything really means makes it like im trying to read Russian with no Russian background.
    I have no clue where to start.
    Maybe try going forwards instead of starting in the middle and working backwards?
    Trail: Learning the Java Language (The Java™ Tutorials)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Calling on other class methods?
    By carrot_spy in forum New To Java
    Replies: 1
    Last Post: 10-06-2012, 10:28 PM
  2. Calling methods
    By Alkor in forum New To Java
    Replies: 15
    Last Post: 02-25-2012, 02:33 PM
  3. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 09:12 PM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 AM

Posting Permissions

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