patternjavaMinor
Drawing the flag of Liberia
Viewed 0 times
flagthedrawingliberia
Problem
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:
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 <= numDots; d++) {
int x = Expo.random(0,1000);
int y = Expo.random(0,650);
if (x < 300 && y < 300)
Expo.setColor(g,Expo.darkBlue);
else if (y < 50)
Expo.setColor(g,Expo.red);
else if (y < 100)
Expo.setColor(g,Expo.white);
else if (y < 150)
Expo.setColor(g,Expo.red);
else if (y < 200)
Expo.setColor(g,Expo.white);
else if (y < 250)
Expo.setColor(g,Expo.red);
else if (y < 300)
Expo.setColor(g,Expo.white);
else if (y < 350)
Expo.setColor(g,Expo.red);
else if (y < 400)
Expo.setColor(g,Expo.white);
else if (y < 450)
Expo.setColor(g,Expo.red);
else if (y < 500)
Expo.setColor(g,Expo.white);
else if (y < 550)
Expo.setColor(g,Expo.red);
else if (y < 600)
Expo.setColor(g,Expo.white);
else if (y < 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");
}Solution
public static void flagOfLiberia(Graphics g) {
Expo.setBackground(g,Expo.black);
for (int d = 1; d <= numDots; d++) {
int x = Expo.random(0,1000);
int y = Expo.random(0,650);
if (x < 300 && y < 300)
Expo.setColor(g,Expo.darkBlue);
else if (y % 100 < 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.Code Snippets
public static void flagOfLiberia(Graphics g) {
Expo.setBackground(g,Expo.black);
for (int d = 1; d <= numDots; d++) {
int x = Expo.random(0,1000);
int y = Expo.random(0,650);
if (x < 300 && y < 300)
Expo.setColor(g,Expo.darkBlue);
else if (y % 100 < 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");
}Context
StackExchange Code Review Q#126383, answer score: 7
Revisions (0)
No revisions yet.