Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Draws shapes with user-defined characters and lengths.
- *
- * @author Marcus Marchetti
- * @version Version 9001, 12/11/15
- */
- public class ShapeDrawer {
- /**
- * The character being used in the shapes.
- */
- private char character;
- /**
- * Creates a new ShapeDrawer with a default chatacter of "*".
- */
- public ShapeDrawer() {
- character = '*';
- }
- /**
- * Gets the current character being used.
- *
- * @return character The character being used.
- */
- public char getCharacter() {
- return character;
- }
- /**
- * Sets a new character to be used.
- *
- * @param newCharacter The new character to be used.
- */
- public void setCharacter(char newCharacter) {
- character = newCharacter;
- }
- /**
- * Creates a left lower right triangle.
- *
- * @param length The length of the triangle.
- */
- public void leftLowerTriangle(int length) {
- if (length < 1) {
- throw new IllegalArgumentException("Length cannot be less than 1!");
- } else {
- for (int i = 1; i <= length; i++) {
- for (int j = 0; j < i; j++) {
- System.out.print(character);
- }
- System.out.println("");
- }
- }
- }
- /**
- * Creates a left upper right triangle.
- *
- * @param length The length of the triangle.
- */
- public void leftUpperTriangle(int length) {
- if (length < 1) {
- throw new IllegalArgumentException("Length cannot be less than 1!");
- } else {
- for (int j = length; j > 0; j--) {
- for (int i = 0; i < j; i++) {
- System.out.print(character);
- }
- System.out.println("");
- }
- }
- }
- /**
- * Creates a right lower right triangle.
- *
- * @param length The length of the triangle.
- */
- public void rightLowerTriangle(int length) {
- if (length < 1) {
- throw new IllegalArgumentException("Length cannot be less than 1!");
- } else {
- for (int i = 0; i < length; i++) {
- for (int j = 2; j <= length - i; j++) {
- System.out.print(" ");
- }
- for (int k = 0; k <= i; k++) {
- System.out.print(character);
- }
- System.out.println("");
- }
- }
- }
- /**
- * Creates a right upper right triangle.
- *
- * @param length The length of the triangle.
- */
- public void rightUpperTriangle(int length) {
- if (length < 1) {
- throw new IllegalArgumentException("Length cannot be less than 1!");
- } else {
- int placeHolder = 0;
- for (int i = length; i >= 1; i--) {
- placeHolder++;
- for (int j = 1; j <= i; j++) {
- System.out.print(character);
- }
- System.out.println("");
- for (int k = 1; k <= placeHolder; k++) {
- System.out.print(" ");
- }
- }
- }
- }
- /**
- * Creates a diamond shape.
- *
- * @param length The length of the diamond.
- */
- public void diamond(int length) {
- if (length < 1 || (length % 2) == 0) {
- throw new IllegalArgumentException("Length cannot be less than one or an even number!");
- } else {
- int size = length;
- for (int i = 1; i < size; i += 2) {
- for (int k = size; k >= i; k -= 2) {
- System.out.print(" ");
- }
- for (int j = 1; j <= i; j++) {
- System.out.print(character);
- }
- System.out.println("");
- }
- for (int i = 1; i <= size; i += 2) {
- for (int k = 1; k <= i; k += 2) {
- System.out.print(" ");
- }
- for (int j = size; j >= i; j--) {
- System.out.print(character);
- }
- System.out.println("");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement