Question

In: Computer Science

Assignment Purpose The purpose of this lab is to write a well commented java program that...

Assignment Purpose

The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.

Instructions

  1. Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100.
  2. The method creates a new array with the items of x moved forward by n Elements that are rotated off the array will appear at the end. Forward rotation means moving elements from right to left on paper. For example, suppose x contains the following items in sequence:

1 2 3 4 5 6 7 and the value of n = 3

  1. Your program should first calculate the sum of numbers at even indexes (consider 0 as even) which in this case is 1 + 3 + 5 + 7 = 16, and then average of numbers at odd indexes which in this case is (2 + 4 + 6) / 3 = 4.
  2. After that it should make a call to rotateArray with arguments as x and n.

Sample output

-----------------------Output Begins-------------------------

The randomly generated integers in array are: 1 2 3 4 5 6 7

Sum of numbers at even indexes = 16

Average of numbers at odd indexes = 4

Enter rotation count: 3

Calling rotateArray with rotation count as 3………

After 1st rotation the array contents are         2 3 4 5 6 7 1

After 2nd rotation the array contents are        3 4 5 6 7 1 2

After 3rd rotation the array contents are        4 5 6 7 1 2 3

-----------------------End of Output----------------------------

So after rotating by 3, the elements in the new array will appear in this sequence:

4 5 6 7 1 2 3

Solutions

Expert Solution

import java.util.*;

public class Main{
        //Method to rotate array by one 
        static void RotatebyOne(int arr[], int n) 
    { 
        int i, temp; 
        // storing the first element in a temporary variable
        temp = arr[0]; 
        for (i = 0; i < n - 1; i++) 
            arr[i] = arr[i + 1]; 
        arr[i] = temp; 
    } 
        
        public static void rotateArray(int x[],int n) {
                //Rotating array one by one
                for(int i=0;i<n;++i) {
                        System.out.print("After " + (i+1) + " rotation the array contents are ");
                        RotatebyOne(x,x.length); 
                        //printing the array
                        for(int j=0;j<x.length;++j) {
                                System.out.print(x[j] + " ");
                        }
                        System.out.println();
                }
                
                System.out.print("So after rotating by " + n + ", the elements in the new array will appear in this sequence: ");
                //printing the array
                for(int i=0;i<x.length;++i) {
                        System.out.print(x[i] + " ");
                }
                System.out.println();   
        }

    public static void main(String[] args){
        
        Scanner input = new Scanner(System.in);
        
        int even=0,odd=0,c=0;
        int x[] = new int[7];
                for(int i=0;i<x.length;++i) {

                        //assigning random values to the array between 1 to 100
                        x[i] = (int) (Math.random() * (100 -1 + 1) + 1) ; 
                        // calculating sum of elements at even and odd places
                        if(i%2==0)
                                even += x[i];
                        else {
                                odd += x[i];
                                c++;
                        }               
                }
                
                System.out.print("The randomly generated integers in array are: ");
                for(int i=0;i<x.length;++i) {
                        System.out.print(x[i] + " ");
                }
                System.out.println("\nSum of numbers at even indexes = " + even);
                System.out.println("Average of numbers at odd indexes = " + (double)odd/c);
                System.out.print("Enter rotation count: ");

                //Reading input for n
                int n = input.nextInt();
                System.out.println("Calling rotateArray with rotation count as " + n + ".....");
                //Calling rotateArray method to rotate the array n times
                rotateArray(x,n);
      
    }

}

OUTPUT:


Related Solutions

Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht can olny sepll a wrod one way.”...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT