I am trying to solve this CSES problem: Grid Paths. You are given a string of length 48, and you have to find the amount of paths such that you traverse all of the grid and end up at the lower left corner.
I believe I have pruned the search to the best of my ability, as according to this book: CP Handbook (Look in the pruning the search category), the best optimization for this type of problem is to prevent your path from closing yourself off, and I have already implemented this. The time limits for this specific problem are tight, and although I have basically solved this problem, I am still failing 1-2 test cases because my solution takes around 1.01 seconds instead of being below the 1 second time limit.
Finally, I just wanted to know if there were any cool micro-optimizations I could use to marginally enhance the speed of my java code, so I could actually pass all of the test cases for this problem.
import java.io.*;
public class GridPaths {
public static class FastIO {
InputStream dis;
byte[] buffer = new byte[1 << 17];
int pointer = 0;
public FastIO(String fileName) throws Exception {
dis = new FileInputStream(fileName);
}
public FastIO(InputStream is) {
dis = is;
}
int nextInt() throws Exception {
int ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
long nextLong() throws Exception {
long ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
Integer[] readArray(int n) throws Exception {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
byte nextByte() throws Exception {
if (pointer == buffer.length) {
dis.read(buffer, 0, buffer.length);
pointer = 0;
}
return buffer[pointer++];
}
String next() throws Exception {
StringBuilder ret = new StringBuilder();
byte b;
do {
b = nextByte();
} while (b <= ' ');
while (b > ' ') {
ret.appendCodePoint(b);
b = nextByte();
}
return ret.toString();
}
}
static char[] board;
static boolean[][] visited = new boolean[7][7];
static int ans = 0;
public static boolean works(int i, int j) {
//makes sure that current spot is on the 7x7 grid and is not visited
return (i >= 0 && i<=6 && j>=0 && j<=6 && !visited[i][j]);
}
public static void solve(int i, int j, int steps) {
if (i == 6 && j == 0) {
if (steps == 48) ans++; //all spots of the grid have to be visited in order to be counted as part of the answer
return;
}
visited[i][j] = true;
//you are given ? characters in the input string, and those mean that you have to try out all 4 combinations (U,D,L,R)
if (board[steps] == '?' || board[steps] == 'L') {
//second condition of the second if statement checks if the spot directly ahead of the current spot is blocked, and if it is, the left and right spots cannot both be unvisited or else you will not continue searching
if (works(i,j-1) && !(!works(i,j-2) && works(i+1,j-1) && works(i-1,j-1))) {
solve(i, j - 1, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'R') {
if (works(i,j+1) && !(!works(i,j+2) && works(i+1,j+1) && works(i-1,j+1))) {
solve(i, j + 1, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'U') {
if (works(i-1,j) && !(!works(i-2,j) && works(i-1,j+1) && works(i-1,j-1))) {
solve(i - 1, j, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'D') {
if (works(i+1,j) && !(!works(i+2,j) && works(i+1,j+1) && works(i+1,j-1))) {
solve(i + 1, j, steps + 1);
}
}
visited[i][j] = false;
}
public static void main(String[] args) throws Exception {
FastIO in = new FastIO(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
board = in.next().toCharArray();
solve(0,0,0);
out.println(ans);
out.close();
}
}
Note: I am already using one of the fastest, if not the fastest, ways to receive input in Java, so I do not believe I can actually improve upon that.
I've been playing around with this. In addition to using a standard mechanism for reading the input file (which I suggested in a comment), you can gain a little time in the search alg itself by doing two things:
Break the case board[steps] == '?' off from the other cases. So check for board[steps] == '?' first, and just try all four directions in that case. Otherwise (the else case for if (board[steps] == '?'), just check for U/D/L/R. Since for most steps, the character will be '?', you save having to make the U/D/L/R tests most of the time.
Look up the character to be tested once, with c = board[steps],and then use c in each test instead of board[steps].
Doing these two things saved about 5% it seems. I was doing 100 reps of the solve and timing with System.currentTimeMillis(). I know there are more accurate ways of timing, but this was good enough to see a definite improvement even though the times jumped around quite a bit trial to trial. The best I ever saw in each case was 3600 millis for 100 iterations as originally written vs 3400 millis with the improvements.
My guess is that it's mostly the first change that matters. I'd expect the compiler to be doing the second already, but I didn't try the two optimizations independently.
I also solved this problem in java (AC), and here is how I did it
public static char[] defaultPath;
public static boolean[][] isUsed;
public static int counter = 0;
public static void solve(int indexChar, int indexRow, int indexColumn) {
if (indexRow == 8 && indexColumn == 2) {
if (indexChar == 48) {
counter++;
}
}else {
// Find correct way: 'D', 'U', 'L', 'R'
char correctWay = '?';
// (1) (1)
// 0 1 or 1 0
// 1 1
if ((isUsed[indexRow+1][indexColumn+1] || isUsed[indexRow+1][indexColumn-1])
&& isUsed[indexRow+2][indexColumn] && !isUsed[indexRow+1][indexColumn]) {
correctWay = 'D';
}
// 1 1
// 0 1 or 1 0
// (1) (1)
else if ((isUsed[indexRow-1][indexColumn+1] || isUsed[indexRow-1][indexColumn-1])
&& !isUsed[indexRow-1][indexColumn] && isUsed[indexRow-2][indexColumn]) {
correctWay = 'U';
}
// 1 0 (1) or 1
// 1 1 0 (1)
else if ((isUsed[indexRow+1][indexColumn-1] || isUsed[indexRow-1][indexColumn-1])
&& !isUsed[indexRow][indexColumn-1] && isUsed[indexRow][indexColumn-2]) {
correctWay = 'L';
}
//(1) 0 1 or 1
// 1 (1) 0 1
else if ((isUsed[indexRow+1][indexColumn+1] || isUsed[indexRow-1][indexColumn+1])
&& !isUsed[indexRow][indexColumn+1] && isUsed[indexRow][indexColumn+2]) {
correctWay = 'R';
}
// Check input path (default path)
char c = defaultPath[indexChar];
if (c == '?') {
if (correctWay == '?') {
// 'D'
if (!isUsed[indexRow+1][indexColumn]) {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}
// 'U'
if (!isUsed[indexRow-1][indexColumn]) {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}
// 'L'
if (!isUsed[indexRow][indexColumn-1]) {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}
// 'R'
if (!isUsed[indexRow][indexColumn+1]) {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}else {
if (correctWay == 'D') {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}else if (correctWay == 'U') {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}else if (correctWay == 'L') {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}else if (correctWay == 'R') {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}
}else {
if (c == correctWay || correctWay == '?') {
if (c == 'D' && !isUsed[indexRow+1][indexColumn]) {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}else if (c == 'U' && !isUsed[indexRow-1][indexColumn]) {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}else if (c == 'L' && !isUsed[indexRow][indexColumn-1]) {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}else if (c == 'R' && !isUsed[indexRow][indexColumn+1]) {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
defaultPath = scanner.next().toCharArray();
isUsed = new boolean[11][11];
for (int i = 0; i < 11; i++) {
isUsed[0][i] = true;
isUsed[1][i] = true;
isUsed[9][i] = true;
isUsed[10][i] = true;
isUsed[i][0] = true;
isUsed[i][1] = true;
isUsed[i][9] = true;
isUsed[i][10] = true;
}
isUsed[2][2] = true;
isUsed[8][1] = false;
isUsed[9][2] = false;
solve(0, 2, 2);
System.out.println(counter);
scanner.close();
}
I am working on code for an assignment in which I have to make Wheel of Fortune, I am having trouble with building the string that will be shown to the user after each guess. I have the wheel spin working but I just can't seem to figure how to build the string. I can also answer any questions about this if you have any. Any help will be much appreciated. Here is my code so far:
class WheelOfFortune3 {
public static void main(String[] args) {
int result, location, newLocation, numGuess = 0;
String puzzle, guess;
String sub[] = new String[26];
for (int i = 0; i < sub.length; i++) {
sub[i] = "_";
}
result = wheelResult();
System.out.println("You spun $" + result);
puzzle = getPuzzle();
System.out.println(puzzle);
do {
guess = In.getString();
location = checkGuess(puzzle, guess);
if (location == -1) {
System.out.println("Incorrect guess");
}
if (location >= 0 && location <= 25) {
System.out.println("Correct guess");
newLocation = location + 1;
sub[numGuess] = puzzle.substring(location, newLocation);
for (int i = 0; i <= 10; i++) {
System.out.print(sub[i] + " ");
}
System.out.println("");
numGuess = numGuess + 1;
System.out.println(numGuess);
System.out.println(sub.length);
}
}
while (numGuess < sub.length);
}
public static int checkGuess(String puzzle, String guess) {
String word = puzzle;
int location;
location = word.indexOf(guess);
return location;
}
public static int wheelResult() {
int result;
int[] wheelSpin = new int[1];
wheelSpin[0] = (int)(24 * Math.random());
if (wheelSpin[0] == 1 || wheelSpin[0] == 18 || wheelSpin[0] == 22) {
result = 200;
return result;
} else if (wheelSpin[0] == 2 || wheelSpin[0] == 5) {
result = 900;
return result;
} else if (wheelSpin[0] == 0 || wheelSpin[0] == 3 || wheelSpin[0] == 15) {
result = 250;
return result;
} else if (wheelSpin[0] == 4 || wheelSpin[0] == 6 || wheelSpin[0] == 12 || wheelSpin[0] == 16) {
result = 300;
return result;
} else if (wheelSpin[0] == 7) {
result = 1500;
return result;
} else if (wheelSpin[0] == 9 || wheelSpin[0] == 11) {
result = 700;
return result;
} else if (wheelSpin[0] == 10 || wheelSpin[0] == 14 || wheelSpin[0] == 21) {
result = 500;
return result;
} else if (wheelSpin[0] == 13) {
result = 5000;
return result;
} else if (wheelSpin[0] == 23 || wheelSpin[0] == 19) {
result = 600;
return result;
} else if (wheelSpin[0] == 8 || wheelSpin[0] == 20) {
result = 100;
return result;
} else if (wheelSpin[0] == 17) {
result = 17;
return result;
}
return -1;
}
public static String getPuzzle() {
String puzzle;
//a long ride
return "A long ride";
//01234567890
}
}
This can be done using a List of Characters and the .contains() method like so:
List<Character> guessedCharacters = new ArrayList<>();
each time a player guesses a letter, add that letter to
the guessedCharacters List like this: guessedCharacters.add(char);
Then you can do something like this to generate the String to output to the player:
StringBuilder toShowSB = new StringBuilder();
for(int i=0,n=puzzle.length;i<n;i++){
if(guessedCharacters.contains(puzzle.charAt(i))){
toShowSB.append(puzzel.charAt(i));
}else{
toShowSB.append("_");
}
}
String toShow = toShowSB.toString();
This will create a String that holds all of the guessed letters in their places and an underscore denoting characters that have not been properly guessed yet.
I used Luhn algorithm but, for some reason all of the card numbers I try it always displays that the card number is valid even if its not.
This is my code so far I believe the issue is with the toString method but I have no idea on how to fix it. Please help!
public class CreditCard
{
private String number;
private String result;
private int sum = 0;
private int n = 0;
private boolean value = true;
private StringBuilder builder = new StringBuilder();
private String word = builder.toString();
public CreditCard(String number)
{
this.number = number;
}
public String toString()
{
for(int x = 0; x < number.length(); x++)
{
char c = number.charAt(x);
if(Character.isDigit(c)){
builder.append(c);
}
}
String result = builder.toString() + " was issued by " + getIssuer();
if(isValid())
result += " and is valid.";
else
result += " and is not valid.";
return result;
}
public String getIssuer()
{
if(builder.length() == 13 && builder.substring(0, 1).equals("4") )
result = "VISA";
else if(builder.length() == 14)
{
if(builder.substring(0, 2).equals("36") || builder.substring(0, 2).equals("38")
|| builder.substring(0, 3).equals("300") || builder.substring(0, 3).equals("301")
|| builder.substring(0, 3).equals("302") || builder.substring(0, 3).equals("303")
|| builder.substring(0, 3).equals("304") || builder.substring(0, 3).equals("305"))
result = "Diner's Club";
else
result = "Unknown";
}
else if(builder.length() == 15)
{
if(builder.substring(0, 2).equals("34") || builder.substring(0, 2).equals("37"))
result = "American Express";
else
result = "Unknown";
}
else if(builder.length() == 16)
{
if(builder.substring(0, 2).equals("51") || builder.substring(0, 2).equals("52")
|| builder.substring(0, 2).equals("53") || builder.substring(0, 2).equals("54")
|| builder.substring(0, 2).equals("55"))
{
result = "MasterCard";
}
else if(builder.substring(0, 4).equals("6011"))
{
result = "Discover";
}
else if(builder.substring(0, 1).equals("4"))
{
result = "VISA";
}
else
result = "Unknown";
}
else
result = "Unknown";
return result;
}
public boolean isValid()
{
if(word.length() % 2 == 0){
for(int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
int num = Character.getNumericValue(c);
if(i % 2 == 0)
{
n = num * 2;
if(n > 9)
{
n -=9;
}
sum +=n;
}
else
sum+= num;
}
}
else
{
for(int x = 0; x < word.length(); x++)
{
char c = word.charAt(x);
int num = Character.getNumericValue(c);
if(x % 2 != 0)
{
n = num * 2;
if(n > 9)
{
n -=9;
}
sum +=n;
}
else
sum += num;
}
}
if(sum % 10 == 0)
value = true;
else
value = false;
return value;
}
}
First of all you need to revise your method, I would put an arguments into getIssuer and isValid method. These method are public and they could be called from outside of the class and they return something, which is not correct.
If not possible, then they have to use number instance variable.
The code returns wrong results, because you are not setting the value of word variable, it is null.
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!