Hello, this is a practice assignment that we are using for an upcoming test. I a
ID: 3739729 • Letter: H
Question
Hello, this is a practice assignment that we are using for an upcoming test. I am not talented in this department but would really like to learn. Can someone please do each of these with comments so I know how you did it? Label and space for each problem so I know which is which. Thank You! JAVA!!!
Complete the following array problems. Each file you submit should contain a main() method to test your solution which is implemented as a static method.
Problem 1. Concatenate
Write a method that takes two integer array parameters and returns an array that contains all the elements from both arrays
public static int [] concat(int [] arr1, int [] arr2) ;
The concatenation of array {1,2,3} with array [5,6,7,8} would be the array [1,2,3,5,6,7,8}.
Problem 2. Larger Array
Write a method that takes two integer array parameters and returns true if the sum of the numbers in the first array is larger that the sum of the numbers in the second array.
public static boolean larger(int [] arr1, int [] arr2);
Problem 3. Double Odd
Write a method that takes an integer array parameter and replaces each odd number in the array with it's value times 2.
public void doubleOdd(int [] arr);
So this method would change {3,4,5,8,9} into {6,4,10,8,18}
Problem 4. Endsmatch
Write a method that takes an integer array as a parameter and returns true if the first and last values of the array are the same
public static boolean endsMatch(int [] arr);
So, true for {3,2,2,1,3} and false for {3,2,3,2}
Problem 5. Reverse
Write a method that takes an integer array and reverses it:
public static void reverse(int [] arr);
Reversing {1,2,3,4} changes it to {4,3,2,1}. Recall from lecture that you will need a temporary variable to hold one of the values to avoid overwriting an entry and losing the value.
Explanation / Answer
import java.util.Scanner;
class Assignment
{
public static int[] concat(int []arr1,int []arr2) // calling function with 2 array parameters
{
int[]combined = new int[arr1.length+arr2.length]; // size of combined array= size of 2 arrays
int count = 0;
for(int i = 0; i<arr1.length; i++)
{
combined[i] = arr1[i]; // inserting first array values into combined from array1
count++;
}
for(int j = 0;j<arr2.length;j++)
{
combined[count++] = arr2[j]; // inserting first array values into combined from array2
}
for(int i = 0;i<combined.length;i++)
System.out.print(combined[i]+" ");// printing combined array values
return null;
}
}
class Sample
{
public static void main(String a[])
{
Scanner s=new Scanner(System.in); // object created to scan values
System.out.println("enter size of array1");
int n=s.nextInt(); // size of array1
int arr1[]=new int[n]; // array1 is created with size n
System.out.println("enter elements into array1");
for(int i=0;i<n;i++)
{
arr1[i]=s.nextInt(); // inserting elements into array1 upto size n
}
System.out.println("enter size of array2");
int n1=s.nextInt(); // size of array2
int arr2[]=new int[n1]; // array 2 created with size n1
System.out.println("enter elements into array2");
for(int i=0;i<n1;i++)
{
arr2[i]=s.nextInt(); // inserting elements into array2
}
Assignment.concat(arr1,arr2); // calling static function using class name
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.