1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java class Arrays

  1. Feb 28, 2012 #1
    The problem statement, all variables and given/known data

    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.

    The attempt at a solution

    Code (Text):
    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++;
    for(int sumCount:sum)
    {
    System.out.printf("%d ",sumCount);
    }
     }
      } // end arrayCombiner
       } // end class ArrayTest
     
  2. jcsd
  3. Feb 28, 2012 #2
    Re: Arrays

    I got it.

    Code (Text):
    public class ArrayTest
    {
        public static void main(String[] args)
        {
            arrayCombiner();
        } // end main
        public static void arrayCombiner()
    {
        int[] odd=new int[10]; // create strings of length 10
        int[] even=new int[10];
        int[] sum=new int[10];
        int counter=0; // initialize counter
        int i=1; // initialize variable
        while(counter<10)
    {
        odd[counter]=i; // assign new content to strings
        even[counter]=i+1;
        sum[counter]=odd[counter]+even[counter];
        counter++;
        i+=2;
    }
        for(int total:sum)
        {
        System.out.printf("%d ",total);
        }
    } // end arrayCombiner

    } // end class ArrayTest
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java class Arrays
  1. Arrays in Java (Replies: 3)

Loading...