/* Evan Donahey
CS 111A
PP5
The parallelogram program obtains an int value and a character type from the user, then utilizes that data for drawing a
hollow parallelogram. The value is used to determine the length of the sides. The desired character is used in the design.
*/
import java.util.Scanner;
class parallelogram
{ // begin class
public static void main (String args [])
{ // begin main
int lengthValue;
char designCharacter;
Scanner userInput = new Scanner (System.in);
System.out.println ("This program will output a parallelogram."); //output program info
System.out.print ("How long do you want each side to be? "); //output prompt and obtain parallelogram size info
lengthValue = userInput.nextInt();
userInput.nextLine(); // dispose of newline char
System.out.print ("Please enter the character you want it to be made of: "); //output prompt and obtain design character
designCharacter = ((userInput.nextLine()).charAt(0));
displayUpperPortion (lengthValue, designCharacter); //call method to display upper-half of parallelogram
displayLowerPortion (lengthValue, designCharacter); //call method to display lower-half of parallelogram
} // end main
/* Method displayUpperPortion outputs the upper half of the parallelogram. The outer for loop is only utilized to output the
correct side length, that is to say, that the number of rows equals the number input from the user. As a note, instead of
thinking of this as two triangles, I have been looking at it as being two square fields. Thus, the enbedded while loop,
repeats until the number of characters that have been output (either the design character or a blank space) is also equal
to the number input from the user. The if/else condition statements evaluate the "cursor" position and determine whether
the design character or an empty space should be printed */
static void displayUpperPortion (int sideValue, char design)
{ //begin method displayUpperPortion
int characterOutputCounter;
//outer for loop outputs \n until the correct side length is reached
for (int counter = 1; counter <= sideValue; counter++)
{
characterOutputCounter = 1;
System.out.print ("\n");
while (characterOutputCounter <= counter) //while loop executes until the correct width is attained (middle row of design)
{
if (characterOutputCounter == 1 || characterOutputCounter == counter) //decides whether to print a space or character
{
System.out.print (design);
characterOutputCounter++;
}
else
{
System.out.print (" ");
characterOutputCounter++;
}
} //end while loop
} //end outer for loop
} //end method displayUpperPortion
/* Method displayLowerPortion is very much like method displayUpperPortion. However, in this method I introduce variable
angleDisplay which is used to calculate the diagonal line. More specifically, it is used in the while loop and it's if/else
conditions when determining whether to output the design character or a blank space. Please note that the final
System.out.println () is completely unnecessary. Being that the output from this file will be typescripted, I am
including it simply to add a line after the parallelogram is displayed*/
static void displayLowerPortion (int sideValue, char design)
{ //begin method displayLowerPortion
int angleDisplay = 2, counter, characterOutputCounter;
for (counter = 1; counter < sideValue; counter++) //outer for loop outputs \n until the correct side length is reached
{
characterOutputCounter = 1;
System.out.print ("\n");
while (characterOutputCounter <= sideValue) //while loop executes until the correct width is attained (middle row of design)
{
if (characterOutputCounter == angleDisplay || characterOutputCounter == sideValue) //decides whether to print a space or character
{
System.out.print (design);
characterOutputCounter++;
}
else
{
System.out.print (" ");
characterOutputCounter++;
}
} //end while loop
angleDisplay++; //increment angleDisplay to continue the output of the diagonal line
} //end outer for loop
System.out.println();
} //end method displayLowerPortion
} // end class parallelogram
/*
Script started on Tue Mar 30 17:47:55 2004
bash-2.04$ java parallelogram
This program will output a parallelogram.
How long do you want each side to be? 6
Please enter the character you want it to be made of: @
@
@@
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@@
@
bash-2.04$ java parallelogram
This program will output a parallelogram.
How long do you want each side to be? 9
Please enter the character you want it to be made of: *
*
**
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**
*
bash-2.04$ exit
exit
script done on Tue Mar 30 17:48:36 2004
*/
syntax highlighted by Code2HTML, v. 0.9