Memory Leak with Single Method - java

This whole memory leak thing seems very complicated to me. A lot of great info here, but I cannot quite understand it. Even after reading Creating a memory leak with Java, I am still lost! Can someone point out to me where the memory leak is in this code, please? (It happens when this method is run repeatedly.) Thank you very much!
08-11 23:02:07.487: I/ViewRootImpl(969): Reporting drop result: true
08-11 23:02:07.987: D/dalvikvm(969): GC_FOR_ALLOC freed 1769K, 18% free 10101K/12180K, paused 13ms, total 13ms
08-11 23:02:08.057: D/dalvikvm(969): GC_CONCURRENT freed 4K, 11% free 10915K/12180K, paused 3ms+5ms, total 30ms
08-11 23:02:08.057: D/dalvikvm(969): WAIT_FOR_CONCURRENT_GC blocked 1ms
08-11 23:02:14.757: I/dalvikvm(969): threadid=3: reacting to signal 3
08-11 23:02:14.767: I/dalvikvm(969): Wrote stack traces to '/data/anr/traces.txt'
public void deal(View v) {
// call and play
if (dealt) {
if (anteBet == playBet) {
// hand plays
dealt = false;
deal.setImageResource(R.drawable.deal);
fold.setImageResource(R.drawable.clear);
dealersCards.clear();
dealersCards.add(d1);
dealersCards.add(d2);
dealersCards.add(d3);
displayCards(dealersCards);
if (getHand(pc1, pc2, pc3) > getHand(dc1, dc2, dc3)
&& getHand(dc1, dc2, dc3) >= 1120000) {
award = true;
ante(anteBet);
award = true;
play(playBet);
} else if (getHand(dc1, dc2, dc3) < 1120000) {
award = true;
ante(anteBet);
}
else {
if (!(getHand(pc1, pc2, pc3) == getHand(dc1, dc2, dc3))) {
award = true;
ante(-anteBet);
award = true;
play(-playBet);
}
}
antePlusPayout();
pairPlusPayout();
} else {
play(anteBet - playBet);
}
// deal cards
} else if (anteBet > 0) {
deal.setImageResource(R.drawable.call);
fold.setImageResource(R.drawable.fold);
displayCards(display);
d1.setImageResource(R.drawable.facedown);
d2.setImageResource(R.drawable.facedown);
d3.setImageResource(R.drawable.facedown);
dealt = true;
deal.setImageResource(R.drawable.call);
}
}
public void displayCards(List<ImageView> disp) {
int cardNumber = 0;
for (ImageView c : disp) {
cardNumber++;
Card tempCard;
do {
tempCard = deck.get(rand(52));
} while (cardsInPlay.contains(tempCard));
cardsInPlay.add(tempCard);
rank = tempCard.getRank();
suit = tempCard.getSuit();
if (cardNumber == 1) {
dc1 = tempCard;
} else if (cardNumber == 2) {
dc2 = tempCard;
} else if (cardNumber == 3) {
dc3 = tempCard;
} else if (cardNumber == 4) {
pc1 = tempCard;
} else if (cardNumber == 5) {
pc2 = tempCard;
} else {
pc3 = tempCard;
}
if (rank == 2 && suit == 'c') {
c.setImageResource(R.drawable.clubs2);
} else if (rank == 3 && suit == 'c') {
c.setImageResource(R.drawable.clubs3);
} else if (rank == 4 && suit == 'c') {
c.setImageResource(R.drawable.clubs4);
} else if (rank == 5 && suit == 'c') {
c.setImageResource(R.drawable.clubs5);
} else if (rank == 6 && suit == 'c') {
c.setImageResource(R.drawable.clubs6);
} else if (rank == 7 && suit == 'c') {
c.setImageResource(R.drawable.clubs7);
} else if (rank == 8 && suit == 'c') {
c.setImageResource(R.drawable.clubs8);
} else if (rank == 9 && suit == 'c') {
c.setImageResource(R.drawable.clubs9);
} else if (rank == 10 && suit == 'c') {
c.setImageResource(R.drawable.clubs10);
} else if (rank == 11 && suit == 'c') {
c.setImageResource(R.drawable.clubsj);
} else if (rank == 12 && suit == 'c') {
c.setImageResource(R.drawable.clubsq);
} else if (rank == 13 && suit == 'c') {
c.setImageResource(R.drawable.clubsk);
} else if (rank == 14 && suit == 'c') {
c.setImageResource(R.drawable.clubsa);
}
else if (rank == 2 && suit == 'd') {
c.setImageResource(R.drawable.diamonds2);
} else if (rank == 3 && suit == 'd') {
c.setImageResource(R.drawable.diamonds3);
} else if (rank == 4 && suit == 'd') {
c.setImageResource(R.drawable.diamonds4);
} else if (rank == 5 && suit == 'd') {
c.setImageResource(R.drawable.diamonds5);
} else if (rank == 6 && suit == 'd') {
c.setImageResource(R.drawable.diamonds6);
} else if (rank == 7 && suit == 'd') {
c.setImageResource(R.drawable.diamonds7);
} else if (rank == 8 && suit == 'd') {
c.setImageResource(R.drawable.diamonds8);
} else if (rank == 9 && suit == 'd') {
c.setImageResource(R.drawable.diamonds9);
} else if (rank == 10 && suit == 'd') {
c.setImageResource(R.drawable.diamonds10);
} else if (rank == 11 && suit == 'd') {
c.setImageResource(R.drawable.diamondsj);
} else if (rank == 12 && suit == 'd') {
c.setImageResource(R.drawable.diamondsq);
} else if (rank == 13 && suit == 'd') {
c.setImageResource(R.drawable.diamondsk);
} else if (rank == 14 && suit == 'd') {
c.setImageResource(R.drawable.diamondsa);
}
else if (rank == 2 && suit == 'h') {
c.setImageResource(R.drawable.hearts2);
} else if (rank == 3 && suit == 'h') {
c.setImageResource(R.drawable.hearts3);
} else if (rank == 4 && suit == 'h') {
c.setImageResource(R.drawable.hearts4);
} else if (rank == 5 && suit == 'h') {
c.setImageResource(R.drawable.hearts5);
} else if (rank == 6 && suit == 'h') {
c.setImageResource(R.drawable.hearts6);
} else if (rank == 7 && suit == 'h') {
c.setImageResource(R.drawable.hearts7);
} else if (rank == 8 && suit == 'h') {
c.setImageResource(R.drawable.hearts8);
} else if (rank == 9 && suit == 'h') {
c.setImageResource(R.drawable.hearts9);
} else if (rank == 10 && suit == 'h') {
c.setImageResource(R.drawable.hearts10);
} else if (rank == 11 && suit == 'h') {
c.setImageResource(R.drawable.heartsj);
} else if (rank == 12 && suit == 'h') {
c.setImageResource(R.drawable.heartsq);
} else if (rank == 13 && suit == 'h') {
c.setImageResource(R.drawable.heartsk);
} else if (rank == 14 && suit == 'h') {
c.setImageResource(R.drawable.heartsa);
}
else if (rank == 2 && suit == 's') {
c.setImageResource(R.drawable.spades2);
} else if (rank == 3 && suit == 's') {
c.setImageResource(R.drawable.spades3);
} else if (rank == 4 && suit == 's') {
c.setImageResource(R.drawable.spades4);
} else if (rank == 5 && suit == 's') {
c.setImageResource(R.drawable.spades5);
} else if (rank == 6 && suit == 's') {
c.setImageResource(R.drawable.spades6);
} else if (rank == 7 && suit == 's') {
c.setImageResource(R.drawable.spades7);
} else if (rank == 8 && suit == 's') {
c.setImageResource(R.drawable.spades8);
} else if (rank == 9 && suit == 's') {
c.setImageResource(R.drawable.spades9);
} else if (rank == 10 && suit == 's') {
c.setImageResource(R.drawable.spades10);
} else if (rank == 11 && suit == 's') {
c.setImageResource(R.drawable.spadesj);
} else if (rank == 12 && suit == 's') {
c.setImageResource(R.drawable.spadesq);
} else if (rank == 13 && suit == 's') {
c.setImageResource(R.drawable.spadesk);
} else if (rank == 14 && suit == 's') {
c.setImageResource(R.drawable.spadesa);
}
}
}

In java, the usual cause of excessive memory usage, is objects being allocated and not destroyed, in some sort of loop, such as a game loop. Are you adding to a dynamic array/list of sorts in a loop? Also, if a method takes a while, this effect can happen with local variables before they are destroyed.

You need to do this with your ImageView if you change their images often
Drawable obj = imgView.getDrawable();
if( obj instanceof BitmapDrawable)
{
Bitmap bmp = obj.getBitmap();
bmp.Recycle();
}
Before this statement call a cleanupcode function
Bitmap CleanupCode(ImageView imgView) {
Drawable obj = imgView.getDrawable();
if( obj instanceof BitmapDrawable) {
Bitmap bmp = obj.getBitmap(); bmp.Recycle(); return bmp;
}
return null;
}
////
Bitmap bmp = CleanupCode(c);
c.setImageResource(R.drawable.clubs3);
if(null != bmp) {
bmp.Recycle();
bmp = null;
}

Related

Reduce the number of conditional operators

How can I reduce the number of operators to 3 properly,
with multiple methods ? or loop ?
the constant of Actions is Int
String act = req.getParameter("ACTION");
int actInt = -1;
try {
actInt = Integer.parseInt(act);
} catch (NumberFormatException nfe) {
//
}
boolean actionNulle = act == null;
boolean actionDefaut = actionNulle || actInt == -1;
if ( (actionReq.equals(ACTION_LIST_ENREG) && (actionDefaut || !(actInt == Actions.AJOUTER || actInt == Actions.VALIDER || actInt == Actions.NOUVEAU || actInt == Actions.NOUVEAU_PAR_COPIE)))
|| (actionReq.equals("PreAbattage") && (actionDefaut || (actInt == Actions.DEFAUT)))
|| (actionReq.equals(ACTION_FORMULAIRE_RECHERCHE) && (actionDefaut || !(actInt == Actions.NOUVEAU_PAR_COPIE || actInt == Actions.NOUVEAU)))
|| (actionReq.equals("ModificationMultiple") && (actionDefaut || !(actInt == Actions.OUI || actInt == Actions.SUBSTITUTION)))
|| (actionReq.equals(ACTION_VISU_RECORD) && (actionDefaut || !(actInt == Actions.COPIE_PRIVE || actInt == Actions.NOUVEAU_PRIVE || actInt == Actions.MODIFIER || actInt == Actions.RESULTAT_CREATION)))
|| (actionReq.equals("VisuLock") && (actionDefaut || !(actInt == Actions.DETRUIRE_VERROU)))
|| (actionReq.equals("CopiePublique") && (actionDefaut || !(actInt == Actions.CREER_DONNEES_PUBLIQUES)))
|| (actionReq.equals("VisuSessions") && (actionDefaut || !(actInt == Actions.DETRUIRE_SESSION)))
|| (actionReq.equals(ACTION_VISU_MESSAGE) && (actionDefaut))
|| (actionReq.equals("RecapModifPub") && (actionDefaut || actInt == Actions.VALIDER))
) {
return true;
}
You could use a Map<String, Set<Integer>>
private final Map<String, Set<Integer>> validCombinations = initializeMap();
// initialize the map once
// later, in your code:
return validCombinations.containsKey(actionReq) &&
(actionDefault || validCombinations.get(actionReq).contains(actInt));

Taking a credit card number and outputting the company name

need to get the card issuer along with the number, only outputs "Unknown".
(the bottom is just tester code).
am i trying to test the wrong variable, am i incorrectly using indexOf()? please, any help would be appreciated
public class CreditCard
{
private String card_number;
private boolean is_number;
private String number_string = "";
public String issuer_name = "";
public CreditCard(String card_number)
{
this.card_number = card_number;
}
public String toString()
{
for (int x = 0; x < card_number.length(); x++)
{
char y = card_number.charAt(x);
is_number = Character.isDigit(y);
if (is_number)
{
number_string += y;
}
}
String s = number_string + " was issued by " + getIssuer();
return s;
}
public void setIssuer(String issuer)
{
issuer_name = issuer;
}
public String getIssuer()
{
String issuer_Name;
if (card_number.indexOf('4') == 0 && card_number.length() == 13 || card_number.length() == 16)
{
issuer_Name = "VISA";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('0') == 2 && number_string.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('5') == 2 && number_string.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('6') == 1 && card_number.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('8') == 1 && card_number.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('4') == 1 && card_number.length() == 15)
{
issuer_Name = "American Express";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('7') == 1 && card_number.length() == 15)
{
issuer_Name = "American Express";
}
if (card_number.indexOf('5') == 0 && card_number.indexOf('1') == 1 && card_number.length() == 16)
{
issuer_Name = "MasterCard";
}
if (card_number.indexOf('5') == 0 && card_number.indexOf('5') == 1 && card_number.length() == 16)
{
issuer_Name = "MasterCard";
}
if (card_number.indexOf('6') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('1') == 2 && number_string.indexOf('1') == 3 && number_string.length() == 16)
{
issuer_Name = "Discover";
}
else
{
issuer_Name = "Unknown";
}
return issuer_Name;
}
public static void main(String[] args)
{
System.out.println(new CreditCard("42225-22222222"));
System.out.println(new CreditCard("76009644571"));
System.out.println(new CreditCard("50197170-10103742"));
System.out.println(new CreditCard("6331101899890016"));
}
}
The if else clause would start something like
if (card_number.indexOf('4') == 0 && card_number.length() == 13 || card_number.length() == 16) {
issuer_Name = "VISA";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('0') == 2 && number_string.length() == 14) {
issuer_Name = "Diner's Club";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('5') == 2 && number_string.length() == 14) {
issuer_Name = "Diner's Club";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('6') == 1 && card_number.length() == 14) {
issuer_Name = "Diner's Club";
} else {
issuer_Name = "Other";
}
Note I am not looking at the correctness of the algorithm, but you asked for how to do the if/else
if (card_number.indexOf('6') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('1') == 2 && number_string.indexOf('1') == 3 && number_string.length() == 16)
{
issuer_Name = "Discover";
}
else
{
issuer_Name = "Unknown";
}
Check this Code.
If a card issuer is not Discover, the output ALWAYS "Unknow"
if (true) {
n=1;
}
if (true) {
n=2;
}
if (true) {
n=3;
}
else {
n=0;
}
The n is either 3 or 0, cannot be 1 or 2.
Because the last if-else will override the n
You can fix this issue by changing "if" to "else if"

Encode/Decode Script that decodes and encoded string twice

Basically have a simple process that involves mapping letters and numbers 2 spaces forward so an A would encode to C , 1 to 3 and such. There is wraparound for so Z would encode to B and 0 would encode to 2. The encode works fine, but after the first cycle of the loop the decode seems to map back 4 spaces instead of 2. Here's the code, let me know what you guys think the problem is.
Encode/Decode
public class EncodeDecode {
private String[] originalList;
private String[] encodedList;
private String[] decodedList;
public EncodeDecode(String[] oL) {
originalList = oL;
encodedList = originalList;
decodedList = originalList;
for (int cnt = 0; cnt < oL.length; cnt++) {
encodedList[cnt] = originalList[cnt];
}
for (int cnt = 0; cnt < oL.length; cnt++) {
decodedList[cnt] = originalList[cnt];
}
}
public String encode(String originalWord) {
//Maps every character in original word to 2 positions forward w/ wrap around.
String result = "";
for (int cnt = 0; cnt < originalWord.length(); cnt++) {
result += forwardMap(originalWord.charAt(cnt));
}
encodedList[0] = result;
return result;
}
public String decode(String codedWord) {
//Maps every character back 2 spaces w/ wrap around
String result = "";
for (int cnt = 0; cnt < codedWord.length(); cnt++) {
result += backMap(codedWord.charAt(cnt));
}
decodedList[0] = result;
return result;
}
public char forwardMap(char ch) {
char result = Character.toLowerCase(ch);
if (result == 'a') return 'c';
if (result == 'b') return 'd';
if (result == 'c') return 'e';
if (result == 'd') return 'f';
if (result == 'e') return 'g';
if (result == 'f') return 'h';
if (result == 'g') return 'i';
if (result == 'h') return 'j';
if (result == 'i') return 'k';
if (result == 'j') return 'l';
if (result == 'k') return 'm';
if (result == 'l') return 'n';
if (result == 'm') return 'o';
if (result == 'n') return 'p';
if (result == 'o') return 'q';
if (result == 'p') return 'r';
if (result == 'q') return 's';
if (result == 'r') return 't';
if (result == 's') return 'u';
if (result == 't') return 'v';
if (result == 'u') return 'w';
if (result == 'v') return 'x';
if (result == 'w') return 'y';
if (result == 'x') return 'z';
if (result == 'y') return 'a';
if (result == 'z') return 'b';
if (result == '0') return '2';
if (result == '1') return '3';
if (result == '2') return '4';
if (result == '3') return '5';
if (result == '4') return '6';
if (result == '5') return '7';
if (result == '6') return '8';
if (result == '7') return '9';
if (result == '8') return '0';
if (result == '9') return '1';
if (result == ' ')
return ' ';
else
return '$';
}
public char backMap(char ch) {
char result = Character.toLowerCase(ch);
if (result == 'c') return 'a';
if (result == 'd') return 'b';
if (result == 'e') return 'c';
if (result == 'f') return 'd';
if (result == 'g') return 'e';
if (result == 'h') return 'f';
if (result == 'i') return 'g';
if (result == 'j') return 'h';
if (result == 'k') return 'i';
if (result == 'l') return 'j';
if (result == 'm') return 'k';
if (result == 'n') return 'l';
if (result == 'o') return 'm';
if (result == 'p') return 'n';
if (result == 'q') return 'o';
if (result == 'r') return 'p';
if (result == 's') return 'q';
if (result == 't') return 'r';
if (result == 'u') return 's';
if (result == 'v') return 't';
if (result == 'w') return 'u';
if (result == 'x') return 'v';
if (result == 'y') return 'w';
if (result == 'z') return 'x';
if (result == 'a') return 'y';
if (result == 'b') return 'z';
if (result == '2') return '0';
if (result == '3') return '1';
if (result == '4') return '2';
if (result == '5') return '3';
if (result == '6') return '4';
if (result == '7') return '5';
if (result == '8') return '6';
if (result == '9') return '7';
if (result == '0') return '8';
if (result == '1') return '9';
if (result == ' ')
return ' ';
else
return '$';
}
public String[] getEncodedList() {
return encodedList;
}
public String[] getDecodedList() {
return decodedList;
}
}
Tester
public class EncodeDecodeTester {
public static void main(String[] args) {
String[] list = {"abcd", "abcd", "abcd"};
EncodeDecode ec = new EncodeDecode(list);
String[] encode = ec.getEncodedList();
for (int index = 0; index < encode.length; index++) {
System.out.println("Encoded: " +ec.encode(list[index]));
System.out.println("Decoded: " +ec.decode(list[index]));
}
}
}
The first thing I notice is object references on String[], change this
encodedList = originalList;
decodedList = originalList;
to
encodedList = new String[originalList.length];
decodedList = new String[originalList.length];
NOtice that in your main function you are calling a decode on the actual string and not on the encoded string. so your program is working properly if the initializations are correct as Elliot pointed.
for (int index = 0; index < encode.length; index++)
{
System.out.println("Encoded: " +ec.encode(list[index]));
System.out.println("Decoded: " +ec.decode(ec.encode(list[index])));
}
and that will give you your desired output
Ending up finding the answer after enough tinkering around with things and bouncing ideas off my roommates.
Changed this:
public String decode(String codedWord)
{
//Maps every character back 2 spaces w/ wrap around
String result = "";
for (int cnt = 0; cnt < codedWord.length(); cnt++)
{
result += backMap(codedWord.charAt(cnt));
}
decodedList[0] = result;
return result;
}
To this:
public String decode(String codedWord)
{
//Maps every character back 2 spaces w/ wrap around
String result = "";
for (int cnt = 0; cnt < codedWord.length(); cnt++)
{
result += backMap(encodedList[0].charAt(cnt));
}
decodedList[0] = result;
return result;
}

Java Tic Tac Toe AI not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My AI code for a computer player is not working as expected. I want it to play a random move, but block an opponent's incoming winning turn. However, it sometimes stops working altogether, and other times plays a specific move even though there is no incoming win combo. My computer AI code is below.
public void compturn()
{
count++ ;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
if (b1.getText() == b2.getText() && b2.getText() != "" && b3.getText() == "")
{
b3.setText(letter);
b3bool = false ;
}
else if (b4.getText() == b5.getText() && b6.getText() == "")
{
b6.setText(letter);
b6bool = false ;
}
else if (b7.getText() == b8.getText() && b9.getText() == "")
{
b9.setText(letter);
b9bool = false ;
}
else if (b1.getText() == b4.getText() && b4.getText() != "" && b7.getText() == "")
{
b7.setText(letter);
b7bool = false ;
}
else if (b2.getText() == b5.getText() && b8.getText() == "")
{
b8.setText(letter);
b8bool = false ;
}
else if (b3.getText() == b6.getText() && b9.getText() == "")
{
b9.setText(letter);
b9bool = false ;
}
else if (b1.getText() == b5.getText() && b5.getText() != "" && b9.getText() == "")
{
b9.setText(letter) ;
b9bool = false ;
}
else if (b3.getText() == b5.getText() && b7.getText() == "")
{
b7.setText(letter);
b7bool = false ;
}
else
{
randomMove();
}
}
public void randomMove()
{
int randomnum = (int)(Math.random() * 10);
if(randomnum == 1 )
{
if(b1.getText().equals("X") || b1.getText().equals("O"))
{
randomMove();
}
else
{
b1.setText(letter);
}
}
if(randomnum == 2 )
{
if(b2.getText().equals("X") || b2.getText().equals("O"))
{
randomMove();
}
else
{
b2.setText(letter);
}
}
if(randomnum == 3 )
{
if(b3.getText().equals("X") || b3.getText().equals("O"))
{
randomMove();
}
else
{
b3.setText(letter);
}
}
if(randomnum == 4 )
{
if(b4.getText().equals("X") || b4.getText().equals("O"))
{
randomMove();
}
else
{
b4.setText(letter);
}
}
if(randomnum == 5 )
{
if(b5.getText().equals("X") || b5.getText().equals("O"))
{
randomMove();
}
else
{
b5.setText(letter);
}
}
if(randomnum == 6 )
{
if(b6.getText().equals("X") || b6.getText().equals("O"))
{
randomMove();
}
else
{
b6.setText(letter);
}
}
if(randomnum == 7 )
{
if(b7.getText().equals("X") || b7.getText().equals("O"))
{
randomMove();
}
else
{
b7.setText(letter);
}
}
if(randomnum == 8 )
{
if(b8.getText().equals("X") || b8.getText().equals("O"))
{
randomMove();
}
else
{
b8.setText(letter);
}
}
if(randomnum == 9 )
{
if(b9.getText().equals("X") || b9.getText().equals("O"))
{
randomMove();
}
else
{
b9.setText(letter);
}
}
}
And my complete code for the main game is here
public int oneplayergame()
{
// ----=---------------------- CREATING ALL JBUTTONS ON THE GAME SCREEN AND ADDING ACTION LISTENERS AND REACTIONS -----------------------------------------
b1 = new JButton("");
b1.setToolTipText("Mark this box");
b1.setFont(TToeFont);
b1.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if (b1bool == true){ count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b1.setText(letter);
b1bool = false ;
calculatevictory();
compturn();
processturn();
}}});
b2 = new JButton("");
b2.setFont(TToeFont);
b2.setToolTipText("Mark this box");
b2.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if (b2bool == true){ count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b2.setText(letter);
b2bool = false ;
calculatevictory();
compturn();
processturn();
}}
});
b3 = new JButton("");
b3.setToolTipText("Mark this box");
b3.setFont(TToeFont);
b3.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if(b3bool == true){
count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b3.setText(letter);
b3bool = false ;
calculatevictory();
compturn();
processturn();
}}
});
b4 = new JButton("");
b4.setToolTipText("Mark this box");
b4.setFont(TToeFont);
b4.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if(b4bool == true){
count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b4.setText(letter);
b4bool = false ;
calculatevictory();
compturn();
processturn();
}}
});
b5 = new JButton("");
b5.setToolTipText("Mark this box");
b5.setFont(TToeFont);
b5.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if (b5bool == true){
count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b5.setText(letter);
b5bool = false ;
calculatevictory();
compturn();
processturn();
}}
});
b6 = new JButton("");
b6.setToolTipText("Mark this box");
b6.setFont(TToeFont);
b6.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if (b6bool == true){
count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b6.setText(letter);
b6bool = false ;
calculatevictory();
compturn();
processturn();
}}
});
b7 = new JButton("");
b7.setToolTipText("Mark this box");
b7.setFont(TToeFont);
b7.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if (b7bool == true){
count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b7.setText(letter);
b7bool = false ;
calculatevictory();
compturn();
processturn();
}}
});
b8 = new JButton("");
b8.setToolTipText("Mark this box");
b8.setFont(TToeFont);
b8.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if(b8bool == true){
count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b8.setText(letter);
b8bool = false ;
calculatevictory();
compturn();
processturn();
}}
});
b9 = new JButton("");
b9.setToolTipText("Mark this box");
b9.setFont(TToeFont);
b9.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent onclickb1)
{
if(b9bool == true){
count++;
if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
{
letter = "O" ;
}
else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
{
letter = "X" ;
}
b9.setText(letter);
b9bool = false ;
calculatevictory();
compturn();
processturn();
}}
});
//CREATING GAME SCREEN LAYOUT
//CREAING GAME SCREEN CONTENT PANE AND ADDING BUTTONS TO IT
// This method checks if someone won the game every time you click a button
public void calculatevictory(){
if (b1.getText() == b2.getText() && b2.getText() == b3.getText() && b1.getText() != "")
{
win = true ;
}
else if (b4.getText() == b5.getText() && b5.getText() == b6.getText() && b4.getText() != "")
{
win = true ;
}
else if (b7.getText() == b8.getText() && b8.getText() == b9.getText() && b7.getText() != "")
{
win = true ;
}
else if (b1.getText() == b4.getText() && b4.getText() == b7.getText() && b1.getText() != "")
{
win = true ;
}
else if (b2.getText() == b5.getText() && b5.getText() == b8.getText() && b2.getText() != "")
{
win = true ;
}
else if (b3.getText() == b6.getText() && b6.getText() == b9.getText() && b3.getText() != "")
{
win = true ;
}
else if (b1.getText() == b5.getText() && b5.getText() == b9.getText() && b1.getText() != "")
{
win = true ;
}
else if (b3.getText() == b5.getText() && b5.getText() == b7.getText() && b3.getText() != "")
{
win = true ;
}
else
{
win = false ;
}
}
// This method sends the win message
public void processturn()
}
// This method resets all game variables
public void resetgame()
{
count = 1 ;
b1bool = true ;
b2bool = true ;
b3bool = true ;
b4bool = true ;
b5bool = true ;
b6bool = true ;
b7bool = true ;
b8bool = true ;
b9bool = true ;
win = false ;
gameinit = true ;
b1.setText("");
b2.setText("");
b3.setText("");
b4.setText("");
b5.setText("");
b6.setText("");
b7.setText("");
b8.setText("");
b9.setText("");
}
public void compturn()
{
}
public void randomMove()
{
}
}
Welcome to AI... there are numerous ways you can do this. I'll give you a few tips.
I assume count is used to determine who's turn it is. I'd suggest you rather create an integer 'turn' and then simply do this:
turn = 1 -turn
...where you currently increment count. If it's 0 than it's o's turn, else x's. You can then remove those two long if statements. (You can also use a boolean flag and just not it each time, like so turn = !turn.)
Now the rest... I see you have no arrays. Are you comfortable using them?
If so, rather define, winning sequences. For example:
int[][] win = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}, {1,4,7}, ...}
Then all you do to determine if you can win, go is to see if you have two of your tokens in spot win[i][0], win[i][1] or win[i][2]. If the third spot it empty, you can win and should move there,
If you can't win, repeat the above process to defend.
If you do not have to defend and you cannot win, move in the centre. If the centre is taken, take a corner, else move random.
This will give you an AI that can still lose sometimes, but will show some intelligence. If you really want to build something unbeatable have a look at the minimax search algorithm.
Good luck!

How to calculate what employees at a factory are "on call" each day and night shift throughout the year?

I'm working on my first program in Java that will be used for a practical purpose and I'm stuck--getting an output that isn't correct.
Here is the problem I'm trying to compute: the manufacturing plant I work at operates 24/7, 363 days a year (off Christmas Eve and Christmas) in four day shifts. Each crew works 4 twelve hour days or nights in a row, then gets 4 days off. There are four crews, each works four days, then has four days off, then works four nights, then has four nights off. A Crew and B Crew rotate opposite of each other, and C Crew and D Crew rotate opposite of each other.
While A Crew and B Crew are working, members of C Crew and D Crew are on call in case someone on A or B Crew is absent. There are three classifications of employees: Senior operators, Junior Operators, and Compounders. On each crew, there are four Senior operators, three Junior operators, and 2 Compounders. For the first two days of a four day rotation cycle, three of the Senior operators are on call, while the fourth is not on call. The Senior operator who is not on call during this period rotates: one is not on call each time the others are on call. For the last two days, the three Junior operators are on call.
For the Senior employees, the previous cycle determines if they are on call for day shifts or night shifts: if the crew that is not working had been working day shifts, the senior operators are on call for the first two day shifts, or vice versa if they had been working nights during the previous cycle. For the Junior operators, the cycle they are about to begin dictates whether they are on call day shifts or night shifts for the second two days off.
The Compounders are on call for the first and fourth days off. Whether they are on call days or nights is determined in the same way as for operators.
My goal is to write a program in Java that calculates which employees are on call for each day and night shift, and output the results to a text file. I am an amateur that has only taken two programming classes thus far, so I am certain that what I have written so far is far less efficient than it could be. I am just trying to figure out why the output is incorrect. My suspicion is that I have either calculated a variable incorrectly or I am missing a piece of the puzzle altogether. Note that the first day of the year for the purposes of the program is January 3rd because the 1st and 2nd were part of the previous year's cycle.
Below is my code. I removed all getters and setters to fit within the allotted length. I appreciate any and all help that can be offered.
import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class OnCallAug812 {
Calendar cal1 = Calendar.getInstance();
private double cycleDay = ((cal1.get(Calendar.DAY_OF_YEAR) - 3)) % 16;
private double dayOfCycle = cycleDay % 4;
// number of days into current rotation ( 0 =4, 1=1, 2=2, 3=3)
private int cycleRotation = (int) (cycleDay / 4);
// number of current rotation (4 per cycle)
private int cycleOfYear = ((cal1.get(Calendar.DAY_OF_YEAR) - 3) / 16);
private int onCallCycle = cycleOfYear % 4;
// A crew operators: 4 Senior
private int a1S = 0;
private int a2S = 1;
private int a3S = 2;
private int a4S = 3;
// variables for A crew names
private String a1;
private String a2;
private String a3;
private String a4;
// A Crew Junior Operators represented by one variable b/c
// all are on call or not on call at same time
private int aJr;
// A crew junior operator names
private String aJr1;
private String aJr2;
private String aJr3;
// B crew operators: 4 Senior
private int b1S = 0;
private int b2S = 1;
private int b3S = 2;
private int b4S = 3;
// B crew operator names
private String b1;
private String b2;
private String b3;
private String b4;
// B Crew Junior Operators represented by one variable b/c
// all are on call or not on call at same time
private int bJr;
// B crew operator junior names
private String bJr1;
private String bJr2;
private String bJr3;
// C crew operators: 4 Senior
private int c1S = 0;
private int c2S = 1;
private int c3S = 2;
private int c4S = 3;
// C operator senior names
private String c1;
private String c2;
private String c3;
private String c4;
// C Crew Junior Operators represented by one variable b/c
// all are on call or not on call at same time
private int cJr;
// C crew junior operator names
private String cJr1;
private String cJr2;
private String cJr3;
// D crew operators: 4 Senior
private int d1S = 0;
private int d2S = 1;
private int d3S = 2;
private int d4S = 3;
// d crew senior names
private String d1;
private String d2;
private String d3;
private String d4;
// D Crew Junior Operators represented by one variable b/c
// all are on call or not on call at same time
private int dJr;
// D crew junior operator names
private String dJr1;
private String dJr2;
private String dJr3;
// Call status for each A Crew employee--set to 0,1, or 2
// 0 = on call day shift
// 1 = on call night shift
// 2 = off call
private int a1SCallStat = -1;
private int a2SCallStat = -1;
private int a3SCallStat = -1;
private int a4SCallStat = -1;
private int aJrCallStat = -1;
// Call status for each B Crew employee--set to 0,1, or 2
private int b1SCallStat = -1;
private int b2SCallStat = -1;
private int b3SCallStat = -1;
private int b4SCallStat = -1;
private int bJrCallStat = -1;
// Call status for each C Crew employee--set to 0,1, or 2
private int c1SCallStat = -1;
private int c2SCallStat = -1;
private int c3SCallStat = -1;
private int c4SCallStat = -1;
private int cJrCallStat = -1;
// Call status for each D Crew employee--set to 0,1, or 2
private int d1SCallStat = -1;
private int d2SCallStat = -1;
private int d3SCallStat = -1;
private int d4SCallStat = -1;
private int dJrCallStat = -1;
// Call status for each crew's pelletizer operators (both on or both off)
private int aP;
private int bP;
private int cP;
private int dP;
private String aP1;
private String aP2;
private String bP1;
private String bP2;
private String cP1;
private String cP2;
private String dP1;
private String dP2;
private int aPCallStat = -1;
private int bPCallStat = -1;
private int cPCallStat = -1;
private int dPCallStat = -1;
public int determineCall(double onCallCycle, int crewNumber) {
double aNumber = (crewNumber + onCallCycle) % 7;
if (aNumber <= 2) {
return 0;
} else if (aNumber >= 3 && aNumber < 6) {
return 1;
} else if (aNumber == 6) {
return 2;
} else
return -1;
}
public int determineCallJr(double onCallCycle, int crewNumber){
double aNumber = (crewNumber + onCallCycle) % 7;
if (aNumber <= 3) {
return 0;
} else if (aNumber >= 4 && aNumber <= 6) {
return 1;
} else
return -1;
}
public int determineCallP(double onCallCycle, int crewNumber){
double aNumber = (crewNumber + onCallCycle) % 7;
if (aNumber <= 3) {
return 0;
} else if (aNumber >= 4 && aNumber <= 6) {
return 1;
} else
return -1;
}
public void calcCall(OnCallAug812 aug1) {
// Senior employees on call checks
if ((aug1.cycleRotation == 0)
&& ((aug1.dayOfCycle == 1) || (aug1.dayOfCycle == 0))) {
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC1S()) == 0) {
aug1.setC1SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC1S()) == 1) {
aug1.setC1SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC1S()) == 2) {
aug1.setC1SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC2S()) == 0) {
aug1.setC2SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC2S()) == 1) {
aug1.setC2SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC2S()) == 2) {
aug1.setC2SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC3S()) == 0) {
aug1.setC3SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC3S()) == 1) {
aug1.setC3SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC3S()) == 2) {
aug1.setC3SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC4S()) == 0) {
aug1.setC4SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC4S()) == 1) {
aug1.setC4SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC4S()) == 2) {
aug1.setC4SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD1S()) == 0) {
aug1.setD1SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD1S()) == 1) {
aug1.setD1SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD1S()) == 2) {
aug1.setD1SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD2S()) == 0) {
aug1.setD2SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD2S()) == 1) {
aug1.setD2SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD2S()) == 2) {
aug1.setD2SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD3S()) == 0) {
aug1.setD3SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD3S()) == 1) {
aug1.setD3SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD3S()) == 2) {
aug1.setD3SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD4S()) == 0) {
aug1.setD4SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD4S()) == 1) {
aug1.setD4SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD4S()) == 2) {
aug1.setD4SCallStat(2);
}
}
if ((aug1.cycleRotation == 1)
&& ((aug1.dayOfCycle == 1) || (aug1.dayOfCycle == 0))) {
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA1S()) == 0) {
aug1.setA1SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA1S()) == 1) {
aug1.setA1SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA1S()) == 2) {
aug1.setA1SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA2S()) == 0) {
aug1.setA2SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA2S()) == 1) {
aug1.setA2SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA2S()) == 2) {
aug1.setA2SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA3S()) == 0) {
aug1.setA3SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA3S()) == 1) {
aug1.setA3SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA3S()) == 2) {
aug1.setA3SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA4S()) == 0) {
aug1.setA4SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA4S()) == 1) {
aug1.setA4SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA4S()) == 2) {
aug1.setA4SCallStat(2);
}
// b crew second rotation
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB1S()) == 0) {
aug1.setB1SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB1S()) == 1) {
aug1.setB1SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB1S()) == 2) {
aug1.setB1SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB2S()) == 0) {
aug1.setB2SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB2S()) == 1) {
aug1.setB2SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB2S()) == 2) {
aug1.setB2SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB3S()) == 0) {
aug1.setB3SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB3S()) == 1) {
aug1.setB3SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB3S()) == 2) {
aug1.setB3SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB4S()) == 0) {
aug1.setB4SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB4S()) == 1) {
aug1.setB4SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB4S()) == 2) {
aug1.setB4SCallStat(2);
}
}
if ((aug1.cycleRotation == 2)
&& ((aug1.dayOfCycle == 1) || (aug1.dayOfCycle == 0))) {
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC1S()) == 0) {
aug1.setC1SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC1S()) == 1) {
aug1.setC1SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC1S()) == 2) {
aug1.setC1SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC2S()) == 0) {
aug1.setC2SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC2S()) == 1) {
aug1.setC2SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC2S()) == 2) {
aug1.setC2SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC3S()) == 0) {
aug1.setC3SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC3S()) == 1) {
aug1.setC3SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC3S()) == 2) {
aug1.setC3SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC4S()) == 0) {
aug1.setC4SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC4S()) == 1) {
aug1.setC4SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getC4S()) == 2) {
aug1.setC4SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD1S()) == 0) {
aug1.setD1SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD1S()) == 1) {
aug1.setD1SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD1S()) == 2) {
aug1.setD1SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD2S()) == 0) {
aug1.setD2SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD2S()) == 1) {
aug1.setD2SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD2S()) == 2) {
aug1.setD2SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD3S()) == 0) {
aug1.setD3SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD3S()) == 1) {
aug1.setD3SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD3S()) == 2) {
aug1.setD3SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD4S()) == 0) {
aug1.setD4SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD4S()) == 1) {
aug1.setD4SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getD4S()) == 2) {
aug1.setD4SCallStat(2);
}
}
if ((aug1.cycleRotation == 3)
&& ((aug1.dayOfCycle == 1) || (aug1.dayOfCycle == 0))) {
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA1S()) == 0) {
aug1.setA1SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA1S()) == 1) {
aug1.setA1SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA1S()) == 2) {
aug1.setA1SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA2S()) == 0) {
aug1.setA2SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA2S()) == 1) {
aug1.setA2SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA2S()) == 2) {
aug1.setA2SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA3S()) == 0) {
aug1.setA3SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA3S()) == 1) {
aug1.setA3SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA3S()) == 2) {
aug1.setA3SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA4S()) == 0) {
aug1.setA4SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA4S()) == 1) {
aug1.setA4SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getA4S()) == 2) {
aug1.setA4SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB1S()) == 0) {
aug1.setB1SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB1S()) == 1) {
aug1.setB1SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB1S()) == 2) {
aug1.setB1SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB2S()) == 0) {
aug1.setB2SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB2S()) == 1) {
aug1.setB2SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB2S()) == 2) {
aug1.setB2SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB3S()) == 0) {
aug1.setB3SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB3S()) == 1) {
aug1.setB3SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB3S()) == 2) {
aug1.setB3SCallStat(2);
}
if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB4S()) == 0) {
aug1.setB4SCallStat(0);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB4S()) == 1) {
aug1.setB4SCallStat(1);
} else if (aug1.determineCall(aug1.getOnCallCycle(), aug1.getB4S()) == 2) {
aug1.setB4SCallStat(2);
}
}
// Junior Extruder Operators
if ((aug1.cycleRotation == 0)
&& ((aug1.dayOfCycle == 3) || (aug1.dayOfCycle == 2))) {
if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getcJr()) == 0) {
aug1.setcJrCallStat(0);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getcJr()) == 1) {
aug1.setcJrCallStat(1);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getcJr()) == 2) {
aug1.setcJrCallStat(2);
}
if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getdJr()) == 0) {
aug1.setDjrCallStat(1);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getdJr()) == 1) {
aug1.setDjrCallStat(0);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getdJr()) == 2) {
aug1.setDjrCallStat(2);
}
}
if ((aug1.cycleRotation == 1)
&& ((aug1.dayOfCycle == 3) || (aug1.dayOfCycle == 2))) {
if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getaJr()) == 0) {
aug1.setaJrCallStat(0);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getaJr()) == 1) {
aug1.setaJrCallStat(1);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getaJr()) == 2) {
aug1.setaJrCallStat(2);
}
if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getbJr()) == 0) {
aug1.setbJrCallStat(1);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getbJr()) == 1) {
aug1.setbJrCallStat(0);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getbJr()) == 2) {
aug1.setbJrCallStat(2);
}
}
if ((aug1.cycleRotation == 2)
&& ((aug1.dayOfCycle == 3) || (aug1.dayOfCycle == 2))) {
if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getcJr()) == 0) {
aug1.setcJrCallStat(0);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getcJr()) == 1) {
aug1.setcJrCallStat(1);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getcJr()) == 2) {
aug1.setcJrCallStat(2);
}
if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getdJr()) == 0) {
aug1.setDjrCallStat(1);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getdJr()) == 1) {
aug1.setDjrCallStat(0);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getdJr()) == 2) {
aug1.setDjrCallStat(2);
}
}
if ((aug1.cycleRotation == 3)
&& ((aug1.dayOfCycle == 3) || (aug1.dayOfCycle == 2))) {
if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getaJr()) == 0) {
aug1.setaJrCallStat(0);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getaJr()) == 1) {
aug1.setaJrCallStat(1);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getaJr()) == 2) {
aug1.setaJrCallStat(2);
}
if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getbJr()) == 0) {
aug1.setbJrCallStat(1);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getbJr()) == 1) {
aug1.setbJrCallStat(0);
} else if (aug1.determineCallJr(aug1.getOnCallCycle(), aug1.getbJr()) == 2) {
aug1.setbJrCallStat(2);
}
}
if ((aug1.cycleRotation == 0)
&& ((aug1.dayOfCycle == 3) || (aug1.dayOfCycle == 0))) {
if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getcP()) == 0) {
aug1.setcPCallStat(0);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getcP()) == 1) {
aug1.setcPCallStat(1);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getcP()) == 2) {
aug1.setcPCallStat(2);
}
if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getdP()) == 0) {
aug1.setdPCallStat(1);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getdP()) == 1) {
aug1.setdPCallStat(0);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getdP()) == 2) {
aug1.setdPCallStat(2);
}
}
if ((aug1.cycleRotation == 1)
&& ((aug1.dayOfCycle == 3) || (aug1.dayOfCycle == 0))) {
if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getaP()) == 0) {
aug1.setaPCallStat(0);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getaP()) == 1) {
aug1.setaPCallStat(1);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getaP()) == 2) {
aug1.setaPCallStat(2);
}
if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getbP()) == 0) {
aug1.setbPCallStat(1);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getbP()) == 1) {
aug1.setbPCallStat(0);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getbP()) == 2) {
aug1.setbPCallStat(2);
}
}
if ((aug1.cycleRotation == 2)
&& ((aug1.dayOfCycle == 3) || (aug1.dayOfCycle == 0))) {
if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getcP()) == 0) {
aug1.setcPCallStat(1);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getcP()) == 1) {
aug1.setcPCallStat(0);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getcP()) == 2) {
aug1.setcPCallStat(2);
}
if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getdP()) == 0) {
aug1.setdPCallStat(0);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getdP()) == 1) {
aug1.setdPCallStat(1);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getdP()) == 2) {
aug1.setdPCallStat(2);
}
}
if ((aug1.cycleRotation == 3)
&& ((aug1.dayOfCycle == 3) || (aug1.dayOfCycle == 0))) {
if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getaP()) == 0) {
aug1.setaPCallStat(1);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getaP()) == 1) {
aug1.setaPCallStat(0);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getaP()) == 2) {
aug1.setaPCallStat(2);
}
if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getbP()) == 0) {
aug1.setbPCallStat(0);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getbP()) == 1) {
aug1.setbPCallStat(1);
} else if (aug1.determineCallP(aug1.getOnCallCycle(), aug1.getbP()) == 2) {
aug1.setbPCallStat(2);
}
}
}
private String readFile(String pathname) throws Exception {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int) file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");
try {
while (scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
}
}
public void check0(PrintWriter output2, int callStat, String name) {
if (callStat == 0) {
output2.print(name);
output2.println();
}
}
public void check1(PrintWriter output2, int callStat, String name) {
if (callStat == 1) {
output2.print(name);
output2.println();
}
}
public void addDate(OnCallAug812 aug1) {
cal1.add(Calendar.DATE, 1);
aug1.cycleDay = ((cal1.get(Calendar.DAY_OF_YEAR) - 3)) % 16;
aug1.dayOfCycle = cycleDay % 4;
// number of days into current rotation ( 0 =4, 1=1, 2=2, 3=3)
aug1.cycleRotation = (int) (cycleDay / 4);
// number of current rotation (4 per cycle)
aug1.cycleOfYear = ((cal1.get(Calendar.DAY_OF_YEAR) - 2) / 16);
aug1.onCallCycle = cycleOfYear % 4;
}
public static void main(String[] args) throws Exception {
OnCallAug812 aug1 = new OnCallAug812();
aug1.calcCall(aug1);
System.out.print("Day of 16 day rotation(0 to 15): ");
System.out.println(aug1.cycleDay);
System.out.print("Day of 4 day cycle (0 to 3): ");
System.out.println(aug1.dayOfCycle);
System.out.print("Cycle of 16 day rotation (0 to 3): ");
System.out.println(aug1.cycleRotation);
System.out.print("16 day cycle number (0 to 22): ");
System.out.println(aug1.cycleOfYear);
System.out.print("On Call Cycle Number(0 to 6 then repeat): ");
System.out.println(aug1.onCallCycle);
}
public OnCallAug812() {
super();
}
}
This is a good OOP project for beginners. That being said you need to design this in an oop style. The first steps in doing this correctly and even getting close to getting the correct answer to design this program using polymorphism.
A) The first step would be to create a class Employee then create sub classes Senior, Junior, and Compound. These classes would contain tracking information on that empolyess last actions (boolean oncall, boolean shift (day night),etc)
B) The next step would be to figure out how you would create a crew probably another class. The crew class can then track factors such as who worked last, the shift they work, who was not on call last etc. The class would contain groups of employees.
This would be the beginning of designing this program correctly. Once you get to this point if you’re still stuck post again and someone will help you move to the next stage.
if you need a example of how to do this you can look at my ProGauge project that does something similar using different types of devices ex: http://www.behance.net/gallery/ProGauge-Project-in-java-JSON-Data-management/4668415 or just find some java polymorphism exampels on the web!

Categories

Resources