# Need hmwk help with Arrays in jgrasp

• 09-14-2010, 12:20 AM
cmps280CD
Need hmwk help with Arrays in jgrasp
heres what I have so far, am i on the right track, here are my instructions and my code so far. Please help

Make a class called HW1.java. In this class, write the following:
A method called doubleArray that has an int array as an input parameter and returns an int array in which each element has been doubled. In the main method, call this doubleArray method with input {2,5,8}. Print out the values of the array returned by the method. (This should be 4, 10, 16.)
A method called max that has an int array as an input parameter and returns an int representing the largest value in the array. Test this method from the main method for the values {20,5,15,40}. Print the result.

public class HW1Makeup{

public static void main(String[] args){
int[] a = {2,5,8};
int[] b = {4,10,16};
print(b);

}

public static int print(int [] b){
for (int i = 0; i < a.length; i++){
System.out.println(b);

}
}
}
• 09-14-2010, 12:44 AM
curmudgeon
You don't declare the array b as you're doing but instead have b be the result of the doubleArray Method. e.g.,
Code:

`int[] b = doubleArray(a);`
What you should be doing first and foremost is trying to create the method doubleArray.
• 09-14-2010, 01:19 AM
Zack
It's also worth noting that this method is invalid:
Code:

```public static int print(int [] b){ for (int i = 0; i < a.length; i++){   System.out.println(b); }```
a is never declared as a variable in this scope. Furthermore, your loop should not be printing b (as you will just get some weird output like [I@19c26f5) but instead print out the elements of b.
• 09-14-2010, 01:33 AM
cmps280CD
This is wat I have now but its not compiling

public class HW1Makeup{

public static void doubleArray(int[]){
int[] a = {2,5,8};
int[] b = doubleArray(a);
print(b);

}
}
• 09-14-2010, 01:36 AM
Onra
First of all you should give the variable in the function argument a name and not only a type.

Also this: int[] b = doubleArray(a); cannot work, because your function does not return an int array.
• 09-14-2010, 01:39 AM
curmudgeon
You really need to study your notes and read the chapter in your text on how to create methods before trying to create code as your method is totally borked. Guessing never works when programming.

Look at examples of how the method looks, the return type (as noted above) the parameters (int[] wont work), and then try to build your method piece by piece using the knowledge you've gained. Also compile frequently and don't add any new code until your compile errors are fixed.
• 09-14-2010, 02:22 AM
cmps280CD
ok now I have this code:

public class HW1Makeup{

public static void main(String[] args){
double[] mylist = {2,5,8};

}
}

I need to double the elements of this array..and print out the values returned by the array...is this good so far, it compiles ok, im using the book now
• 09-14-2010, 02:31 AM
Onra
Then define the signature of your doubleArray function next(public static...).
• 09-14-2010, 02:44 AM
curmudgeon
Quote:

Originally Posted by Onra
Then define the signature of your doubleArray function next(public static...).

public static? It looks like you're placing that in the method parameter list

But by all means now try to do your method, but again a little bit at a time. I'll give you the signature....

Code:

```public static int[] doubleArray(int[] originalArray) {   // your code goes here. }```
• 09-14-2010, 03:23 AM
Onra
Ok, now think of an algorithm that takes your original array and writes all values*2 into a new array.