Results 1 to 10 of 10
Thread: Calling methods
- 10-20-2012, 08:41 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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.
}
- 10-20-2012, 08:46 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
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
if this doesn't answer your question let me know ill go into more detail.Java Code:public static void main(String[] args){ int[] myArray = new int[5]; printIntArray(myArray); }
If a mode is reading this is there a escape key so I can show someone how to use code blocks?
- 10-20-2012, 08:59 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: Calling methods
look at the following code.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; }
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
- 10-20-2012, 09:06 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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?
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.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; } }
I have no clue where to start.
- 10-20-2012, 09:09 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
- 10-20-2012, 10:06 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
- 10-20-2012, 10:25 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Calling methods
Fixed infinite loop and gave random number values... So confusing for me but im kind of getting it.
This code prints a random number in the locations like this.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; } }
> 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.
- 10-21-2012, 12:09 AM #8
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
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.
- 10-21-2012, 12:26 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
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-21-2012, 06:48 AM #10
Re: Calling methods
Maybe try going forwards instead of starting in the middle and working backwards?
Trail: Learning the Java Language (The Java™ Tutorials)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Calling on other class methods?
By carrot_spy in forum New To JavaReplies: 1Last Post: 10-06-2012, 10:28 PM -
Calling methods
By Alkor in forum New To JavaReplies: 15Last Post: 02-25-2012, 02:33 PM -
Calling for methods
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 02-27-2010, 09:12 PM -
Calling Methods
By bluegreen7hi in forum New To JavaReplies: 3Last Post: 12-17-2007, 06:22 AM -
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11-15-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks