Page 1 of 4 123 ... LastLast
Results 1 to 20 of 72
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default Java Checkerboad question CS106A Stanford univerity

    hiya,
    I have a code to make a checkerboard written by Mehran sahami CS106A Stanford university in usual black and white colour as in chess.But my assignment is to make a checkerboad with white and circle in square instead of black colour.See picture below of a checkerboad I need to program.



    I have got a code by Mehran sahami CS106A Java Stanford university.Which is

    /*File:chap4ques11checkerboard.java
    * This program draws checkerboad with circle inside alternate sqaures.
    */


    import acm.program.*;
    import acm.graphics.*;

    public class chap4ques11checkerboard extends GraphicsProgram{

    /*Number of rows*/

    private static final int NROWS = 8;

    /*Number of columns*/

    private static final int NCOLUMN = 8;

    /*Runs the program*/

    public void run(){
    int sqSize =getHeight() / NROWS;
    for (int i=0; i < NROWS; i++){
    for (int j=0; j < NCOLUMN; 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);

    }
    }


    }


    }

    This creates black and white checkerboad.Any idead how we can out circles in square boxes instead filled them with black colour.

    Thank you in advance.

    cheers
    Last edited by ccie007; 05-19-2010 at 06:29 PM. Reason: little error

  2. #2
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I'm sure someone will help you shortly.

    Until then, have you attempted to do the exercise? Do you have any ideas on how to do the exercise?

  3. #3
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    I have attempted many times.

  4. #4
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Maybe if you were to post up what you have tried doing? I'm sure you don't want someone to just do the exercise for you, right?

  5. #5
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    This is what i have done so far.

    /*File:chap4ques11checkerboard.java
    * This program draws checkerboad with circle inside alternate sqaures.
    */


    import acm.program.*;
    import acm.graphics.*;

    public class check extends GraphicsProgram{

    /*Number of rows*/

    private static final int NROWS = 8;

    /*Number of columns*/

    private static final int NCOLUMN = 8;

    /*Runs the program*/

    public void run(){
    int sqSize =getHeight() / NROWS;
    for (int i=0; i < NROWS; i++){
    for (int j=0; j < NCOLUMN; j++){

    int x = j * sqSize;

    int y = i * sqSize;

    GRect sq = new GRect(x,y,sqSize,sqSize);

    add(new GOval(26, 3, 20, 20));


    add(sq);

    }
    }


    }
    }

  6. #6
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    This is how it looks at the moment.
    http://i.domaindlx.com/cobainia/ch.jpg

  7. #7
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    There's a big clue on how to do this exercise if you examine how the previous checkerboard program works. I think that if you understand how the checkerboard program works then you should have no problem doing this exercise.

    Take a look at the code used to draw the squares and determine whether they should be filled or not.

    Java Code:
    GRect sq = new GRect(x,y,sqSize,sqSize);
    sq.setFilled(((i + j) % 2) !=0);
    As you can see from the first line of code a GRect object takes it's x and y value from the integer x and integer y. So, what's the value assigned to int x and int y? Look at the following code.

    Java Code:
    int x = j * sqSize;
    
    int y = i * sqSize;
    What we see here is that the value x and y holds depends on the value of j (for the integer x), and i (for the integer y) and that is then multiplied by sqSize (the size of the square). And of course int i and int j are increasing by 1 with every loop as you can see in the for loop (i++ and j ++). So try and visualize this; the graphics window starts at 0, 0 (top left of the screen) and when the program runs it multiplies j with sqSize in order to find the x coordinate. It also multiplies i with sqSize in order to find the y coordinate. Well, what is the vaue of i and j when the loop is run? As you can see both integers are initialized with the value 0:

    Java Code:
    for (int i=0; i < NROWS; i++){
    for (int j=0; j < NCOLUMN; j++){
    So in run of the loop the x and y coordinate would be 0, 0 and that is where the box will be drawn (remember, the x and y coordinate is referring to the top left of the GRect as that's where it starts to draw).

    Ok so what happens the second time the loop is run? i remains 0, but j increases by 1. So now x is equal to sqSize, and y remains 0. So imagine in your head that the x coordinate has gone from 0, to sqSize (however many pixels that may be), and the y coordinate hasn't changed which makes sense since we are on the same line (or row) of squares.

    Ok so I hope that kind of explains how the x and y coordinates are determined. As for whether the square is filled or not take a look at the second line of the code:

    Java Code:
    sq.setFilled(((i + j) % 2) !=0);
    This is calling the setFilled method on the object sq, and the parameters expected are boolean values equal to true or false. In others word when we add the value of i and j together, and then divide that by 2, if the remainder is NOT equal to 0 then that square will be filled. In the first run of the program we know that i and j are equal to 0 and therefore the test returns false and so the square isn't filled. Of course, the next run of the loop the values of i and j will have changed (now to i = 0, j = 1 which we know is odd when we add them together and therefore when divided by 2 returns a remainder - so the remainder is not equal 0) which will result in the next square being filled since the test is now true.

    I'm sure someone will soon correct me if I'm wrong. I think that if you understand the above then you should have a good idea on how to do the exercise by adding circles to the canvas rather than filling the squares in as it's very similar.
    Last edited by Learning Java; 05-20-2010 at 07:59 PM.

  8. #8
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up nice one LJ

    Learning Java explains the problem well...
    @ LJ see what i meant now? nice one :D

    @ ccie007
    all that your code shows that you have done is manually located the position of the first GOval's x and y co-ordinates.
    This is not the objective of the exercise.

    the objective is:
    after you add each GRect: Determine, whether to add a GOval as well.

    It is important to make sure that you follow what the original code does and how the nested (one inside the other) for loops calculate the x and y coordinates for each GRect

    the original code than also determines whether to set the GRect as filled. based on whether the combined values of i and J are odd or even. as LJ explains
    its merely a case of substituting the set filled statement with code to add a new GOval,
    the properties (parameters) of said GOval (the x , y , width and height) do not require manually working out. those properties (parameters) are available to you, because you used them as the properties (parameters) of the GRect.
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  9. #9
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Thank you LJ iam still studying it.A quick question Sonny.It is written above that your are studying the art and science of Java.I am reading the same and lectures from CS106A.I believe you know about Java alot but I get confuse when I see just println statement in the book art and science of java and system.out.println,public void run(args[]) etc on other books.Do we use just println because we are using Eclipse(recommended platform for CS106A) and other use system.out.println because they are using JDK etc.

    thank you

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    Thank you LJ iam still studying it.A quick question Sonny.It is written above that your are studying the art and science of Java.I am reading the same and lectures from CS106A.I believe you know about Java alot but I get confuse when I see just println statement in the book art and science of java and system.out.println,public void run(args[]) etc on other books.Do we use just println because we are using Eclipse(recommended platform for CS106A) and other use system.out.println because they are using JDK etc.

    thank you
    Hover your mouse cursor over println in your Eclipse editor, and you will see that it is declared in acm.program.Program. Because your program inherits from ConsoleProgram or GraphicsProgram, which in turn inherits from acm.program.Program, you have access to its version of println() (which is a simple wrapper around System.out.println()). The ACM libraries do this to try to keep things simple, but sometimes, as you can see, it causes more confusion than it saves.

    -Gary-

  11. #11
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    is it same with ([]args)? as in

    public class Echo {
    public static void main (String[] args) {
    for (String s: args) {
    System.out.println(s);

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    is it same with ([]args)? as in

    public class Echo {
    public static void main (String[] args) {
    for (String s: args) {
    System.out.println(s);
    Well, now it's going to get a little more confusing. main() is a static method, so it does not have access to non-static methods or instance variables of the class, such as the println() method. For ConsoleProgram or GraphicsProgram classes, you should put your code in a run() method. If you want a main() method, it should look something like this:

    Java Code:
            public static void main(String[] args) {
                    new ClassName().start();
            }
    Replace ClassName with the name of your class, of course. The start() method does some setup work, and then invokes init() and run(). The init() and run() methods are not static, so they do have access to non-static methods and variables, such as println().

    These ACM programs are not really "standard Java" -- the ACM libraries wrap functionality in an attempt to make things simpler. Keep in mind that CS106A is not really a Java course, but rather an introductory computer science course. They use the ACM libraries in order to avoid having to explain some Java-only concepts that you really don't need for the course (but that you should learn if you want to be a Java developer).

    Read this for a more complete explanation:

    Chapter 6. The acm.program Package

    -Gary-

  13. #13
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Ok so what happens the second time the loop is run? i remains 0, but j increases by 1. So now x is equal to sqSize, and y remains 0. So imagine in your head that the x coordinate has gone from 0, to sqSize (however many pixels that may be), and the y coordinate hasn't changed which makes sense since we are on the same line (or row) of squares.
    I couldnot understand this bit fron LJ's explanation.Could somebody explain this to me plz.

    My understanding is that in second time when the loop is run both i and j will have value 1.
    Last edited by ccie007; 05-21-2010 at 06:46 PM. Reason: ops

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    have access to non-static methods or instance variables of the class, such as the println()
    Check you doc. Mine says that System.out is static
    So you can use System.out.println() in a static context.

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    Check you doc. Mine says that System.out is static
    So you can use System.out.println() in a static context.
    I wasn't talking about System.out.println() but rather the println() method declared in acm.program.Program -- I think it's clear if you're following the thread.

    -Gary-

  16. #16
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Thank you GCalvin for the info.

  17. #17
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    I couldnot understand this bit fron LJ's explanation.Could somebody explain this to me plz.

    My understanding is that in second time when the loop is run both i and j will have value 1.
    No.
    Java Code:
                    for (int i=0; i < NROWS; i++){
                            for (int j=0; j < NCOLUMN; j++){
    
                                    int x = j * sqSize;
    
                                    int y = i * sqSize;
    
                                    GRect sq = new GRect(x,y,sqSize,sqSize);
    
                                    add(new GOval(26, 3, 20, 20));
    
    
                                    add(sq);
    
                            }
                    }
    There are two loops, an outer loop with i as the index, and an inner loop with j as the index. LJ was talking about the second iteration of the inner loop. In that case, i is still 0 while j has been incremented to 1. j continues to increase by 1 each time through the inner loop, until it reaches NCOLUMN, at which point i is incremented to 1, and j is set back to 0.

    -Gary-

  18. #18
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Norm and GCalvin any comment for me .I have just entered in Java and software development world from networking world(cisco etc).I used pascal,javascript at college.I believe i made a right move by stepping into software development.What do u guys reckon.

  19. #19
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    Norm and GCalvin any comment for me .I have just entered in Java and software development world from networking world(cisco etc).I used pascal,javascript at college.I believe i made a right move by stepping into software development.What do u guys reckon.
    Well, that's a bit off-topic, but I'm not at all certain that moving away from networking and into development is a great move. Real networking experts are in greater demand and more valuable than developers (especially junior developers), and it's more difficult to offshore a networking job than a development job. Having said that, you should do what you want, what you're good at, and what you enjoy.

    -Gary-

  20. #20
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    j continues to increase by 1 each time through the inner loop, until it reaches NCOLUMN, at which point i is incremented to 1, and j is set back to 0.GCalvin how do u mean by NCOLUMN above.Is it 1 coloumn or 8 column.cheers.
    Last edited by ccie007; 05-21-2010 at 07:09 PM.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 04:43 PM
  2. Need help with java question
    By ccie007 in forum New To Java
    Replies: 23
    Last Post: 05-18-2010, 06:32 PM
  3. Java question
    By TGH in forum New To Java
    Replies: 12
    Last Post: 11-27-2009, 02:05 PM
  4. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM
  5. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 04:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •