Results 1 to 19 of 19
Thread: Efficient Looping to Draw Shapes
- 10-29-2009, 02:36 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
Efficient Looping to Draw Shapes
Right now, we're working with Groovy, a variation on Java, so quite similar, if not just like, Java.So please bear with me if some syntax looks unfamiliar, it's easily convertible to Java syntax!
Anyhow, here's the shape I'm trying to draw,
*******
#*----*
##*---*
#-#*--*
#--#*-*
#---#**
#----#*
#######
For the first and last row, these can be expressed at the begining and end of the program, like so
7.times{print '*') and likewise for #.
I'm trying to output one character at a time, so that's when the for looping comes into play. The involvment of two characters is what's throwing me off. I managed to write the following for another shape, which projects the same idea of incrementing/decrementing spacing...
//Answer to Q2.9
n = 3
println '*'
for (row in 1..7)
{
if (row < 5)
{
print '*'
(row - 1).times{
print ' '
}
println '*'
}
else
{
print '*'
(row - n).times{
print ' '
}
n = n + 2
println '*'
}
}
println '*'
Any idea how I can go on about with this?
Many thanks!
- 10-29-2009, 02:38 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Are you looking for Groovy code or Java code?
- 10-29-2009, 02:40 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
The aforementioned code draws this,
*
**
*.*
*..*
*...*
*....*
*...*
*..*
*.*
**
*
- 10-29-2009, 02:45 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
Having worked with Java, I don't mind either...I can covert then.
- 10-29-2009, 02:45 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
- 10-29-2009, 02:47 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
Java and Groovy are nearly the same (Java is more punctuation-structured), so whatever error I'll have in Groovy will be the same in Java. We can pretend we're working with Java in this instance.
There are not many Groovy forums, as developed as this one.
- 10-29-2009, 09:11 PM #7
"Java and Groovy are nearly the same"
Groovy syntax looks very different then java.
It like saying that VB.Net syntax is nearly the same as C# syntax because they use the same underlying libraries.Last edited by mrmatt1111; 10-29-2009 at 09:17 PM.
My Hobby Project: LegacyClone
- 10-29-2009, 09:50 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
Groovy website description:
Groovy...
.
.
.
builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
makes modern programming features available to Java developers with almost-zero learning curve
.
.
.
compiles straight to Java bytecode so you can use it anywhere you can use Java
There is more. Feel free to visit the website. Effectively, Groovy is an inspiration from Java - in fact, if you try writing the for loop as written in Java (meaning with the three components within parantheses, i.e. variable declaration, condition and updating) in the groovy console, it will compile :)
Now you guys are completely defeating the purpose of this forum and thread. All I'm asking is if you can be help me generate a compact program - whether you disagree with the fact that Java and Groovy are alike transcends the main idea of the thread. I honestly don't mind if you write it in java, this is not homework to be given in. It's for myself. I would just like to know how people generally approach these kinds of questions.
- 10-29-2009, 10:20 PM #9Java Code:
for y as integer = 0 to height for x as integer = 0 to width if y = 0 then print "*" elseif y = height -1 then print "#" else if x = 0 then print "#" elseif x = width -1 then print "*" else if x = y then print "*" elseif x+1=y then print "#" else print "-" end if end if end if next println "" next
My Hobby Project: LegacyClone
- 10-30-2009, 12:22 AM #10
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
Thanks! But what language is your's in exactly? Pseudocode?
- 10-30-2009, 12:34 AM #11
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
I'm not too sure your code works!
- 10-30-2009, 04:38 AM #12
Have you tried running it? It works for me. If you don't want to convert from psudocode to Java, just check each branch and see if the logic is valid. My guess is that's how they made the psudocode to begin with, since most people don't program in psudocode.
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 10-30-2009, 06:44 PM #13
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
Ok, it does work, although partially! I'm tweeking it a bit...
- 10-30-2009, 06:49 PM #14
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
your "end if" statements are unnecessary, aren't they?
- 10-30-2009, 07:02 PM #15
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
- 10-30-2009, 07:10 PM #16
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
I'm not concerned with language syntax - I find it unnecessary to insert the end if statements, since the if statements automatically end (/break) when the condition evaluates to true and the statement within is executed...
- 10-30-2009, 11:53 PM #17
The end if statement marks the end of the if branch - it's similar to the close brace '}' in C style programing languages. It's common for BASIC-like syntaxes (e.g. Visual Basic).
Quoted from If...Then...Else Statement (Visual Basic)
Java Code:If condition [ Then ] [ statements ] [ ElseIf elseifcondition [ Then ] [ elseifstatements ] ] [ Else [ elsestatements ] ] End If
Edit:
I think their code IS Visual Basic... It's been a while but the syntax does look familiar.Last edited by CodesAway; 10-30-2009 at 11:59 PM.
CodesAway - codesaway.info
writing tools that make writing code a little easier
- 10-31-2009, 04:15 AM #18Edit:
I think their code IS Visual Basic... It's been a while but the syntax does look familiar.Last edited by mrmatt1111; 10-31-2009 at 04:39 AM.
My Hobby Project: LegacyClone
- 10-31-2009, 05:05 AM #19
Member
- Join Date
- Oct 2009
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
erase shapes ???
By h9h in forum Java 2DReplies: 1Last Post: 10-12-2009, 09:11 PM -
Need a simple and efficient solution.
By Samgetsmoney in forum New To JavaReplies: 9Last Post: 02-20-2009, 02:18 AM -
Efficient Perfect Number
By Lite3864 in forum New To JavaReplies: 4Last Post: 11-23-2008, 02:07 AM -
Colors and shapes.
By Torgero in forum New To JavaReplies: 14Last Post: 10-13-2008, 06:25 PM
Bookmarks