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

Following is the answer:

import java.util.Random;
import java.util.Scanner;

public class RotateArray {
void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++) {
leftRotatebyOne(arr, n);
printArray(arr, n);
System.out.println("\n");
}
}

void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[i] = temp;
}

void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}

void sumOfEven(int arr[], int n){
int sum = 0;
double avg = 0;
int sum2 = 0;
int count = 0;
for(int i = 0 ; i < n ; i++){

if(i%2 == 0){
sum+= arr[i];
}else {
sum2+= arr[i];
count++;
}
}
System.out.println("\nSum of numbers at even indexes = " + sum);
System.out.println("Average of numbers at odd indexes = " + (sum2/count));
}

// Driver program to test above functions
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, count;

RotateArray rotate = new RotateArray();

System.out.println("Enter array size:");
n = sc.nextInt();

int arr[] = new int[n];
Random rand = new Random();
for (int i = 0; i < n; i++) {
arr[i] = rand.nextInt(100-1) + 1;
}
System.out.println("The randomly generated integers in array are: ");
rotate.printArray(arr, n);
rotate.sumOfEven(arr, 7);
System.out.println("Enter rotation count: ");
count = sc.nextInt();
rotate.leftRotate(arr, count, 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 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...
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 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