CheckerBoard.java

i can't understand this code

Page 1 of 1

11 Replies - 5571 Views - Last Post: 18 September 2009 - 09:27 AM Rate Topic: -----

#1 islam-morad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 09

CheckerBoard.java

Posted 18 September 2009 - 06:12 AM

here is a programming exercise which i can't understand it's code and i want u to help me understand it

the program should produce the following output
Posted Image

here is the code help me understand it (i just cant understand what the for nested loops are doing, the getheight and the x and y

public class checkerborad extends GraphicsProgram {
		private static final int NRows=8;
	private static final int NColumns=8;
	public void run() {
		int sqsize= getHeight()/NRows;
		for (int i = 0; i<NRows; i++) {
			for (int j = 0; j<NColumns; j++){
				int x = j*sqsize;
				int y = i*sqsize;
				GRect sq = new GRect (x, y, sqsize, sqsize);
				sq.setFilled(((i+j)%2) !=0);
				add(sq);
			}
		}
	}
}



thanks :^:

Is This A Good Question/Topic? 0
  • +

Replies To: CheckerBoard.java

#2 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: CheckerBoard.java

Posted 18 September 2009 - 06:28 AM

I think the getheight function is getting the total size of the screen from top to bottom.
The nested for loops are building the squares one square at a time (8 x 8). It looks like its
starting with the row and building the entire column for that one row, then does the next row and builds
the column for that one, etc.
I think the x and y fields are getting the top left corner coordinate to start building the one square, it needs to know where to start making the square.
Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: CheckerBoard.java

Posted 18 September 2009 - 06:30 AM

 for (int i = 0; i<NRows; i++) {
			for (int j = 0; j<NColumns; j++){



In the first round, the i will be 0, while j increases until j<NColumns. So it's like, at the first row, there are 8 columns. Then move on to the second row, and there are 8 columns again. This goes on until the first for loop finishes.

This post has been edited by AntonWebsters: 18 September 2009 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 islam-morad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 09

Re: CheckerBoard.java

Posted 18 September 2009 - 06:40 AM

thank u for replying guys but i am still confused i still cant understand it
Was This Post Helpful? 0
  • +
  • -

#5 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: CheckerBoard.java

Posted 18 September 2009 - 06:42 AM

View Postislam-morad, on 18 Sep, 2009 - 08:40 AM, said:

thank u for replying guys but i am still confused i still cant understand it


the nested forloops...
it builds the squares in this order. Top down.
1 9 17
2 10 18
3 11 19
4 12 20
5 13 and so on....
6 14
7 15
8 16
Was This Post Helpful? 0
  • +
  • -

#6 islam-morad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 09

Re: CheckerBoard.java

Posted 18 September 2009 - 06:48 AM

ok now i unerstand the for loops, one for the rows and the other for columns
but why j is multiplied by sqsize and i is also multiplied by sqsize

and what about the Boolean expression?
Was This Post Helpful? 0
  • +
  • -

#7 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: CheckerBoard.java

Posted 18 September 2009 - 07:00 AM

int sqsize= getHeight()/NRows;

getheight is the size of the window say 100 Nrows = 8.
so a sqsize would be whatever 100 / 8 is.
Was This Post Helpful? 0
  • +
  • -

#8 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: CheckerBoard.java

Posted 18 September 2009 - 07:00 AM

Boolean expression? You mean this?
sq.setFilled(((i+j)%2) !=0);


That's not a Boolean expression...@.@
It's for filling up odd-numbered squares, so that the odd-numbered squares will be black, while the even-numbered ones will be white.

This post has been edited by AntonWebsters: 18 September 2009 - 07:07 AM

Was This Post Helpful? 1
  • +
  • -

#9 islam-morad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-September 09

Re: CheckerBoard.java

Posted 18 September 2009 - 07:08 AM

now i started to understand but why x = j*sqsizw and y = i*sqsize
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: CheckerBoard.java

Posted 18 September 2009 - 07:32 AM

that is to create a new square and place it in the next position in the board..multiplying x with j calculates the x coordinate to place the Square, and i*size calculates the y coordinate...

This post has been edited by mostyfriedman: 18 September 2009 - 07:36 AM

Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: CheckerBoard.java

Posted 18 September 2009 - 09:13 AM

This looked like fun. Here's an example with less mystery Java. Read comments.

import java.awt.*;
import javax.swing.*;

class CheckBoard extends JPanel  {
	private static final int Rows = 8;
	private static final int Cols = 8;
	
	public void paint(Graphics g) {
		Dimension d = this.getSize();
		
		// don't draw both sets of squares, when you can draw one
		// fill in the entire thing with one color
		g.setColor(Color.WHITE);
		
		// make the background
		g.fillRect(0,0,d.width,d.height); 

		// draw only black
		g.setColor(Color.BLACK);
		
		// pick a square size based on the smallest dimension
		int sqsize = ((d.width<d.height) ? d.width/Cols : d.height/Rows);
		
		// loop for rows
		for (int row=0; row<Rows; row++) {
			int y = row*sqsize;  // y stays same for entire row, set here
			int x = (row%2)*sqsize; // x starts at 0 or one square in
			for (int i=0; i<Cols/2; i++) {  // you will only be drawing half the squares per row
				// draw square
				g.fillRect(x,y,sqsize,sqsize);
				// move two square sizes over
				x += sqsize*2;
			}
		}
	}

	public void update(Graphics g) { paint(g); }
	
	public static void main (String[] args) {
		JFrame frame = new JFrame("CheckBoard");
		frame.setSize(600,400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(new CheckBoard());
		frame.setVisible(true);
	}
}


Was This Post Helpful? 1
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: CheckerBoard.java

Posted 18 September 2009 - 09:27 AM

no recursion this time baavgai?? :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1