Hey! I am new to java programing and need some help with some homework, here are the requirements:
Right now I am working on just setting up my arrays but it is not working. I read the size of the array from the user and then tell the user to enter the elements of the array. I am very confused and frustrated!! Please help if you can, thanks for all the advice in advance.
The assignment
Write a program that will provide methods for manipulating arrays.
Write static methods within your main program class to do the following.
Assume A, B are arrays of int.
ReadArray (A) - reads int values from std input into the array A.
PrintArray (A) - printlns the elements of A to std output 8 per line.
int Sum (A) - returns the sum of the elements in array A.
int[] AddArrays (A, B) - returns a new array where each element is the sum
of the corresponding elements in A and B.
int DotProduct (A, B) - returns the dot product a1*b1 + a2*b2 + ...
The methods should handle arrays of any length. AddArray and DotProduct
should handle two arrays of different length by treating the shorter array as
if it were padded with zeros out to the length of the longer array.
The main method should prompt the user to enter two arrays and their values
(prompt for number of elements, create array, call Readarray). The program
should then print each array and its sum, add the two arrays, print the sum
of this new array, and print the dot product of the two original arrays.
import java.util.Scanner;
import java.io.*;
public class AKL
{
//---------------------------------------------------------------------------
// Method main
//
//---------------------------------------------------------------------------
public static void main(String[] args) throws IOException
{
int num =0;
int[] myInts1;
int[] myInts2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the size of ARRAY 1 : ");
num = scan.nextInt();
myInts1 = new int[num];
System.out.println("Please enter the size of ARRAY 2 : ");
int num1 = scan.nextInt();
myInts2 = new int[num1];
readarray(myInts1);
} // ends main
public static void readarray(int []array)
{
Scanner scan = new Scanner(System.in);
for (int index = 0; index < array.length; index ++)
{
System.out.println("Please enter the elements for ARRAY1 : ");
array[index] = scan.nextInt();
}
}
}//end class