SRL Archive - Help and Support

Java help


Sin

2013-05-16 20:01:25

C4 Assignment The Mad Jailor
*
One night, the Mad Jailor at the Kingston Penitentiary went crazy. The 25 jail cells began like this (u=unlocked, l=locked):
lllllllllllllllllllllllll
After all of the inmates were deeply asleep, the jailor walked down the aisle of 100 cells in the prison and unlocked every one. The doors looked like (u=unlocked, l=locked):
uuuuuuuuuuuuuuuuuuuuuuuuu
Then the jailor walks down the row locking every 3rd cell. The cells then look like:
uuluuluuluuluuluuluuluulu
Then the jailor walks down the row unlocking every 4th cell if it is locked or locking the cell if it is unlocked. The cells then look like:
uullululluuuuullululluuuu
Then the jailor walks down the row unlocking every 5th cell if it is locked or locking the cell if it is unlocked. The cells then look like:
uullllullluuuuululuuluuul

The jailor continues walking down the row unlocking or relocking cells. The jailor begins by walking to the right down the aisle. After one sweep he reverses direction and sweeps left (skipping cell). Therefore, the jailor alternates right, then left when he walks down the aisle. Once he finishes skipping every 5th cell, he resets the skipping sequence to lock/unlock every 3rd cell. He resets back to skipping every 3rd cell after each time he skips every 5th cell. Assuming that the prisoners stayed asleep all night, how many walks down the aisle will it take before ALL cells are locked again?

Write a program that:
Displays in Application mode, not in an Applet.
Uses a char array to store whether the cell is locked (l) or unlocked (u).
After each sweep of key turning, print the array values to the screen.
At the end of the program (cells become ALL ā€˜l’ again), print the number of sweeps taken to reach that point. Print the number of sweeps taken after each re-arrangement of the array.
Make methods to initialize the arrays, turn the keys, print the number of the unlocked and locked doors to the screen. Print the array.





Can someone help me out with that? It's so freaking annoying, I don't understand what it's even asking…

NKN

2013-05-17 07:06:31

Can't you just use recursion to iterate through the array, changing it?

Doesn't seem too hard. What do you need to be displayed, the jailer walking, as like, an animation thing? I'm confused on the application part, what's going on it.
edit: Don't mind me, I didn't know the technical term for a program that uses the main method. This should be easy.


Make a procedure that uses recursion to change the array.
Parameters for it should be like: public void fixArray(char[] arr, int num)

Where arr is your array, and num is how many he skips before locking/unlocking.

And to show in the application, just System.out.print your entire array.


Am I missing something?

@Sin

Sin

2013-05-17 07:29:36

Yeah I get the format and how to do it, but the instructions are SO complicated@@@
It isn't well described and my prof refuses to explain it further -_______-

weequ

2013-05-17 13:33:43

I got lost at reversing directions… up, down, left, right :S

Brandon

2013-05-17 15:16:42

@Sin;

This is the idea that came into my head while reading the question.. Just run it and see.. I think I'm wrong though because the cells never lock lol but the idea is what you need I guess..


import java.awt.*;
import javax.swing.*;

public class Main {

public static void main(String[] Args) {
JPanel Jailer[] = new JPanel[50];
JPanel LeftCells[] = new JPanel[50];
JPanel RightCells[] = new JPanel[50];

JFrame F = new JFrame("Jail");
F.setLayout(new GridLayout(3, 100, 1, 1));
for (int I = 0; I < 150; ++I) {
if (I < 50) {
LeftCells[I] = new JPanel();
LeftCells[I].setBackground(Color.red);
F.add(LeftCells[I]);
} else if (I >= 100 && I < 150) {
RightCells[I - 100] = new JPanel();
RightCells[I - 100].setBackground(Color.red);
F.add(RightCells[I - 100]);
} else {
Jailer[I - 50] = new JPanel();
Jailer[I - 50].setBackground(Color.white);
F.add(Jailer[I - 50]);
}
}

F.setPreferredSize(new Dimension(815, 75));
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.pack();
F.setVisible(true);
unlockAllCells(Jailer, LeftCells, RightCells);
resetJailer(Jailer);
lockEveryThird(Jailer, LeftCells, RightCells);
resetJailer(Jailer);
lockUnlockEveryNth(Jailer, LeftCells, RightCells, 4);
resetJailer(Jailer);
lockUnlockEveryNth(Jailer, LeftCells, RightCells, 5);
resetJailer(Jailer);

while (!isAllCellsLocked(LeftCells, RightCells)) {
for (int I = 3; I < 5; ++I) {
lockUnlockEveryNthSide(Jailer, RightCells, I);
lockUnlockEveryNthSideReverse(Jailer, LeftCells, I);
}
}
}

public static boolean isAllCellsLocked(JPanel LeftCells[], JPanel RightCells[]) {
for (int I = 0; I < LeftCells.length; ++I) {
if (LeftCells[I].getBackground() != Color.red || RightCells[I].getBackground() != Color.red) {
return false;
}
}
return true;
}

public static void lockUnlockEveryNthSideReverse(JPanel Jailer[], JPanel Cells[], int N) {
for (int I = Cells.length - 1; I > 0; I -= N) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

if (Cells[I].getBackground() == Color.red) {
Cells[I].setBackground(Color.green);
} else {
Cells[I].setBackground(Color.red);
}
Sleep(500);
}
}

public static void lockUnlockEveryNthSide(JPanel Jailer[], JPanel Cells[], int N) {
for (int I = 0; I < Cells.length; I += N) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

if (Cells[I].getBackground() == Color.red) {
Cells[I].setBackground(Color.green);
} else {
Cells[I].setBackground(Color.red);
}
Sleep(500);
}
}

public static void lockEveryThird(JPanel Jailer[], JPanel LeftCells[], JPanel RightCells[]) {
for (int I = 0; I < LeftCells.length; I += 3) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

LeftCells[I].setBackground(Color.red);
RightCells[I].setBackground(Color.red);
Sleep(500);
}
}

public static void lockUnlockEveryNth(JPanel Jailer[], JPanel LeftCells[], JPanel RightCells[], int N) {
for (int I = 0; I < LeftCells.length; I += N) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

if (LeftCells[I].getBackground() == Color.red) {
LeftCells[I].setBackground(Color.green);
RightCells[I].setBackground(Color.green);
} else {
LeftCells[I].setBackground(Color.red);
RightCells[I].setBackground(Color.red);
}
Sleep(500);
}
}

public static void resetJailer(JPanel Jailer[]) {
Jailer[0].setBackground(Color.black);
for (int I = 0; I < Jailer.length; ++I) {
Jailer[I].setBackground(Color.white);
}
}

public static void unlockAllCells(JPanel Jailer[], JPanel LeftCells[], JPanel RightCells[]) {
for (int I = 0; I < LeftCells.length; ++I) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

LeftCells[I].setBackground(Color.green);
RightCells[I].setBackground(Color.green);
Sleep(100);
}
}

public static void Sleep(long Time) {
try {
Thread.sleep(Time);
} catch(Exception e) {
e.printStackTrace();
}
}
}

Sin

2013-05-17 20:05:35

From: Brandon
@Sin;

This is the idea that came into my head while reading the question.. Just run it and see.. I think I'm wrong though because the cells never lock lol but the idea is what you need I guess..


import java.awt.*;
import javax.swing.*;

public class Main {

public static void main(String[] Args) {
JPanel Jailer[] = new JPanel[50];
JPanel LeftCells[] = new JPanel[50];
JPanel RightCells[] = new JPanel[50];

JFrame F = new JFrame("Jail");
F.setLayout(new GridLayout(3, 100, 1, 1));
for (int I = 0; I < 150; ++I) {
if (I < 50) {
LeftCells[I] = new JPanel();
LeftCells[I].setBackground(Color.red);
F.add(LeftCells[I]);
} else if (I >= 100 && I < 150) {
RightCells[I - 100] = new JPanel();
RightCells[I - 100].setBackground(Color.red);
F.add(RightCells[I - 100]);
} else {
Jailer[I - 50] = new JPanel();
Jailer[I - 50].setBackground(Color.white);
F.add(Jailer[I - 50]);
}
}

F.setPreferredSize(new Dimension(815, 75));
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.pack();
F.setVisible(true);
unlockAllCells(Jailer, LeftCells, RightCells);
resetJailer(Jailer);
lockEveryThird(Jailer, LeftCells, RightCells);
resetJailer(Jailer);
lockUnlockEveryNth(Jailer, LeftCells, RightCells, 4);
resetJailer(Jailer);
lockUnlockEveryNth(Jailer, LeftCells, RightCells, 5);
resetJailer(Jailer);

while (!isAllCellsLocked(LeftCells, RightCells)) {
for (int I = 3; I < 5; ++I) {
lockUnlockEveryNthSide(Jailer, RightCells, I);
lockUnlockEveryNthSideReverse(Jailer, LeftCells, I);
}
}
}

public static boolean isAllCellsLocked(JPanel LeftCells[], JPanel RightCells[]) {
for (int I = 0; I < LeftCells.length; ++I) {
if (LeftCells[I].getBackground() != Color.red || RightCells[I].getBackground() != Color.red) {
return false;
}
}
return true;
}

public static void lockUnlockEveryNthSideReverse(JPanel Jailer[], JPanel Cells[], int N) {
for (int I = Cells.length - 1; I > 0; I -= N) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

if (Cells[I].getBackground() == Color.red) {
Cells[I].setBackground(Color.green);
} else {
Cells[I].setBackground(Color.red);
}
Sleep(500);
}
}

public static void lockUnlockEveryNthSide(JPanel Jailer[], JPanel Cells[], int N) {
for (int I = 0; I < Cells.length; I += N) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

if (Cells[I].getBackground() == Color.red) {
Cells[I].setBackground(Color.green);
} else {
Cells[I].setBackground(Color.red);
}
Sleep(500);
}
}

public static void lockEveryThird(JPanel Jailer[], JPanel LeftCells[], JPanel RightCells[]) {
for (int I = 0; I < LeftCells.length; I += 3) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

LeftCells[I].setBackground(Color.red);
RightCells[I].setBackground(Color.red);
Sleep(500);
}
}

public static void lockUnlockEveryNth(JPanel Jailer[], JPanel LeftCells[], JPanel RightCells[], int N) {
for (int I = 0; I < LeftCells.length; I += N) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

if (LeftCells[I].getBackground() == Color.red) {
LeftCells[I].setBackground(Color.green);
RightCells[I].setBackground(Color.green);
} else {
LeftCells[I].setBackground(Color.red);
RightCells[I].setBackground(Color.red);
}
Sleep(500);
}
}

public static void resetJailer(JPanel Jailer[]) {
Jailer[0].setBackground(Color.black);
for (int I = 0; I < Jailer.length; ++I) {
Jailer[I].setBackground(Color.white);
}
}

public static void unlockAllCells(JPanel Jailer[], JPanel LeftCells[], JPanel RightCells[]) {
for (int I = 0; I < LeftCells.length; ++I) {
resetJailer(Jailer);
Jailer[I].setBackground(Color.black);

LeftCells[I].setBackground(Color.green);
RightCells[I].setBackground(Color.green);
Sleep(100);
}
}

public static void Sleep(long Time) {
try {
Thread.sleep(Time);
} catch(Exception e) {
e.printStackTrace();
}
}
}


That looks really neat :D
But unfortunately, it's not the correct way :/
You have two rows of jail cells, when there is only supposed to be one. I found out at school today that it has to be like this:

~ALL LOCKED~
~ALL UNLOCKED~
lulululululululululululululululululululululul
uullluuullluuullluuullluuullluuullluuullluuulll
lulluuuuullululluuuuullululluuuuullululluuuuullulul


Direction: Right –> Skip: 3
Direction: Left <– Skip: 4
Direction: Right –> Skip: 5
Direction: Left <– Skip: 3
Direction: Left <– Skip: 4
Direction: Right –> Skip: 5

etc.

It should take 12 turns to lock everything again.
I got the due date extended to Tuesday (Monday is Victoria Day), and so I have three days to do it..

Brandon

2013-05-17 20:48:56

From: Sin
That looks really neat :D
But unfortunately, it's not the correct way :/
You have two rows of jail cells, when there is only supposed to be one. I found out at school today that it has to be like this:



:S Iunno.. I guess your school is right but.. an aisle is a walkway with rows or columns on two sides :S So thats where I got the idea from lol Too much attention to details I guess :l

NKN

2013-05-18 12:27:11

I don't fully understand your project.

it's simple enough to add or remove something I forgot to fix it.
It never gets to all locked, it just eventually unlocks them all again.


public class SinsHomework {
private static int cnt = 0;
private static boolean done = false;
private static char[] cells = {'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u'};
public static void main(String[] args){
lockDoors(cells,3,0);
System.out.println(cnt);
}
public static void lockDoors(char[] carr,int cellz,int count){
int i = 0;


for(i=1;i<carr.length;i++){
if((i+1)%cellz == 0)
if(carr[i] == 'l')
carr[i] = 'u';
else
carr[i] = 'l';

}
if(count>0 ){
for(i=0;i<carr.length;i++){
if(carr[i] == 'u'){
done = false;
break;
}
else
done = true;
}
if(done){
cnt = count;
return;
}

}
int swp = 0;
for (i = carr.length - 1; i >= carr.length / 2; i--) {

char temp = carr[i];
carr[i] = carr[swp];
carr[swp] = temp;
swp++;
}
System.out.println(carr);
if(cellz>4)
lockDoors(carr,3,count+1);
else{
lockDoors(carr,cellz+1,count+1);
}

}





















































}

Sin

2013-05-18 17:59:28

From: NKN
I don't fully understand your project.

it's simple enough to add or remove something I forgot to fix it.
It never gets to all locked, it just eventually unlocks them all again.


public class SinsHomework {
private static int cnt = 0;
private static boolean done = false;
private static char[] cells = {'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u'};
public static void main(String[] args){
lockDoors(cells,3,0);
System.out.println(cnt);
}
public static void lockDoors(char[] carr,int cellz,int count){
int i = 0;


for(i=1;i<carr.length;i++){
if((i+1)%cellz == 0)
if(carr[i] == 'l')
carr[i] = 'u';
else
carr[i] = 'l';

}
if(count>0 ){
for(i=0;i<carr.length;i++){
if(carr[i] == 'u'){
done = false;
break;
}
else
done = true;
}
if(done){
cnt = count;
return;
}

}
int swp = 0;
for (i = carr.length - 1; i >= carr.length / 2; i--) {

char temp = carr[i];
carr[i] = carr[swp];
carr[swp] = temp;
swp++;
}
System.out.println(carr);
if(cellz>4)
lockDoors(carr,3,count+1);
else{
lockDoors(carr,cellz+1,count+1);
}

}





















































}


That's wrong too :'(

NKN

2013-05-18 18:15:40

From: Sin
That's wrong too :'(


I said it's wrong…
It should be an easy fix, just fix it

Everything is there.

I would, but I don't understand the problem.

Kasi

2013-05-18 18:47:57

maybe if you type out how the lines should look someone might be able to help you, looking at the info supplied by your prof, he seems like he pulled that question out his ass, i dont understand how its actually supposed work.

@NKN; damn why did you initialize your array like that??

NKN

2013-05-18 19:30:31

From: Kasi
maybe if you type out how the lines should look someone might be able to help you, looking at the info supplied by your prof, he seems like he pulled that question out his ass, i dont understand how its actually supposed work.

@NKN; damn why did you initialize your array like that??

Did it at 5 AM, only had like, 30 minutes. CBA to make it pretty.