Question

In: Computer Science

(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...

(JAVA)

InvertArrangement

+invert(int[] a) : int []

+print(int[] a) : void

Example 1: the invert method receives the following arrangement: [1,2,3,4,5]

The invert method returns the array [5,4,3,2,1]


Example 2: the print method receives the following array: [5,4,3,2,1]

The print method prints: 5,4,3,2,1 (data separated by commas).

TIP: for the print method use System.out.print () without the "ln".

Solutions

Expert Solution

Example 1:

public static int[] invert(int[] a){
int l=a.length;
int j=0;
int n[]=new int[l];//create new array
for(int i=l-1;i>=0;i--){
n[j]=a[i];//copy elements from last to new array
j++;
}
return n;//return array
}

Example 2:

public static void print(int[] a){
int i;
for(i=0;i<a.length-1;i++){
System.out.print(a[i]+",");//print each element
}
System.out.print(a[i]);
}

Full Code:

public class Main
{
public static int[] invert(int[] a){
int l=a.length;
int j=0;
int n[]=new int[l];//create new array
for(int i=l-1;i>=0;i--){
n[j]=a[i];//copy elements from last to new array
j++;
}
return n;//return array
}
public static void print(int[] a){
int i;
for(i=0;i<a.length-1;i++){
System.out.print(a[i]+",");//print each element
}
System.out.print(a[i]);
}
   public static void main(String[] args) {
       int a[]={1,2,3,4,5};
       a=invert(a);//call function and invert elements
       print(a);
   }
}

Screenshots:

Screenshots:

The screenshots are attached below for reference.

Please follow them for output.

Please upvote my answer. Thank you.


Related Solutions

JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int x, int y) { x = 1; y = 3; } For each of the following parameter-passing methods, saw what the final values in the array A would be, after the call to f. (There may be more than one correct answer.) a. By value. b. By reference. c. By value-result.
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
public void printNumbers(int low, int high) { // using a for loop, print all numbers from...
public void printNumbers(int low, int high) { // using a for loop, print all numbers from low to high } public int sumOfNumbers(int n) { // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 } public void printMultiplicationTable(int num) { // using a for loop, print the multiplication table of num (up to first 10!) // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25,...
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers from low to high for(int i = low; i <= high; i++){ System.out.println(i); } } public int sumOfNumbers(int n){ // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 int sum = 0; for(int i = 1; i <= n; i++){ sum += i; } return sum; } public void...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT