Skip to content

Instantly share code, notes, and snippets.

@mohamed-ennahdi
Last active February 21, 2017 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamed-ennahdi/26ba663b21f0010b926e to your computer and use it in GitHub Desktop.
Save mohamed-ennahdi/26ba663b21f0010b926e to your computer and use it in GitHub Desktop.
package com.exercises.main;
import java.util.Scanner;
/**
* @author Mohamed Ennahdi El Idrissi
*
*
* This program does the following:
* - Reading the size (int) of the array from the keyboard;
* - Reading each element of the array from the keyboard;
* - Reading the number of rotations (n) from the keyboard;
* - Saving a copy of the array;
* - The gist of the logic is to shift 'n' times;
* - Displaying the array before rotation;
* - Displaying the array after rotation;
* - Using 'assert' as a test unit to verify that the program runs as expected:
* - Comparing the new value's position with the original array using a formula.
*
*/
public class EvaluationExercise {
public final static int MAX_SIZE = 100;
private int[] array;
/**
* Default Contructor.
*/
public EvaluationExercise() {
this.array = new int[]{1, 2, 3, 4, 5, 6};
}
/**
* Constructor.
*/
public EvaluationExercise(int[] array) {
this.array = array;
}
/**
* Getter.
* @return
*/
public int[] getArray() {
return array;
}
/**
* Setter.
* @param array
*/
public void setArray(int[] array) {
this.array = array;
}
/**
* Right shift the array by one position.
*/
public void rightShit() {
int v = array[array.length - 1];
for (int i = array.length - 2; i >= 0 ; i --) {
array[i + 1] = array[i];
}
array[0] = v;
}
/**
* Display the array.
*/
public void displayArray() {
for (int i = 0; i < this.array.length; i++) {
System.out.print(this.array[i] + " ");
}
}
public void rotate(int n) {
/*
* Shifting 'n' times.
* With O(n * size), where n is very small.
*/
for (int i = 0; i < n; i++) {
this.rightShit();
}
}
/**
* @param args
*/
public static void main(String[] args) {
/*
* Reading the size of array, elements of the array,
* and number of rotations from the keyboard.
*/
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
int size = -1;
System.out.println("Enter array size: ");
do {
try {
size = scanner.nextInt();
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
} while (size < 0 || size > EvaluationExercise.MAX_SIZE);
} finally {
if (scanner != null) {
scanner.close();
}
}
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
System.out.println("Enter array[" + i + "] = " );
array[i] = scanner.nextInt();
}
try {
scanner = new Scanner(System.in);
System.out.println();
System.out.println();
System.out.println("Enter number of rotations: ");
int n = -1;
do {
try {
n = scanner.nextInt();
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
} while (n < 0 || n > size);
} finally {
if (scanner != null) {
scanner.close();
}
}
EvaluationExercise exercise = new EvaluationExercise(array);
/*
* Displaying the original array.
*/
System.out.println();
System.out.println();
System.out.print("Before rotation(s): ");
exercise.displayArray();
/*
* Overhead optimization.
* - No need to 'rotate' when n == size, or when n is a duplicate of size.
* - No need to 'rotate' n times when n % size is considerably smaller and sufficient.
*/
n = n % size;
/*
* Rotating.
*/
exercise.rotate(n);
System.out.println();
System.out.print("After rotation(s): ");
/*
* Displaying the array after rotation(s).
*/
exercise.displayArray();
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment