Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java question For the following questions, you may assume that both java.awt.Col

ID: 3847800 • Letter: J

Question

java question

For the following questions, you may assume that both java.awt.Color and SimpleCanvas have been imported. Write the method flip that takes a colour as its argument. If the argument is white, it returns black, if the argument is black, it returns white, if the argument is anything else, it throws an Illegal Argument Exception.//returns the opposite colour to col public Color flip (Color col) Use flip and the Simplecanvas method drawsq below to write the method drawNoland that draws the flag of Noland, as illustrated below. The flag must be square with side n, and the width of each stripe must be one-sixth of n.//draws a square of side s and colour c on its canvas, //with its top-left corner at x, y public void draws (int x, int y, int s, Color c)//draws the flag of Noland with side n public void drawNoland (int n)

Explanation / Answer

public Color flip(Color col) {
       if(col.equals(Color.WHITE))
           return Color.BLACK;
       if(col.equals(Color.BLACK))
           return Color.WHITE;
       throw new IllegalArgumentException();
   }
  
   public void drawNoland(int n) {
       drawRow(n/6, 0, new int[]{0,0,0,0,0,0});
       drawRow(n/6, n/6, new int[]{0,1,1,1,1,0});
       drawRow(n/6, n/3, new int[]{0,1,0,0,0,0});
       drawRow(n/6, n/2, new int[]{0,1,0,1,1,0});
       drawRow(n/6, 4*n/6, new int[]{0,1,0,1,0,0});
       drawRow(n/6, 5*n/6, new int[]{0,0,0,0,0,0});
   }
  
   /*
   * values contains color to be filled
   * 0 means black, 1 means white
   */
   public void drawRow(int side, int y, int values[]) {
       int x = 0;
       for(int v: values) {
           if(v==1)
               drawSq(x, y, side, Color.WHITE);
           else
               drawSq(x, y, side, Color.BLACK);
          
           x += side;
       }
   }