Welcome to Dream.In.Code
Become a Java Expert!

Join 149,520 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,406 people online right now. Registration is fast and FREE... Join Now!




Using nested for loops and class constants.

 
Reply to this topicStart new topic

Using nested for loops and class constants., Need help with for loops and class constants.

wilsmic85
3 Jul, 2007 - 04:58 PM
Post #1

New D.I.C Head
*

Joined: 3 Jul, 2007
Posts: 5


My Contributions
Edit: Thanks for the assistance.

This post has been edited by wilsmic85: 4 Jul, 2007 - 02:51 PM
User is offlineProfile CardPM
+Quote Post

wilsmic85
RE: Using Nested For Loops And Class Constants.
3 Jul, 2007 - 07:24 PM
Post #2

New D.I.C Head
*

Joined: 3 Jul, 2007
Posts: 5


My Contributions
Here's what the output should look like:

Edit: Link removed.

I have a difficult time with the pipe that breaks the pattern on the disk. It goes straight down the middle of it. The pipe is the || part.

This post has been edited by wilsmic85: 4 Jul, 2007 - 02:53 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Using Nested For Loops And Class Constants.
3 Jul, 2007 - 09:56 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi,

The code below will get you started. I am not too sure if you had certain restrictions on the type of functions and methods you could use, but the example I have provided will get you through the disk and looking great.

CODE

// This program produces the Space Needle
public class Cool {
    public static final int HEIGHT = 4;

    public static void main(String[] args) {
        printNeedle();
        drawFigure();
    }

    // Produce the needle at the top
    public static void printNeedle () {
        for (int i = 1; i <= HEIGHT; i++) {
            for (int j = 1; j <= 1; j++) {
                System.out.printf("%14s\n", "||");
            }
        }
    }

    public static void drawFigure() {
        // Temporary variable to hold the line during assembly
        String line;

        for (int i = 0; i < HEIGHT; i++) {
            line = "__/";

            // For lines 5-8 calculate number colons
            for (int j = 0; j < i; j++) {
                line = line + ":::";
            }

            // Add the pipe in the middle and pad it with spaces to line up
            line = line + "||";
            System.out.printf("%14s",line);

            for (int j = 0; j < i; j++) {
                System.out.print(":::");
            }

            System.out.print("\\__\n");
        }
    }
}



Take note of the use of printf which will give you the proper padding of spaces to your string so that you can get the pipes to line up. I threw this solution together really quick and it has been tested, but it is of course a bit of a ways from completion.

The tools you can learn from the segment I provided will help you establish the center of the need for the rest of the drawing and can make the rest pretty easy I believe.

It's rude and crude and can be seriously refactored, but it works.

Enjoy!
User is offlineProfile CardPM
+Quote Post

wilsmic85
RE: Using Nested For Loops And Class Constants.
4 Jul, 2007 - 10:00 AM
Post #4

New D.I.C Head
*

Joined: 3 Jul, 2007
Posts: 5


My Contributions
Edit

This post has been edited by wilsmic85: 4 Jul, 2007 - 08:54 PM
User is offlineProfile CardPM
+Quote Post

wilsmic85
RE: Using Nested For Loops And Class Constants.
4 Jul, 2007 - 10:22 AM
Post #5

New D.I.C Head
*

Joined: 3 Jul, 2007
Posts: 5


My Contributions
Edit.

This post has been edited by wilsmic85: 4 Jul, 2007 - 08:54 PM
User is offlineProfile CardPM
+Quote Post

wilsmic85
RE: Using Nested For Loops And Class Constants.
4 Jul, 2007 - 01:49 PM
Post #6

New D.I.C Head
*

Joined: 3 Jul, 2007
Posts: 5


My Contributions
Edit.

This post has been edited by wilsmic85: 4 Jul, 2007 - 08:53 PM
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Using Nested For Loops And Class Constants.
5 Jul, 2007 - 02:12 AM
Post #7

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
What's up with deleting your posts? The whole point of the public forum is to help people. Not that anyone actually searches the forum before posting (as they should), but if they did and had the same problem you had, they would not get any help from you post. In the future, please refrain from deleting your posts after resolution.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month