1
\$\begingroup\$

I'm wondering how I could condense the following (redundant on many levels) code:

import java.util.*;

public class Randomstems {

    private static final STEM[] allStemsW1 = new STEM[25], allStemsW2 = new STEM[25], allStemsW3 = new STEM[25];

    public static void main(String[] args) {
        Scanner getAnswer = new Scanner(System.in);
        Random getRandomSTEM = new Random();

        allStemsW1[0] = new STEM("ante", "before", 1);
        allStemsW1[1] = new STEM("anti", "against", 2);
        allStemsW1[2] = new STEM("bi", "two", 3);
        allStemsW1[3] = new STEM("circum", "around", 4);
        allStemsW1[4] = new STEM("com", "together", 5);

        allStemsW1[5] = new STEM("con", "together", 6);
        allStemsW1[6] = new STEM("de", "down", 7);
        allStemsW1[7] = new STEM("dis", "away", 8);
        allStemsW1[8] = new STEM("equi", "equal", 9);
        allStemsW1[9] = new STEM("extra", "beyond", 10);

        allStemsW1[10] = new STEM("inter", "between", 11);
        allStemsW1[11] = new STEM("intra", "within", 12);
        allStemsW1[12] = new STEM("intro", "into", 13);
        allStemsW1[13] = new STEM("mal", "bad", 14);
        allStemsW1[14] = new STEM("mis", "bad", 15);

        allStemsW1[15] = new STEM("non", "not", 16);
        allStemsW1[16] = new STEM("post", "after", 17);
        allStemsW1[17] = new STEM("pre", "before", 18);
        allStemsW1[18] = new STEM("semi", "half", 19);
        allStemsW1[19] = new STEM("sub", "under", 20);

        allStemsW1[20] = new STEM("super", "over", 21);
        allStemsW1[21] = new STEM("syn", "together", 22);
        allStemsW1[22] = new STEM("sym", "together", 23);
        allStemsW1[23] = new STEM("tri", "three", 24);
        allStemsW1[24] = new STEM("un", "not", 25);

        // week 2
        allStemsW2[0] = new STEM("ad", "to", 1);
        allStemsW2[1] = new STEM("antropo", "man", 2);
        allStemsW2[2] = new STEM("archy", "government", 3);
        allStemsW2[3] = new STEM("ard", "always", 4);
        allStemsW2[4] = new STEM("aqua", "water", 5);

        allStemsW2[5] = new STEM("auto", "self", 6);
        allStemsW2[6] = new STEM("audi", "hear", 7);
        allStemsW2[7] = new STEM("bell", "war", 8);
        allStemsW2[8] = new STEM("biblio", "book", 9);
        allStemsW2[9] = new STEM("bio", "life", 10);

        allStemsW2[10] = new STEM("cap", "take", 11);
        allStemsW2[11] = new STEM("cede", "go", 12);
        allStemsW2[12] = new STEM("cent", "one hundred", 13);
        allStemsW2[13] = new STEM("centri", "center", 14);
        allStemsW2[14] = new STEM("cide", "kill", 15);

        allStemsW2[15] = new STEM("cise", "cut", 16);
        allStemsW2[16] = new STEM("cred", "believe", 17);
        allStemsW2[17] = new STEM("dict", "say", 18);
        allStemsW2[18] = new STEM("ician", "specialist", 19);
        allStemsW2[19] = new STEM("itis", "inflammation", 20);

        allStemsW2[20] = new STEM("logy", "science", 21);
        allStemsW2[21] = new STEM("miss", "send", 22);
        allStemsW2[22] = new STEM("neo", "new", 23);
        allStemsW2[23] = new STEM("port", "carry", 24);
        allStemsW2[24] = new STEM("scrib", "writer", 25);

        // week 3
        allStemsW1[0] = new STEM("duct", "lead", 1);
        allStemsW1[1] = new STEM("ex", "out", 2);
        allStemsW1[2] = new STEM("fer", "carry", 3);
        allStemsW1[3] = new STEM("hema", "blood", 4);
        allStemsW1[4] = new STEM("homo", "same", 5);

        allStemsW1[5] = new STEM("hydro", "water", 6);
        allStemsW1[6] = new STEM("hypo", "under", 7);
        allStemsW1[7] = new STEM("micro", "small", 8);
        allStemsW1[8] = new STEM("mono", "one", 9);
        allStemsW1[9] = new STEM("neuro", "nerve", 10);

        allStemsW1[10] = new STEM("omni", "all", 11);
        allStemsW1[11] = new STEM("pan", "all", 12);
        allStemsW1[12] = new STEM("pend", "hang", 13);
        allStemsW1[13] = new STEM("penta", "five", 14);
        allStemsW1[14] = new STEM("phon", "sound", 15);

        allStemsW1[15] = new STEM("photo", "light", 16);
        allStemsW1[16] = new STEM("poly", "many", 17);
        allStemsW1[17] = new STEM("proto", "first", 18);
        allStemsW1[18] = new STEM("pseudo", "false", 19);
        allStemsW1[19] = new STEM("re", "again", 20);

        allStemsW1[20] = new STEM("spec", "look", 21);
        allStemsW1[21] = new STEM("tele", "far", 22);
        allStemsW1[22] = new STEM("tomy", "cut", 23);
        allStemsW1[23] = new STEM("vid", "look", 24);
        allStemsW1[24] = new STEM("viv", "life", 25);

        // starting the 'test'
        // total stuff
        int totalCorrect = 0;

        // 1st group of 25
        int allCorrect1 = 0;
        int didBefore1 = 0;
        boolean stop1 = false;
        while(!stop1) {
            int amCorrect1 = 0;
            didBefore1++;
            if(didBefore1>1) {
                for(int i=0; i<25; i++) { // reset
                    allStemsW1[i].reset();
                }
            }
            int[] incorrectAnswers = new int[25];
            for(int i=0; i<25; i++) {
                int randoStem = getRandomSTEM.nextInt(allStemsW1.length);
                if(i!=0) {
                    boolean needTo = true;
                    while(needTo) {
                        if(allStemsW1[randoStem].hasDoneThis) {
                            randoStem = getRandomSTEM.nextInt(allStemsW1.length);
                        }
                        for(int n=0; n<25; n++) {
                            if(allStemsW1[randoStem].hasDoneThis) {
                                randoStem = getRandomSTEM.nextInt(allStemsW1.length);
                                n = 25;
                            } else {
                                needTo = false;
                            }
                        }
                    }
                }
                STEM randomStem = allStemsW1[randoStem];
                randomStem.didThis();
                System.out.println("Please type the meaning of the STEM \"" + randomStem.getName() + "\" below:");
                String answer = getAnswer.nextLine();
                randomStem.answerQ(answer, (25-1-i));
                if(randomStem.isCorrect) {
                    randomStem.isCorrect = false;
                    amCorrect1++;
                } else {
                    randomStem.isCorrect = false;
                    incorrectAnswers[i] = i+1;
                }
            }
            int points = amCorrect1*4;
            System.out.println("-----------------------");
            System.out.println("Results:");
            System.out.println("You got " + amCorrect1 + "/25 right! You got " + points + " points!");
            allCorrect1 += amCorrect1;
            if(points != 100) {
                for(int i=0; i<incorrectAnswers.length; i++) {
                    if(incorrectAnswers[i] != 0) { // ACTUALLY got WRONG
                        System.out.print("you got " + allStemsW1[i].getName() + " wrong, ");
                    } else if(i==(incorrectAnswers.length-1)) {
                        System.out.println(""); // end the ^^^^ print
                    }
                }
            }
            if(points == 100 && didBefore1>1) {
                stop1 = true;
            } else if(points != 100 && didBefore1>1) {
                System.out.println("Do you wish to try to get 100 again? (true/false)");
                boolean yN = getAnswer.nextBoolean();
                if(!yN && didBefore1!=0) {
                    stop1 = true;
                }
            }
        }
        System.out.println("-------------------------------------");
        System.out.println("Results:");
        System.out.println("You got " + allCorrect1 + "/" + (didBefore1*25) + " correct!");
        totalCorrect+=allCorrect1;

        // 2nd group of 25
        int allCorrect2 = 0;
        int didBefore2 = 0;
        boolean stop2 = false;
        while(!stop2) {
            int amCorrect2 = 0;
            didBefore2++;
            if(didBefore2>1) {
                for(int i=0; i<25; i++) { // reset
                    allStemsW2[i].reset();
                }
            }
            int[] incorrectAnswers = new int[25];
            for(int i=0; i<25; i++) {
                int randoStem = getRandomSTEM.nextInt(allStemsW2.length);
                if(i!=0) {
                    boolean needTo = true;
                    while(needTo) {
                        if(allStemsW2[randoStem].hasDoneThis) {
                            randoStem = getRandomSTEM.nextInt(allStemsW2.length);
                        }
                        for(int n=0; n<25; n++) {
                            if(allStemsW2[randoStem].hasDoneThis) {
                                randoStem = getRandomSTEM.nextInt(allStemsW2.length);
                                n = 25;
                            } else {
                                needTo = false;
                            }
                        }
                    }
                }
                STEM randomStem = allStemsW2[randoStem];
                randomStem.didThis();
                System.out.println("Please type the meaning of the STEM \"" + randomStem.getName() + "\" below:");
                String answer = getAnswer.nextLine();
                randomStem.answerQ(answer, (25-1-i));
                if(randomStem.isCorrect) {
                    randomStem.isCorrect = false;
                    amCorrect2++;
                } else {
                    randomStem.isCorrect = false;
                    incorrectAnswers[i] = i+1;
                }
            }
            int points = amCorrect2*2;
            System.out.println("-----------------------");
            System.out.println("Results:");
            System.out.println("You got " + amCorrect2 + "/25 right! You got " + points + " points!");
            allCorrect2 += amCorrect2;
            if(points != 100) {
                for(int i=0; i<incorrectAnswers.length; i++) {
                    if(incorrectAnswers[i] != 0) { // ACTUALLY got WRONG
                        System.out.print("you got " + allStemsW2[i].getName() + " wrong, ");
                    } else if(i==(incorrectAnswers.length-1)) {
                        System.out.println(""); // end the ^^^^ print
                    }
                }
            }
            if(points == 100 && didBefore2>1) {
                stop2 = true;
            } else if(points != 100 && didBefore2>1) {
                System.out.println("Do you wish to try to get 100 again? (true/false)");
                boolean yN = getAnswer.nextBoolean();
                if(!yN && didBefore2!=0) {
                    stop2 = true;
                }
            }
        }
        System.out.println("-------------------------------------");
        System.out.println("Results:");
        System.out.println("You got " + allCorrect2 + "/" + (didBefore2*25) + " correct!");
        totalCorrect+=allCorrect2;

        // 3rd group of 25
        int allCorrect3 = 0;
        int didBefore3 = 0;
        boolean stop3 = false;
        while(!stop3) {
            int amCorrect3 = 0;
            didBefore3++;
            if(didBefore3>1) {
                for(int i=0; i<25; i++) { // reset
                    allStemsW3[i].reset();
                }
            }
            int[] incorrectAnswers = new int[25];
            for(int i=0; i<25; i++) {
                int randoStem = getRandomSTEM.nextInt(allStemsW3.length);
                if(i!=0) {
                    boolean needTo = true;
                    while(needTo) {
                        if(allStemsW3[randoStem].hasDoneThis) {
                            randoStem = getRandomSTEM.nextInt(allStemsW3.length);
                        }
                        for(int n=0; n<25; n++) {
                            if(allStemsW3[randoStem].hasDoneThis) {
                                randoStem = getRandomSTEM.nextInt(allStemsW3.length);
                                n = 25;
                            } else {
                                needTo = false;
                            }
                        }
                    }
                }
                STEM randomStem = allStemsW3[randoStem];
                randomStem.didThis();
                System.out.println("Please type the meaning of the STEM \"" + randomStem.getName() + "\" below:");
                String answer = getAnswer.nextLine();
                randomStem.answerQ(answer, (25-1-i));
                if(randomStem.isCorrect) {
                    randomStem.isCorrect = false;
                    amCorrect3++;
                } else {
                    randomStem.isCorrect = false;
                    incorrectAnswers[i] = i+1;
                }
            }
            int points = amCorrect3*2;
            System.out.println("-----------------------");
            System.out.println("Results:");
            System.out.println("You got " + amCorrect3 + "/25 right! You got " + points + " points!");
            allCorrect3 += amCorrect3;
            if(points != 100) {
                for(int i=0; i<incorrectAnswers.length; i++) {
                    if(incorrectAnswers[i] != 0) { // ACTUALLY got WRONG
                        System.out.print("you got " + allStemsW3[i].getName() + " wrong, ");
                    } else if(i==(incorrectAnswers.length-1)) {
                        System.out.println(""); // end the ^^^^ print
                    }
                }
            }
            if(points == 100 && didBefore3>1) {
                stop3 = true;
            } else if(points != 100 && didBefore3>1) {
                System.out.println("Do you wish to try to get 100 again? (true/false)");
                boolean yN = getAnswer.nextBoolean();
                if(!yN && didBefore3!=0) {
                    stop3 = true;
                }
            }
        }
        System.out.println("-------------------------------------");
        System.out.println("Results:");
        System.out.println("You got " + allCorrect3 + "/" + (didBefore3*25) + " correct!");
        totalCorrect+=allCorrect3;

        // total stuff
        System.out.println("-------------------------------------");
        System.out.println("Total results:");
        System.out.println("You got " + totalCorrect + "/100" + " correct!");

        getAnswer.close();
    }
}

I know that it's a lot of code, but it's basically the same thing 3 times over. I originally tried a condenser way, but it threw many errors. That was also before I had 3 different STEMs lists.

EDIT:
Here's what I get in the console (it works...shortened version):

// each one is done at least 50 times
Please type the meaning of the STEM "(random STEM from week 1)" below:
Please type the meaning of the STEM "(random STEM from week 2)" below:
Please type the meaning of the STEM "(random STEM from week 3)" below:

If you get it correct, it says correct. It keeps track of how many you got correct and, at the end of each week and at the end of it all, it says what your total grade was.

\$\endgroup\$
  • \$\begingroup\$ Can you include the STEM class? It would make it easier to see what it is needed for. \$\endgroup\$ – spyr03 Aug 26 '18 at 14:20
1
\$\begingroup\$
  1. Instead of having three different lists, have a list of lists, one per week with the quiz questions. This would get rid of redundant code and make it trivial to add a week.

  2. keep your data in some kind of file, then load it into a list. This will get rid of all of those constructors in your main function, as well as making it easier to edit your data, or rewrite your program in a different language.

  3. Abstract things into classes and methods. I can't really understand your code now because I need to keep remember every variable in the entire function and keep track of deeply nested blocks. At the very least abstract out: getting a random stem, asking a stem, and asking an arbitrary number of stems. In my opinion, decomposing a problem into smaller problems is the most important part of programming.

\$\endgroup\$
  • \$\begingroup\$ i get this, but...only the first one is truly something I could do because of my format. See my edit. \$\endgroup\$ – TheJavaNub Aug 26 '18 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.