I am supposed to use this method

public static void drawIndentedLine (int spaces, String begin, int n, String middle, String end)

This is what I am suppsed to do
It should print a single line, terminated with a newline, that begins with the specified number of spaces, followed by the begin string, followed by n copies of the middle string, followed by the end string.

Am I doing the right thing for my ouput by using system.out.println ?
"______________"
" ___________"
" _________"

Dani AI

Generated

Separation of concerns will make this simple. Let the small helper build the text of a single indented line and return it; put printing in a tiny wrapper (or in your test main). That keeps the method independent of main (so was right: calling helpers from main for tests is normal) and gives you one reusable builder you can call from your diamond routine. For : implement the builder once, then call it inside the top and bottom loops of your diamond method.

public static String makeIndentedLine(int indent, String start, int count, String middle, String end) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < indent; i++) sb.append(' ');
    if (start != null) sb.append(start);
    for (int i = 0; i < count; i++) sb.append(middle);
    if (end != null) sb.append(end);
    return sb.toString();
}

Example of composing a diamond by calling that builder from two loops (top then bottom). The usual formulas are: spaces = size - i - 1, middleCount = 2*i + 1. Run the bottom loop from size-2 down to 0 to avoid duplicating the middle row.

public static void renderDiamond(int size) {
    if (size <= 0) return;
    // top (including middle)
    for (int i = 0; i < size; i++) {
        int spaces = size - i - 1;
        int middleCount = 2 * i + 1;
        System.out.println(makeIndentedLine(spaces, "/", middleCount, "-", "\\"));
    }
    // bottom (exclude middle)
    for (int i = size - 2; i >= 0; i--) {
        int spaces = size - i - 1;
        int middleCount = 2 * i + 1;
        System.out.println(makeIndentedLine(spaces, "\\", middleCount, "-", "/"));
    }
}

Notes and troubleshooting: if your assignment forces a void drawIndentedLine that prints, implement it as a one-line wrapper that calls the string builder and System.out.println so all construction logic stays in one place. Validate inputs (reject or clamp negative indent/count) and be aware that middle with length > 1 changes visual width. Test with small sizes first to confirm spacing and whether you want the middle row duplicated or not.

Recommended Answers

All 8 Replies

Am I doing the right thing for my ouput

Does your program print out what you want printed?

yes it should print them but i dont want operation of this method to depend on the main.I am creating 5 other methods like this and i want to use the main only for testing other methods

Can you explain what problems you are having? I don't see how a method would depend on "the main"?
I'm not sure what "the main" is? Is it a method or a class or what?

the main is a the method . I am trying to create 5 other methods with headers which will carry out various operations like drawing indented lines,parralelogram,diamond etc.I am using the main method only to make calls on the other methods to test them.

That sounds normal: the main() method calling other methods to test them.

I just want to know how i can connect methods for example i am planning to draw a diamond using the

public static void drawDiamond (int size)

I want to know how to use drawIndentedLine in my implementation of drawDiamond.I want one loop to draw the top part, and another loop to draw the bottom part. The size of my diamond should be the number of times each loop repeats.

how i can connect methods

By calling them one after the other.

void aMethod() {
  firstMethod(...);
  secondMethod();
  thirdMethod();
}    

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.