Question

In this assignment you will be writing multiple methods that work with arrays. These methods will...

In this assignment you will be writing multiple methods that work with arrays. These methods will perform specific functions and, in most cases, return results.

Your class should be named Grades.

Your program will have the following methods:

  1. public static void rotateElements(int[] arr)
  2. public static void rotateElements(int[] arr, int rotationCount)
  3. public static void reverseArray(int[] arr)
  4. public static void main(String[] args)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

public class Main {

public static void rotateElements(int[] arr) {

int temp;

temp = arr[0];

for (int j = 0; j < arr.length-1; j++)

{

arr[j] = arr[j+1];

}

arr[arr.length - 1] = temp;

for (int k = 0; k < arr.length; k++) {

System.out.print(arr[k] + " ");

}

System.out.println();

}

public static void leftRotatebyOne(int arr[], int n)

{

int temp = arr[0], i;

for (i = 0; i < n - 1; i++)

arr[i] = arr[i + 1];

arr[i] = temp;

}

public static void rotateElements(int[] arr, int rotationCount) {

for (int i = 0; i < rotationCount; i++)

leftRotatebyOne(arr, arr.length);

for (int k = 0; k < arr.length; k++) {

System.out.print(arr[k] + " ");

}

System.out.println();

}

public static void reverseArray(int[] arr) {

int[] b = new int[arr.length];

int j = arr.length;

for (int i = 0; i < arr.length; i++) {

b[j - 1] = arr[i];

j = j - 1;

}

/*printing the reversed array*/

for (int k = 0; k < arr.length; k++) {

System.out.print(b[k] + " ");

}

}

public static void main(String[] args) {

int arr1[] = {1, 2, 3, 4, 5};

rotateElements(arr1);

int arr2[] = {1, 2, 3, 4, 5};

rotateElements(arr2, 4);

int arr3[] = {1, 2, 3, 4, 5};

reverseArray(arr3);

}

}

Add a comment
Know the answer?
Add Answer to:
In this assignment you will be writing multiple methods that work with arrays. These methods will...
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
  • In this assignment you will be writing multiple methods that work with arrays. These methods will...

    In this assignment you will be writing multiple methods that work with arrays. These methods will perform specific functions and, in most cases, return results. Your class should be named Grades. Your program will have the following methods: public static int maxValue(int[] arr) public static int maxValue(int[] arr, int firstIndex, int lastIndex) public static int indexOfFirstMaxValue(int[] arr) public static int minValue(int[] arr)

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • Consider the following short program. If there's an error (either compilation or runtime) in the program,...

    Consider the following short program. If there's an error (either compilation or runtime) in the program, then tell me what the problem is. But if the program works fine, then describe in 1 or 2 sentences what's the goal of the program. public class Main { public static void main(String[] args) { String[] arr = args; m(arr); public static void m(String... a) { for (int i = 0; i < a.length; i++) System.out.println(a[i] + a[i]);

  • KINDLY Describe the approach (in plain English) used to completing the coding below and explain the major decisions made...

    KINDLY Describe the approach (in plain English) used to completing the coding below and explain the major decisions made in designing the program. As part of your explanation, be sure to identify the Java constructs you used that are specific and relevant to the program below. Depending on the program, these may include the mechanisms for output, input, selection statements, loops, methods, and so forth. ***********BEGINNING OF CODE*************** import java.util.*; public class CountOccurances{ public static void main(String[] args) { Scanner...

  • Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...

    Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies. 2. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard. This is the base code given: public class NumbersDemo { public static void main (String args[]) { // Write your...

  • In this lab, you complete a partially written Java program that includes methods that require multiple...

    In this lab, you complete a partially written Java program that includes methods that require multiple parameters (arguments). The program prompts the user for two numeric values. Both values should be passed to methods named calculateSum(), calculateDifference(), and calculateProduct(). The methods compute the sum of the two values, the difference between the two values, and the product of the two values. Each method should perform the appropriate computation and display the results. The source code file provided for this lab...

  • Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13...

    Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13 Assignment 13 Preparation This assignment will focus on the use of object methods during event handling. Assignment 13 Assignment 13 Submission Follow the directions below to submit Assignment 13: This assignment will be a modification of the Assignment 12 program (see EncryptionApplication11.java below). Replace the use of String concatenation operations in the methods with StringBuilder or StringBuffer objects and their methods. EncryptTextMethods.java ====================== package...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • Get the file “HW4Part4a.java” from the repository. Modify the program class to match the new file...

    Get the file “HW4Part4a.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named “pow2” with one parameter, an integer n. The function is defined as follows: if n ≤ 0 then pow2(n) = 1. Otherwise, pow2(n) = 2 · pow2(n − 1). public class HW4Part4a { public static void main(String[] args) { for (int i = 0; i <= 20; i++) { System.out.println("pow2("+i+") = "+pow2(i)); }...

  • 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)....

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