jeudi 29 janvier 2015

Java Triangle Pattern Printing

Trying to write a program that will print a number of triangle patterns, both hollow and solid. I have implemented this by using constructors, along with other utility methods. As of right now I am trying to display a triangle of size 7 with the following character (*).


for some reason I am only getting the following output: 0000. I have created a test triangle down below



Triangle s1 = new Triangle(7, '*');
s1.displaySolidUL();

public class Triangle {

// Declare & intialize data fields
final private static char defaultChar = '*';
final private static int defaultSize = 10;

private static char triangleChar;
private static int triangleSize;

private static int triangleCount = 0;


// Constructors
public Triangle() {
this(defaultSize, defaultChar);
}

public Triangle(int s) {
this(s, defaultChar);
}

public Triangle(char n) {
this(n, defaultChar);
}

public Triangle(int size, char character) {
if ((triangleSize <= 0) || (triangleSize > 50)) {
size = defaultSize;
// System.out.println(errorSizeMsg);
} else if (triangleChar == ' ') {
character = defaultChar;
// System.out.println(errorBlankChar);
} else {
size = triangleSize;
character = triangleChar;

// increment triangle count
triangleCount++;
}
}

// Accessors and Mutators
public int getSize() {
return triangleSize;
}

public char getChar() {
return triangleChar;
}

public void setSize(int size) {
triangleSize = size;
}

public void setChar(char character) {
triangleChar = character;
}


// Main methods for displaying triangles
public void displaySolidLL() {

}

public void displaySolidLR() {

}

public void displaySolidUL() {
for (int x = 0; x <= triangleSize; x++) {
for (int y = 0; y <= triangleSize; y++) {
if (y <= x) {
System.out.print(triangleChar);
}
}
}
System.out.println();
}

public void displaySolidUR() {

}


// Other utility classes
// Printing out new lines
public static void newLine(int numLines) {
for (int i = 0; i < numLines; i++) {
System.out.println();
}
}

// Display triangle count
public static void getTriangleCount() {
System.out.print("The total number of triangles printed equals: " + triangleCount);
}
}

Aucun commentaire:

Enregistrer un commentaire