Samantha
Samantha

Reputation: 29

Java Drawing Shape with "*"

I am very new to programming and am just trying to see if there are simpler ways to create the shape below.

I got the shape by using System.out.println() multiple times, but again am just trying to see if there are more concise ways to create this. I got the shape with this code, but if anyone has any other ways to do so please let me know!

public static void main(String[] args) {
    System.out.println("******");
    System.out.println(" *    *");
    System.out.println("  *    *");
    System.out.println("   ******");
    System.out.println("  *    *");
    System.out.println(" *    *");
    System.out.println("******");
    System.out.println(" *    *");
    System.out.println("  *    *");
    System.out.println("   ******");
}

Upvotes: 0

Views: 2021

Answers (2)

Zabuzard
Zabuzard

Reputation: 25903

Explanation

Two things do repeat here:

******
*    *

The rest of the code only adjusts with how much spaces you prefix the pattern and the overall logic of moving the pattern to the right or back to the left.


Base

So, let's first create two methods and a small helper method:

private static String constructPrefix(int length) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++) {
        sb.append(" ");
    }
    return sb.toString();
}

private static void printDelimiter(int indent) {
    System.out.println(constructPrefix(indent) + "******");
}

private static void printPart(int indent) {
    System.out.println(constructPrefix(indent) + "*    *");
}

Movement

Next we will create a helper method that moves the sequence to the right and one to move it to the left:

private static void printSequenceToRight(int indentRight) {
    // Start with an indent of one, include the end
    for (int i = 0; i <= indentRight; i++) {
        printPart(i);
    }
}

private static void printSequenceToLeft(int indentRight) {
    // Start with right end, include one
    for (int i = indentRight; i >= 1; i--) {
        printPart(i);
    }
}

Sequence

Now we need a method that prints one part of the sequence. Let's define that a part always starts with the delimiter and ends without delimiter.

private static void printSequence(int indent) {
    printDelimiter(0);
    printSequenceToRight(indent - 1);
    printDelimiter(indent);
    printSequenceToLeft(indent - 1);
}

Finally all we need is a method that prints a full sequence, i.e. that repeats a sequence (and appends the final delimiter):

public static void printFullSequence(int amount, int indent) {
    for (int i = 0; i < amount; i++) {
        printSequence(indent);
    }
    printDelimiter(0);
}

Usage

Usage is simple, a call could look like:

printFullSequence(5, 2);

Output is:

******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******
  *    *
 *    *
******

The cool thing is, it's fully dynamic. You could also call it like

printFullSequence(1, 6);

Which outputs:

******
 *    *
  *    *
   *    *
    *    *
     *    *
      *    *
       ******
      *    *
     *    *
    *    *
   *    *
  *    *
 *    *
******

Upvotes: 1

Gatusko
Gatusko

Reputation: 2598

So I think you want this in the output console

******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******

So why don't make an static function and a for for outputing in the console:

public static void main(String[]args) 
{
    Scanner scan = new Scanner(System.in);
    int howManyTimes = scan.nextInt();

    for(int i=0;i<howManyTimes;i++)
    {
        outPut();
    }
    System.out.println("******"); // For always closing the Graph
}

public static void outPut()
    {
        System.out.println("******");
        System.out.println(" *    *");
        System.out.println("  *    *");
        System.out.println("   ******");
        System.out.println("  *    *");
        System.out.println(" *    *");
    }

Input 5

OutPut:

******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******
  *    *
 *    *
******
 *    *
  *    *
   ******
  *    *
 *    *
******

Upvotes: 3

Related Questions