Member Avatar

Can anyone help me with my homework?

Create a Java class named ArrayTest with a method called arrayCombiner that creates three arrays of size 10. The first array should contain the values 1, 3, 5, 7, 9, 11, 13, 15, 17, and 19. The second array should contain the values 2, 4, 6, 8, 10, 12, 14, 16, 18, and 20. Using loops, populate the third array with the sum of the value of the number at the same index location from the other two arrays. For example, the first element will be 3 (1+2), the second will be 7 (3 + 4) etc. Write another loop that loops through the third array and prints out its contents. Use the following main method in ArrayTest:

public static main(String[] args)
{
	arraySummer();
}

Recommended Answers

yes we can help
What have you coded so far?
What part are you having problems with?

Jump to Post

We just started arrays and I have no idea how to do this problem. Could you post some code so I can get started. I really do no understand arrays. Thanks.

check here:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html here:

Jump to Post

in your main method, you don't have an oddArray or evenArray, neither do you have one on class scope, so how do you think you can use them in the main method?

also, why do you try to create methods outside of your class?
and , yet another thing:

Jump to Post

I am confused.

public class ArrayTest
{
public static main void(String[] args)
{
arraySummer();
}

} // end main
pubic static void arrayCombiner(passedArray)
{
int[] odd=new int[10];
int[] even=new int[10];
int[] sum=new int[10];

for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
System.out.println(sum[i+j]);
}
}

} // …
Jump to Post

might be a mistake from his part. or, create a method arrayCombiner which you call from withing arraySummer() but I doubt that's what you should do. just rename arrayCombiner to arraySummer and test it

Jump to Post

All 38 Replies

yes we can help
What have you coded so far?
What part are you having problems with?

Member Avatar

We just started arrays and I have no idea how to do this problem. Could you post some code so I can get started. I really do no understand arrays. Thanks.

Member Avatar

Can anyone help with my code?

public class ArrayTest
{
public static main void(String[] args)
{
arrayCombiner(oddArray);
arrayCombiner(evenArray);
}

}
} // end class ArrayTest
pubic static int[] arrayCombiner(passedArray)
{
int[] oddArray={1,3,5,7,9,11,13,15,17,19};
int[] evenArray={2,4,6,8,10,12,14,16,18,20};

for(int i=0; i<10; i++)
{
System.out.println(oddArray[i]);
}
return oddArray;

for(int j=0; j<10; j++)
{
System.out.println(evenArray[j]);
}
return evenArray;
} // end arrayCombiner

in your main method, you don't have an oddArray or evenArray, neither do you have one on class scope, so how do you think you can use them in the main method?

also, why do you try to create methods outside of your class?
and , yet another thing:
return oddArray;
is not depending of a condition, so this will always be executed. your compiler will have trouble with the code following this return statement, since it will never be reached.

1. put those methods in your class ( by moving the closing } of your class to the end)
2. since your assignment says: arraySummer(); you're not supposed to have parameters. remove them.
3. remove the return statements, your method is not supposed to return anything, so change the returntype in the signature to void
4. what you need is:
int[] odd = new int[10];
int[] even = new int[10];
int[] sum = new int[10];
then:
one for loop to fill odd, and each time you fill an element of odd, add that value to the value at the same index in sum
one for loop to fill even, and each time you fill an element of even, add that value to the value at the same index in sum

Member Avatar

I am confused.

public class ArrayTest
{
public static main void(String[] args)
{
arraySummer();
}

} // end main
pubic static void arrayCombiner(passedArray)
{
int[] odd=new int[10];
int[] even=new int[10];
int[] sum=new int[10];

for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
System.out.println(sum[i+j]);
}
}

} // end arrayCombiner
} // end class ArrayTest

I am confused.

public class ArrayTest
{
public static main void(String[] args)
{
arraySummer();
}

} // end main
pubic static void arrayCombiner(passedArray)
{
int[] odd=new int[10];
int[] even=new int[10];
int[] sum=new int[10];

for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
System.out.println(sum[i+j]);
}
}

} // end arrayCombiner
} // end class ArrayTest

what are you confused about... BTW if it wont compile its most likely because you used 'pubic' instead of 'public' as an access modifier for your method and your curly braces are a bit other wise, you're also missing the data type for your methods argument

Member Avatar

Please help.

public class ArrayTest {
public static void main(String[] args) {
arraySummer();
} // end main
public static void arrayCombiner(int[] passedArray){
int[] odd=new int[10];
int[] even=new int[10];
int[] sum=new int[10];

for(int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
System.out.println(sum[i+j]);
}
}
} // end arrayCombiner
} // end class ArrayTest

Also, I think arraySummer() should be arrayCombiner(). I do not understand why my instructor would ask to create arrayCombiner and then call arraySummer.

might be a mistake from his part. or, create a method arrayCombiner which you call from withing arraySummer() but I doubt that's what you should do. just rename arrayCombiner to arraySummer and test it

Member Avatar

It does not compile.

what error message do you get?

Member Avatar

arrayCombiner(int[]) in ArrayTest cannot be applied to () arrayCombiner();

public class ArrayTest {
public static void main(String[] args) {
arrayCombiner();
} // end main
public static void arrayCombiner(int[] passedArray){
int[] odd=new int[10];
int[] even=new int[10];
int[] sum=new int[10];

for(int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
System.out.println(sum[i+j]);
}
 }
  } // end arrayCombiner
   } // end class ArrayTest

Changing it to arrayCombiner() does not help.

replace
public static void arrayCombiner(int[] passedArray)
with
public static void arrayCombiner()

also, remember, you don't have any values in your arrays, except for the default zero values.

Member Avatar

Does not work. I get this:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at ArrayTest.arrayCombiner(ArrayTest.java:12)
at ArrayTest.main(ArrayTest.java:3)

for(int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
System.out.println(sum[i+j]);

what do you think this will do when i = 2 and j = 9?
you don't want to do sum i + j
you only need one array in the first place. I've explained it a bit earlier, but I'll try it again

int count = 0;
int i = 1;
while ( i < 10 ){
  odd[count] = i;
  even[count] = i+1;
  sum[count] = i + (i+1);
  i += 2;
}
Member Avatar

Like this?

public class ArrayTest {
public static void main(String[] args) {
arrayCombiner();
} // end main
public static void arrayCombiner() {
int[] odd=new int[10];
int[] even=new int[10];
int[] sum=new int[10];

int count=0;
int i=1;
while(i<10) {
odd[count]=i;
even[count]=i+1;
sum[count]=i+(i+1);
i+=2;
System.out.printf("%d ",sum[count]);
}
 } // end arrayCombiner
  } // end class ArrayTest

hmmmm ... make it

while ( count < 10 )

plus, I would put the printing in a separate loop after the while.

for ( int sumCount: sum )
System.out.println("the sum = " + sumCount);

if you want a good exercise: explain here in the thread why you need
while ( count < 10 )
instead of
while ( i < 10 )

Member Avatar

while(count<10 ) does not work.

I get an infinite loop.

you're getting an infinite loop, because you're not augmenting the value of count

add

count += 1;

as last line of your while loop.
now, check while ( i < 10 ) against while ( count < 10 ) ..
do you know why you need count there?

Member Avatar

It does not work. I really do not understand. Please help.

show your current code

Member Avatar
public class ArrayTest {
public static void main(String[] args) {
arrayCombiner();
} // end main
public static void arrayCombiner() {
int[] odd=new int[10];
int[] even=new int[10];
int[] sum=new int[10];

int count=0;
int i=1;
while(count<10) {
odd[count]=i;
even[count]=i+1;
sum[count]=i+(i+1);
count+=1;
}
{
for(int sumCount: sum)
System.out.println(sumCount);
}
 } // end arrayCombiner
  } // end class ArrayTest
Member Avatar

Can anyone help?

the problem is in your while loop you don't change the value of i
try to iterate i

Member Avatar

It does not work even if I add i++;

where exactly did you put it?

Also I suggest to change your for loop to print the values
something like this

for(int counter = 0;counter <10; counter++){
//print out sum with counter as the index 
}

Edit: there's nothing really wrong with the for loop I was just suggesting a better way to view what's going on :)

Member Avatar

No. Nothing works.

at the while loop how about you put i++; after count+=1;

Member Avatar

Does not work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.