/*
Samuel Ardrey
CS 111A
October 7th, 2010
Programming Lab 3: Parallelogram Program Extra Credit Solution
Output a parallelogram with its dimensions and the character output chosen by the user.
parallelogram.java
*/
import java.util.Scanner;
class ParallelogramExtra
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);
int length, width;
String charin;
char charout;
System.out.println("This program will output a parallelogram.");
System.out.print("How long do you want each side to be? ");
length = scan.nextInt();
System.out.print("How wide do you want the border to be? ");
width = scan.nextInt();
while (width > (length / 2)) //will operate if the user input a width that is more than half of the length of the output
{
System.out.println("The width must be between 1 and " + (length / 2));
System.out.print("How wide do you want the border to be? ");
width = scan.nextInt();
}
System.out.print("Please enter the character you want it to be made of: ");
charin = scan2.nextLine();
charout = charin.charAt(0);
for (int i = 1; i <= length; i ++)
{
if (i <= width * 2) //will operate if the output needs no spaces in the middle
{
for (int nospaces = 1; nospaces <= i; nospaces ++)
System.out.print(charout);
}
else //if the output requires spaces in the middle, this will continue
{
for (int l = 1; l <= width; l++) //outputs the left side (characters) of the line
System.out.print(charout);
for (int space = 1; space + (width * 2) <= i; space ++) //outputs middle spaces
System.out.print(" ");
for (int r = 1; r <= width; r++) //outputs the right side (characters) of the line
System.out.print(charout);
}
System.out.println();
}
for (int i = length - 1; i > 0; i--)
{
for (int outspace = 1; outspace <= length - i; outspace++) //adds spaces to beginning of line
System.out.print(" ");
if (i > width * 2) //if the output requires spaces in the middle, this will continue
{
for (int l = 1; l <= width; l++) //outputs the left side (characters) of the line
System.out.print(charout);
for (int inspace = 1; inspace + (width * 2) <= i; inspace ++) //outputs middle spaces
System.out.print(" ");
for (int r = 1; r <= width; r++) //outputs the right side (characters) of the line
System.out.print(charout);
}
else //will continue if the output needs no spaces in the middle
{
for (int nospace = i; nospace > 0 ; nospace --)
System.out.print(charout);
}
System.out.println();
}
}
}
/*
This program will output a parallelogram.
How long do you want each side to be? 6
How wide do you want the border to be? 1
Please enter the character you want it to be made of: @
@
@@
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@@
@
This program will output a parallelogram.
How long do you want each side to be? 9
How wide do you want the border to be? 5
The width must be between 1 and 4
How wide do you want the border to be? 3
Please enter the character you want it to be made of: *
*
**
***
****
*****
******
*** ***
*** ***
*** ***
*** ***
*** ***
******
*****
****
***
**
*
This program will output a parallelogram.
How long do you want each side to be? 4
How wide do you want the border to be? 5
The width must be between 1 and 2
How wide do you want the border to be? 2
Please enter the character you want it to be made of: o
o
oo
ooo
oooo
ooo
oo
o
*/
syntax highlighted by Code2HTML, v. 0.9