Drawing the flag of Liberia I am working on this assignment to draw the flag of Liberia for my essentials of computer programming class.



I finished it but had extra time to kill and noticed that a couple similar blocks of code are not very concise and could be shortened with a loop, but right now I can't seem to form the code just right!

Below I have included one of the methods that I would like to shorten:

public static void flagOfLiberia(Graphics g) {
    Expo.setBackground(g,Expo.black);
    for (int d = 1; d &lt;= numDots; d++) {
        int x = Expo.random(0,1000);
        int y = Expo.random(0,650);

        if (x &lt; 300 &amp;&amp; y &lt; 300)
            Expo.setColor(g,Expo.darkBlue);
        else if (y &lt; 50)
            Expo.setColor(g,Expo.red);
        else if (y &lt; 100)
            Expo.setColor(g,Expo.white);
        else if (y &lt; 150)
            Expo.setColor(g,Expo.red);
        else if (y &lt; 200)
            Expo.setColor(g,Expo.white);
        else if (y &lt; 250)
            Expo.setColor(g,Expo.red);
        else if (y &lt; 300)
            Expo.setColor(g,Expo.white);
        else if (y &lt; 350)
            Expo.setColor(g,Expo.red);
        else if (y &lt; 400)
            Expo.setColor(g,Expo.white);
        else if (y &lt; 450)
            Expo.setColor(g,Expo.red);
        else if (y &lt; 500)
            Expo.setColor(g,Expo.white);
        else if (y &lt; 550)
            Expo.setColor(g,Expo.red);
        else if (y &lt; 600)
            Expo.setColor(g,Expo.white);
        else if (y &lt; 650)
            Expo.setColor(g,Expo.red);

        drawDot(g,x,y);
        Expo.setColor(g,Expo.white);
        Expo.fillStar(g,150,150,100,5);
    }
    showName(g,"Liberia");
}
public static void flagOfLiberia(Graphics g) {
    Expo.setBackground(g,Expo.black);
    for (int d = 1; d &lt;= numDots; d++) {
        int x = Expo.random(0,1000);
        int y = Expo.random(0,650);

        if (x &lt; 300 &amp;&amp; y &lt; 300)
            Expo.setColor(g,Expo.darkBlue);
        else if (y % 100 &lt; 50)
            Expo.setColor(g,Expo.red);
        else
            Expo.setColor(g,Expo.white);

        drawDot(g,x,y);    // must be run inside the loop
    }
    Expo.setColor(g,Expo.white);
    Expo.fillStar(g,150,150,100,5);
    showName(g,"Liberia");
}


The above should be at least a little improvement. I used a modulus (%) to check every multiple of 100, seeing if the remainder is less than or greater than 50. If you desire a cutoff at 650, then a single if statement can be added before the modulus to check that y is below the threshold. Also, I placed the star after the for loop in order to execute it only once.