This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Succeeding with AI and have Veljko Krunic on-line!
See this thread for details.
Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Art and science of Java. Chapter #4 exercise #11

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

This is my first post. I am beginner in learning CS and am working through the art and science of Java book to teach myself Java, using a version of Stanford's cs106a class. I'm having a trouble with one of the exercise.

The task is: Write a GraphicsProgram that uses two nested for loops to create the following checkerboard diagram in the upper left corner of the canvas. Alternate squares both horizontally and vertically contain a somewhat smaller GOval to create the checkerboard effect.

My code works in proper way:

But it looks so messy , so I ask you to help me find more laconic way to write this program please
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well congratulations on noticing it looks messy and wanting to do something about it, most beginners just want to get code working. Writing working code that is maintainable is far more important than just having code that works.

To make your code less messy:

1. Use meaningful names. Using i, j, k etc for looping counters and x & y for locations is fine; but 'w' instead of 'width' just makes the code harder to read and using 'l' is a positive no-no as it's so easy to mis-read it as '1'.
2. Consistently format and indent your code. There are many standard ways of indenting code and everyone has their favourite so choose one you like and make sure you use it consistently.
3. Add whitespace judiciously ie adding the occasional blank line in an appropriate place can make things easier to read.

for example:


The next step would to refactor the code to put the for loops into their own methods, which I'll leave up to you.
 
Ranch Hand
Posts: 120
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saltanat Akhmet wrote:the art and science of Java



It's ironic that book is named "art" and science.
 
Marshal
Posts: 68966
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is very appropriate. We should use more effort on the art bit than the science bit. Tony is concentrating on art here.
 
Quazi Irfan
Ranch Hand
Posts: 120
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, it is very appropriate. We should use more effort on the art bit than the science bit. Tony is concentrating on art here.



Oh, sorry about that. I was under the assumption that it's a code from the book. Now that I've re-read the post - it's what the OP wrote.
 
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice way of solving the exercise, this is how I initially solved it:



And this is how I coded after seeing the solution posted here:

 
Campbell Ritchie
Marshal
Posts: 68966
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Welcome to the Ranch and thank you for posting your solution, it's good to know the thread helped you.
 
R Ferreira
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Marshal
Posts: 15542
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution you posted actually doesn't meet the requirements.  You are asked to use two nested for-loops. No mention of a while-loop. While you could interpret an non-mention as non-exclusionary, there is a solution where you actually only use two for-loops, one nested in another.  This is a common idiom for processing a two-dimensional gridlike structure like a checkerboard.  Also, there's still a lot of code that's jammed into just one method. The code could be made clearer by setting up different levels of abstraction and hiding detailed operations in lower-level helper methods.
 
Junilu Lacar
Marshal
Posts: 15542
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an outline that you can fill in:

In each of those methods that have been elided, you only need to provide the expression to return or the appropriate argument for the add() method call. That's about as simple as this solution can get in terms of refactoring and readability.
 
R Ferreira
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I forgot about the requirements, didn't even notice the nested for loops.. although I knew about methods already, I tried to solve this exercise using only what the author described up into that point in the book. That exercise is from chapter 4, and he only starts to talk about methods in chapter 5.. but you are right, solving that exercise using only two for loops without any function calls would generate a even more messy code.
I'm trying to fill the outlet in my mind, the only thing I don't get is the offset method, what it does is return the first column of any row, right? 0, 8, 16, and so on until 64(although 64 wouldn't be used since its outside the 8 by 8 board.. what do you need the offset method for?
 
R Ferreira
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trying to fill the outline* sorry. I can't edit my posts?
 
Junilu Lacar
Marshal
Posts: 15542
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you copy the code into your IDE and do it there, then copy paste into a reply. No need to edit your previous post; it will be informative for others to see the progression of changes.
 
Junilu Lacar
Marshal
Posts: 15542
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use offset to calculate the X or Y coordinate value for a particular row or column. Instead of adding to the value of X and Y as you iterate the loops, you can calculate the values based on the row/column. That is, Y for row 0 is 0, for row 1 it will be SIZE, row 2 it is SIZE * 2, row 3 it is SIZE * 3, and so on. There is a recognizable pattern there. The same pattern applies for X and columns.
 
R Ferreira
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I meant, my last post, had a typo, I can't edit it? had to double post.. Hmmm, I'll do it that way then, and post my code once its finished..
 
R Ferreira
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finished it, sorry about the delay, thanks for the help Junilu, I see the pattern you were talking about, the checker is only drawn in the squares that are even in both column and row number, and the squares that are odd in both column and row.. that really simplifies the design lol

 
Junilu Lacar
Marshal
Posts: 15542
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job. The program looks pretty awesome when it's that clean and organized, doesn't it?
 
R Ferreira
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does thanks!
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic