
Ashy A.
asked 11/01/20comp sci question
Write a program that produces an ASCII art representation of an hourglass using nested for loops. Use a class constant to make it possible to change the height of the hourglass.
Output when a class constant named SIZE is set to a value of 2:
|======|
\[4 spaces]/
\[2 spaces]/
||
/::\
/::::\
|======|
Output when a class constant named SIZE is set to a value of 3:
|========|
\[[6 spaces]/
\[4 spaces]/
\[2 spaces]/
||
/::\
/::::\
/::::::\
|========|
please put the amount of spaces that is stated in the place of the square brackets (and the stuff within them)
1 Expert Answer

Patrick B. answered 11/01/20
Math and computer tutor/teacher
class AsciiHourglass
{
private int N;
public AsciiHourglass( int n) { N=n; }
public int Get() { return(N); }
public void Draw()
{
int maxN = 2*(N+1);
int Nn = N+1;
for (int iLoop=0; iLoop<Nn; iLoop++)
{
if (iLoop==0)
{
System.out.print("|");
}
else
{
System.out.print("\\");
}
for (int jLoop=0; jLoop<maxN; jLoop++)
{
if (iLoop==0)
{
System.out.print("=");
}
else
{
System.out.print(" ");
}
}
if (iLoop==0)
{
System.out.print("|");
}
else
{
System.out.print("/");
}
System.out.println("");
maxN-=2;
} //for loop
System.out.println("||");
maxN = 2;
Nn = N+1;
for (int iLoop=0; iLoop<Nn; iLoop++)
{
if (iLoop==(Nn-1))
{
System.out.print("|");
}
else
{
System.out.print("/");
}
for (int jLoop=0; jLoop<maxN; jLoop++)
{
if (iLoop==Nn-1)
{
System.out.print("=");
}
else
{
System.out.print(":");
}
}
if (iLoop==Nn-1)
{
System.out.print("|");
}
else
{
System.out.print("\\");
}
System.out.println("");
maxN+=2;
} //for loop
}
public static void main(String args[])
{
int N=-1;
if (args.length>0)
{
N = Integer.parseInt(args[0]);
}
while (N<=0)
{
System.out.print("Please input # N:>");
String inbuff = System.console().readLine();
N = Integer.parseInt(inbuff);
}
AsciiHourglass x = new AsciiHourglass(N);
x.Draw();
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESOURCES under this link11/01/20