Hi, i have been working on this program and will display the array contents to the user and ask how many positions he would like to shift off the right side of the array, and replace onto the left side.
This is what is supposed to look like:
Array contents: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Shift how many positions? 5
Array contents: 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10
Shift how many positions? 2
Array contents: 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8
Shift how many positions? 0
Array contents: 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8
Shift how many positions? -8
Array contents: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1
Shift how many positions? 15
Array contents: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1
Shift how many positions? 17
Array contents: 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14
And im getting this:
Array Contents: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
Shift how many positions?5
Here is the main class:
public class Shift1{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner kp = new Scanner(System.in);
final int size = 15;
char q = 'y';
boolean flag = false;
Shifter test = new Shifter(size);
test.display();
System.out.println();
Scanner input = new Scanner(System.in);
System.out.print("Shift how many positions?");{
int value1 = input.nextInt();
test.shift(value1);
test.display();
}
}
Here is additional class:
public class Shifter
{
public int [] data=new int[15];
public Shifter()
{
int size=0;
}
public Shifter(int size){
for (int i = 0; i < data.length; i++)
{
Random r = new Random(15);
int second = r.nextInt(15) + 1;
int temp = data[i];
data[i] = data[second];
data[second] = temp;
}
}
public void shift(int pos){
for(int x=0; x<pos; x++)
{
int cnt = data.length-1;
int temp = data[cnt];
for(cnt=data.length-1; cnt>0; cnt--)
{
data[cnt] = data[cnt]-1;
}
data[0] =temp;
}
}
public void display(){
String values = "";
for (int i = 0; i < data.length; i++)
{
if (i < 15)
{
values += (i + 1);
if (i < 14)
{
values += ", ";
}
}
}
System.out.printf("Array Contents: %s \n", values);
}
}