Raptor6872 Raptor6872 - 1 month ago 10
Java Question

How to print a set of asterisk(*) towers on the same line, sorted by height in Java?

The code below is the closest iteration of my project that I've been able to get.

The goal of the program is to print the towers side by side from tallest on the left to the shortest on the right.

enter image description here

I already figured out the parts to make the user enter only an odd number and the section about generating an odd random int between 3 and 11.

All is listed in the code below.

Also, I have it print the random int's just to see what they are for now.

import java.util.Random;
import java.util.Scanner;

/*
* Name
* Class
* Fall 2016
* Date
* Programming Assignment - 2
*/

public class Towers {

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

//Name and Program info
System.out.println("Class, Fall 2016");
System.out.println("NAME:");
System.out.println("PROGRAMMING ASSIGNMENT 2");
System.out.println("");

//Print Statement


// Width for tower 1
System.out.print("Please enter the width of tower 1 (an odd number): ");
int towerWidth1 = in.nextInt();
while (towerWidth1 %2 == 0) {
System.out.println("Invalid Input! Width needs to be odd.");
System.out.println("Reenter the width of tower 1 (an odd number): ");
towerWidth1 = in.nextInt();
}

// Width for tower 2
System.out.print("Please enter the width of tower 2 (an odd number): ");
int towerWidth2 = in.nextInt();
while (towerWidth2 %2 == 0) {
System.out.println("Invalid Input! Width needs to be odd.");
System.out.println("Reenter the width of tower 2 (an odd number): ");
towerWidth2 = in.nextInt();
}

// Width for tower 3
System.out.print("Please enter the width of tower 3 (an odd number): ");
int towerWidth3 = in.nextInt();
while (towerWidth3 %2 == 0) {
System.out.println("Invalid Input! Width needs to be odd.");
System.out.println("Reenter the width of tower 3 (an odd number): ");
towerWidth3 = in.nextInt();
}

//Random odd height tower 1
Random Rand1 = new Random();
int towerHeight1 = 2 + Rand1.nextInt(11 - 2);
if (towerHeight1 %2 == 0) {
towerHeight1++;
}
System.out.println(towerHeight1);

//Random odd height tower 2
Random Rand2 = new Random();
int towerHeight2 = 2 + Rand2.nextInt(11 - 2);
if (towerHeight2 %2 == 0) {
towerHeight2++;
}
System.out.println(towerHeight2);

//Random odd height tower 3
Random Rand3 = new Random();
int towerHeight3 = 2 + Rand3.nextInt(11 - 2);
if (towerHeight3 %2 == 0) {
towerHeight3++;
}
System.out.println(towerHeight3);

// Print blank line
System.out.println("");

/*Print Towers //scratch work
for (int w1 = 1; w1 <= towerWidth1; w1++)
{
System.out.print("*");
for (int w2 = 1; w2 <= towerWidth2; w2++)
{
System.out.print(" ");
for (int w3 = 1; w3 <= towerWidth3; w3++)
{
System.out.print(" ");
}
}
}*/

// Print Tower 1
for (int w = 1; w <= towerWidth1; w++)
{
for (int h = 1; h <= towerHeight1; h++)
{
System.out.print("*");
}
System.out.println();
}

// Print Tower 2
for (int w = 1; w <= towerWidth2; w++)
{
for (int h = 1; h <= towerHeight2; h++)
{
System.out.print("*");
}
for (int h = 1; h <= towerHeight3; h++)
{
System.out.print("*");
}
System.out.println();
}

// Print Tower 3
for (int w = 1; w <= towerWidth3; w++)
{
for (int h = 1; h <= towerHeight3; h++)
{
System.out.print("*");
}
System.out.println();
}


}
}


This is what the output I get looks like, it varies as the random numbers change.

enter image description here

When I went to the professor for help and he gave me pseudocode but I don't really understand it?

Does anyone have any idea?

Psuedocode:

For each row=maxHeight ... 1

print width many chars

if tower1 is at height row
then print it by printing width many * chars
else
then print width many space chars

print one space

if tower2 is at height row
then print it by printing width many * chars
else
then print width many space chars

Answer

Since this is your homework, the main implementation details are up to you, but I'd suggest breaking this down into some methods.

getFloor - prints width many c chars followed by a space.

private static void printFloor(int width, char c) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < width; i++) {
        sb.append(c);
    }
    sb.append(" ");
    System.out.print(sb.toString());
}

Then, that psudeocode translates into this

// For each row=maxHeight ... 1
for (int height = maxHeight; height >= 0; --height) {
    printFloor(w0, '*'); // print width many chars

    if (tower1 >= height) { // if tower1 is at height row
        printFloor(w1, '*');
    } else {
        printFloor(w1, ' ');
    }

    if (tower2 >= height) { // if tower2 is at height row
        printFloor(w2, '*');
    } else {
        printFloor(w2, ' ');
    }

    System.out.println(); // Start next height
}

You need to figure out how to get int maxHeight = max(tower0, tower1, tower2). As well as sort that into descending order.

Your code should get already get you the widths (w0, w1, w2).