I am stuck on this problem, I have been working on it for awhile now, and I stil
ID: 3684184 • Letter: I
Question
I am stuck on this problem, I have been working on it for awhile now, and I still dont understand how to do this at all. If anyone can write the code for me, I would be entirely grateful, I honestly am lost.
ArrayMethods.java
This program will contain 4 methods:
1) A public, static Java method called lastNumber that accepts an array of integers andreturns the last element of the array.
2) A public, static Java method called join that will accept a character array as its parameter.The method should return a String that is the result of concatenating all of the characters inthe array.
3) A public, static Java method called reverseArray that will accept an array of integer valuesand reverse the order of the array elements. The method should work with any array, regardless of the length.Do not print anything. Do not use any built-in Java “reverse” method. Do not not return avalue.
4) A main method with sufficient calls to test the lastNumber, join and reverseArray methodsthoroughly.
Explanation / Answer
ArrayMethods.java
import java.util.Arrays;
public class ArrayMethods {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {3,4,51,4,7,89,32,12,34,32};
char c[] = {'s', 'u', 'r', 'e', 's', 'h'};
int lastElement = lastNumber(a);
System.out.println("The Last Number of an Array is : "+lastElement);
String str = join(c);
System.out.println("The result of concatenating of all characters in an Array is "+str);
System.out.println("Array Elements before reverse "+Arrays.toString(a));
reverseArray (a);
System.out.println("Array Elements After reverse "+Arrays.toString(a));
}
public static int lastNumber(int a[]){
int last = a[a.length-1];
return last;
}
public static String join(char c[]){
String s = "";
for(int i=0; i<c.length; i++){
s = s + c[i];
}
return s;
}
public static void reverseArray(int a[]){
for(int i = 0; i < a.length / 2; i++)
{
int temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
}
}
Output:
The Last Number of an Array is : 32
The result of concatenating of all characters in an Array is suresh
Array Elements before reverse [3, 4, 51, 4, 7, 89, 32, 12, 34, 32]
Array Elements After reverse [32, 34, 12, 32, 89, 7, 4, 51, 4, 3]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.