• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Figuring out my spacing with spaces

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I've been struggling to get this program to print correctly. It's supposed to be the space needle, but I can't get any spacing to work (see what I did there...). The main problem is in drawBase, but also with drawTop printing upside down. Here's the link to see what it's supposed to look like. I've been stuck on these spaces for hours now, any help would be much obliged.
Size 4:https://wcc.instructure.com/courses/1410236/files/75616935
Size 2:https://wcc.instructure.com/courses/1410236/files/75616936
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That link is no use as the website requires your login details to access the information. Please post an image of what you are expecting.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

I have made a tiny change to your post so the link requiring a login doesn't appear to be a link.
Is this drawing supposed to have some sort of mathematical formula to calculate how many spaces there are? Or is there some sort of file saying how many spaces there are per line?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will get faster performance if you add the characters to be drawn to a StringBuilder, and then print the entire StringBuilder in a single print instruction, but that isn't going to help your current problem.


If you can print "||" n×, why can't you work out how to print " " (or ' ') n×? It will look almost the same. Remember to say print rather than println.
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,
IMO he needs to print some graphics i think may be like the one as in this link.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what happened when I tried executing OP's code, yes, that is what the output is supposed to look like.
A pretty useless exercise unless you need practice in writing lots of for loops.
By The Way: Make all methods in that class private, except main(). They shou‍ld not be callable from outside the class. Not unless you want to draw half a Space Needle.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Niko,
None of your method is doing the correct work,as intended,problem is in the logic of for loops(please note the *Campbell* suggestion).and IMO this all occurs because you do not use a good naming scheme,f and i doesn't let you understand their part of role in your program.you may do these things:
.    Analyze the problem first.
.    Build your logic for different parts.
.    Write a good and neat pseudo code.
.    Make use of OOPs paradigm-make use of refactoring-encapsulate the logic for different parts.

Kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

praveen kumaar wrote:. . . Make use of OOPs paradigm . . .

Unfortunately, it looks as if they are not being taught OO, but procedural programming.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OO means object‑oriente[ed][ation] and IMO means in my opinion. I discovered the other day that there are 44 different meanings for IMO , so we ought to avoid abbreviations.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I can tell, OP is trying to solve Part B in this document: http://courses.cs.washington.edu/courses/cse142/12sp/homework/2/spec.pdf

@Niko: It would help if you also posted the output you are getting. Copy/paste what you're seeing and put it between code tags
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'' sorry everybody, I was taking a few hours of sleep before my next set of classes in the morning. Here are the pictures for the two outputs. The bigger one is Size 4, while the smaller one is Soze 2.
IMG_5845.PNG
[Thumbnail for IMG_5845.PNG]
IMG_5846.PNG
[Thumbnail for IMG_5846.PNG]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that's what you're expected to generate. What are you actually generating? It's easier to see what you're doing wrong if we get to actually see what your program is generating.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I can tell just by looking at the code: your lines probably don't line up properly. A new line of output will always start at column 1.  For example, to draw the "pipe", you need to figure out how many spaces should be printed in front of the "||" so that it aligns properly. I think the bulk of your problem is that you're just directly printing out the characters without first printing spaces so that your characters get printed starting at the appropriate column. As in any kind of art, empty space is important, too.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:From what I can tell, OP is trying to solve Part B in this document . . .

That would appear correct. It obviously is an exercise in writing for loops. As far as I can see however, there is all sorts of repetition. The single line shown in the Washington document has repetition of colons, and is also in two halves with reflection symmetry, so that suggests methods which print the same character several times, or methods which print a bit of text forwards or backwards.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In engineering drawing, we often use a center line as a point of reference. That concept will be helpful here. If you know which column your center line is on, you can calculate the distance from the center to the edges of your figure. With that, you can calculate how much empty space you need to create from the border of your "canvas" to the left edge of your figure.  You only need to calculate how much empty space needs to get on the left side since output goes from left to right. That is, you won't need to print spaces after the right edge of your figure because you just go to a new line anyway. Does that make sense?
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I'm on the public bus to the school right now so my laptop doesn't have any internet. So I won't be able to post a pic for 30 minutes or so. But take that entire figure, and smash it to the left. I don't have any spacing in it. That's the main problem.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's the additional twist of having to be able to scale the "drawing" -- as with the cited document, OP seems to be using a scale of 4 for this submission. The second image OP posted shows the figure drawn with a scale of 2.  Niko's Height variable should probably be declared as a static final int and its name changed to SCALE.
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Our teacher wants us to do this only using for loops. So basically what I need to do is create a decreasing for loop... but for some reason I can't figure it out for the life of me. I'be been programming with Java for about two weeks now
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niko Fricker wrote:I'm sorry, I'm on the public bus to the school right now so my laptop doesn't have any internet. So I won't be able to post a pic for 30 minutes or so. But take that entire figure, and smash it to the left. I don't have any spacing in it. That's the main problem.


Ok, it would have helped if you started out saying that in the first place. Now that we've established the main problem clearly, you don't really have to post an image of what your program generates. I think we can all imagine what it looks like now. This is why we ask people to TellTheDetails  (← click that, it's a link) when they ask for help. Details like "take that entire figure and smash it to the left" can really clear up any doubts as to what's going on.

Unfortunately, figuring out the spacing is part of this problem so we won't provide any code that will do that.  You need to figure it out yourself. I've already given you a hint: figure out which column your (vertical) center line would have to be in so that your figure will have enough room. Then figure out how much empty space you need between the left border/margin and the left edge of the part of the figure that you're currently printing out.  The math is not that complicated, it's just mainly simple division and subtraction.
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright thank you
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niko Fricker wrote:Our teacher wants us to do this only using for loops. So basically what I need to do is create a decreasing for loop... but for some reason I can't figure it out for the life of me. I'be been programming with Java for about two weeks now


Well, don't take that too literally. You can't do this ONLY with for-loops. You have to do some calculations, too. What your teacher probably meant by doing it with for loops is that he doesn't want you to write code like this:

That doesn't mean that you can't do any kind of calculations to help control the execution of the for-loops you're using.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niko Fricker wrote:Our teacher wants us to do this only using for loops. So basically what I need to do is create a decreasing for loop... but for some reason I can't figure it out for the life of me. I'be been programming with Java for about two weeks now


I've been recommending the book Think Like a Programmer by V Anton Spraul a lot lately. The first part of the book gives you strategies for tackling this sort of problem. It's all about finding patterns. The things about patterns and being able to recognize them is that you need to find a way to organize, break down, and generalize the differences between the parts that make up the pattern.  The only thing you've been able to recognize at this point is that there's something that's decreasing.  You have to figure out exactly what is decreasing and by how much it's decreasing. Also, I don't think all the patterns involved here are decreasing.

Here's an example:  25, 20, 15, 10, 5, 0 --- what's the pattern here?  A series of numbers, decreasing consistently by 5, right?  Analyze your figure, count spaces and characters from one line to the next. Then analyze your counts. Look at the pattern of change from one line to the next and see what general formula you can find that consistently describes what the change is from one line to the next.  You do this for everything that gets repeated in your for-loops.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:What your teacher probably meant by doing it with for loops is that he doesn't want you to write code like this:


Also, what your code is essentially (not literally) doing now instead of the above is this:

Do you see why your output looks like it's all smashed to the left side now?
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So this is my first attempt of what you're talking about... it just won't print anything. Am I on the right path?
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, not quite there yet...
badcode.PNG
[Thumbnail for badcode.PNG]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to print spaces.  Here's a statement that will print a single space character:

 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You need to print spaces.  Here's a statement that will print a single space character:


But if I do this, how will I be able to scale them?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That Think Like a Programmer book suggests that you try something simple first. Then you can take what you learn by doing something simple and, by analogy, do something different and perhaps slightly more involved.

For example, it's pretty easy to print a series of numbers, on each line like this:

You can use a simple for loop to do that.

Next, you can add a twist, so that you're printing basically the same series of numbers but you want it to look like this:

Don't try to solve the problem all at once. Build up to the solution and just keep adjusting. You're already halfway there.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niko Fricker wrote:But if I do this, how will I be able to scale them?


Scaling is just multiplying by a ratio. Your current ratio is 1:4 -- you need to figure out what "1" means for each part of your figure. Most of the parts are sized relative to other parts.  That PDF that I cited a few responses ago has some helpful tips about scaling. It even has an illustration that uses the scale of 4. I suggest you go and read it. You have been given a lot of tips already. I think it's time for you to do your part and do the work.

Good luck.
 
Niko Fricker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the help Junilu!
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider my earlier suggestion that you need a method like thisAlso go through the drawing with pencil and paper and write down how many spaces there are on the left, and how many of the other characters, and if lines are repeated unchanged, how many times.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're wondering about the advice others have given you regarding the use of objects and object-oriented programming to solve this, here's an example of what you main() method might look like by doing that.

There are many ways to design objects. That's just one option. Another way would allow you to write something like this:

The PDF from the University of Washington that I linked to in my first response here suggested that the scale be set as a constant. The above code (first example) implies that the scale is an object attribute that is either set when you create a SpaceNeedle object or, as the second example shows, can be changed via a mutator (setter method). Either way, when you print out the object, the ASCII art rendering would be generated, which implies that you override the toString() method.

This might be over your head right now since you probably won't be taught about objects and instance methods until later or, unfortunately in many cases it seems, in a follow-on course to this one that you're taking right now.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I started off being taught objects in Java® and I am sure that is the correct way to do it. Many people have great difficult going from procedural coding, which is what you are using, to object‑orientation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic