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

What is wrong with this code? It is supposed to be a working Sierpinski carpet.

ID: 3666589 • Letter: W

Question

What is wrong with this code? It is supposed to be a working Sierpinski carpet.

I would suggest putting this in eclipse to format it and see what is happening.

import java.util.*; import java.applet.*; import java.awt.*; public class SierpenskiCarpet extends Applet{ private Image display; private Graphics drawingArea; public void inti() { int height = getSize().height; int width = getSize().width; display = createImage(width,height); drawingArea = display.getGraphics(); //TODO draw carpet drawCarpet(0,0,height,drawingArea); } public void paint(Graphics g){ g.drawImage(display, 0, 0, null); } public static void drawCarpet(int x,int y, int side, Graphics g){ int sub = side/3; g.fillRect(x+sub, y+sub, sub, sub); if(sub>=3){ //Top 3 squares drawCarpet(x,y,sub,g);//Left drawCarpet(x+sub,y,sub,g);//Middle drawCarpet(x+sub*2,y,sub,g); //Middle 2 squares drawCarpet(x,y+sub,sub,g);//Left drawCarpet(x+sub*2,y+sub,sub,g);//Right //Bottom 3 squares drawCarpet(x,y+sub*2,sub,g);//Left drawCarpet(x+sub,y+sub*2,sub,g);//Middle drawCarpet(x+sub*2,y+sub*2,sub,g);//Right } } }

Explanation / Answer

There is nothing wrong with the code. It is compiling with no errors. I have formatted the code for you . Please try compiling the below code and tell me if you are facing problems any further.

import java.util.*;
import java.applet.*;
import java.awt.*;
public class SierpenskiCarpet extends Applet
{
private Image display;
private Graphics drawingArea;
public void inti()
{
int height = getSize().height;
int width = getSize().width;
display = createImage(width,height);
drawingArea = display.getGraphics(); //TODO draw carpet
drawCarpet(0,0,height,drawingArea);
}

public void paint(Graphics g)
   {
g.drawImage(display, 0, 0, null);
   }

public static void drawCarpet(int x,int y, int side, Graphics g)
   {
   int sub = side/3;
   g.fillRect(x+sub, y+sub, sub, sub);
   if(sub>=3)
       { //Top 3 squares
           drawCarpet(x,y,sub,g);//Left
           drawCarpet(x+sub,y,sub,g);//Middle
           drawCarpet(x+sub*2,y,sub,g); //Middle 2 squares
           drawCarpet(x,y+sub,sub,g);//Left
           drawCarpet(x+sub*2,y+sub,sub,g);//Right //Bottom 3 squares
           drawCarpet(x,y+sub*2,sub,g);//Left
           drawCarpet(x+sub,y+sub*2,sub,g);//Middle
           drawCarpet(x+sub*2,y+sub*2,sub,g);//Right
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote