Question

section Four (20 marks) Assume methods to be defined from Question 1 to Question 3 in this section are public class ArrayMeth
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
 *  Java program to switch two arrays
 */

class Main
{
  private static void switchTwoArrays(int[] first, int[] second)
  {
    int temp;

    for (int i = 0; i < first.length; i++)
    {
      temp = first[i];
      first[i] = second[i];
      second[i] = temp;
    }

  }

  public static void printArray(int[] arr)
  {
    for (int i : arr)
    {
      System.out.print(i + " ");
    }
  }

  public static void main(String[] args)
  {
    int[] arr1 = {1,2,3};
    int[] arr2 = {4,5,6};

    System.out.print("Array 1: ");
    printArray(arr1);
    System.out.println();

    System.out.print("Array 2: ");
    printArray(arr2);
    System.out.println();

    switchTwoArrays(arr1, arr2);

    System.out.println("After Switching");
    
    System.out.print("Array 1: ");
    printArray(arr1);
    System.out.println();

    System.out.print("Array 2: ");
    printArray(arr2);
    System.out.println();
  }
}
/*  Program ends here */

Cimtusharsharma/OnerlookedSeashe11Modes share run + new repl myrepls languages 丝talk intusharshar No description gome jam Mai

Note: The method is included in the class. Please test it with appropriate cases, the provided test case seems wrong. Drop comments for queries.

Add a comment
Know the answer?
Add Answer to:
section Four (20 marks) Assume methods to be defined from Question 1 to Question 3 in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write array methods that carry out the following tasks for an array of integers by completing...

    Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • C++ Arrays & Methods

     #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the same order. Otherwise, it...

  • Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should...

    Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should loop the array adding the values and return the summation (use 4, 1, 4, 6, 10, 15, 32, 79, 18 as the values in the array) 1 accepts 2 integer parameters; this method should add them and return the value 1 accepts 3 integer parameters; this method should add them all and return the value In main, define the parameters above (you may use...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • Question 10 15 pts In this question, you are asked to implement a recursive function that...

    Question 10 15 pts In this question, you are asked to implement a recursive function that checks whether two increasingly sorted arrays of integers are disjoint or not (two arrays are disjoint iff they share no common elements). Your function needs to follow the following signature: int is Disjoint (int" sorted 1, int size 1, int* sorted 2, int size2) Here is the description of the input and output parameters of is Disjoint: • int" sorted 1: pointer to the...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • in java Part 1 In this section, we relook at the main method by examining passing...

    in java Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags when running programs. On command line, for example, we may do “java MyProgram -a -v". Depending on options, the program may do things differently. For example, "a" may do all things while "-v" display messages verbosely. You can provide options in Eclipse instead of command line: "Run ... Run Configurations ... Arguments". Create a Java class (Main.java)....

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT