Project: For Loops with StringShapes

Collaboration Complete this project alone and/or with help from UofA section leaders in 228 Gould Simpson

In this project you are asked to develop a class StringShapes with methods that return various shapes as strings. Also write enough assertions in class StringShapesTest to obtain 100% problem coverage.

Setup: Create a project called StringShapes in Eclipse and get these two classes into your project:


All methods are provided as stubs in StringShapes.java. A stub is a method that compiles but does not work yet. Each stub currently returns a bogus string for a couple of reasons. The main reason is that this will avoid compile time errors when submitted to WebCat. Here is a summary of the methods provided in StringShapes.java. Each is explained in more detailed in the list of eight methods that follows. Each method should be implemented one at a time.

public String rectangle(int height, int width, char ch)

public String rightAngleUp(int size, char ch)

public String rightAngleDown(int size, char ch)

public String triangleRight(int size, char ch)

public String triangleUp(int size, char ch)

public String triangleDown(int size, char ch)

public String diamond(int size, char ch)

public String hourGlass(int size, char ch)

1)public String rectangle(int height, int width, char ch)

Return a rectangle as a string with height rows and width columns. For example, the following code should generate the string like the one shown to the right. Remember "\n" represents a new line. There must be a new line at the end of each and every line (row), including the last line (row). The string "on new line" was concatenated here in the hope that you will remember to include "\n" for each line. The expected values like "++++\n++++\n" must be written very carefully: no spaces and a new line "\n" at the end of every row, including the last (or Rick's WebCat test will complain).

@Test
publicvoid testRectangle() {
StringShapes shape = new StringShapes();
// Create a StringShape with a height of 2, a width of 4 '+'s.
// Compare to a String with new line characters backslash n: "\n"
String aRectangle = shape.rectangle(2, 4, '+');
assertEquals("++++\n++++\n", aRectangle);
// Show what the string looks like with new lines as "\n"
System.out.println(aRectangle + "on new line");
} / Output
should be:
++++
++++
on new line

2) public String rightAngleUp(int size, char ch)

Return a right triangle as a string where the base has sizecharacters ch, and the top row has size 1. For example, the following code should generate the string like the one to the right. Remember "\n" represents a new line. There must be a new line at the end at the end of every line, including the last line.

@Test
publicvoid testRightAngleUp() {
StringShapes s = new StringShapes();
String aRightAngleUp= s.rightAngleUp(6, '*');
assertEquals("*\n**\n***\n****\n*****\n******\n", aRightAngleUp);
} / *
**
***
****
*****
******

3) public String rightAngleDown(int size, char ch)

Return a right triangle as a string where the top row has size characters ch, and the last row has 1. For example, the following code should generate the string like the one to the right. Remember "\n" represents a new line. There must be a new line at the end at the end of every line.

@Test
publicvoid testRightAngleDown() {
StringShapes s = new StringShapes();
String aRightAngleDown = s.rightAngleDown(4, '$');
assertEquals("$$$$\n$$$\n$$\n$\n", aRightAngleDown);
} / $$$$
$$$
$$
$

4) public String triangleRight(int size, char ch)

Return a triangle as a string where the middle row ##### hassize+1 characters ch. The top part is a rightAngleUp(size, ch). The bottom is a rightAngleDown(size, ch). For example, the following triangleRight message would return the triangle shown.

Hint: You should use previous implemented (coded and tested) methods to solve this easily :-)

@Test
publicvoid testTriangleRight() {
StringShapes s = new StringShapes();
String aString = s.triangleRight(4, '#');
// Note that this is a rightAngleUp(size, ch)
// followed by a rightAngleDown(size, ch)
assertEquals("#\n##\n###\n####\n####\n###\n##\n#\n", aString);
} / #
##
###
####
####
###
##
#

6) public String triangleDown(int size, char ch)

Return a triangle as a string of size rows where the top row has (2n-1) chars ch, and all subsequent rows have two fewer characters. Pad the correct number of spaces on the left! triangleDown() messages would return triangles like this:

######### @@@@@@@ ***

####### @@@@@ *

##### @@@

### @

#

@Test
publicvoid testTriangleDown() {
StringShapes s = new StringShapes();
String aTriangleDown= s.triangleDown(4, '#');
assertEquals("#######\n #####\n ###\n #\n", aTriangleDown);
} / #######
#####
###
#

5) public String triangleUp(int size, char ch)

Return a triangle as a string of size rows where the top row has one char ch, and all subsequent rows have two more characters. You must pad the correct number of spaces to the left!

@Test
publicvoid testTriangleUp() {
StringShapes s = new StringShapes();
String tUp = s.triangleUp(5, '.');
System.out.println(tUp);
assertEquals(" .\n ...\n .....\n ...... \n...... \n", tUp);
} / .
...
.....
......
......

7) public String diamond(int size, char ch)

Return a diamond as a string of 2n rows using the character ch. diamond() messages would return a diamond as triangleUp concatenated to a triangleDown.

Hint: You should use previous implemented (coded and tested) methods to solve this easily :-)

@Test
publicvoid testDiamond() {
StringShapes s = new StringShapes();
String aDiamond = s.diamond(5, '#');
assertEquals(" #\n ###\n #####\n #######\n" + "#########\n" +
"#########\n #######\n #####\n ###\n #\n", aDiamond);
} / #
###
#####
#######
#########
#########
#######
#####
###
#

8) public String hourGlass(int size, char ch)

Return a hourglass of 2n rows and the character provided. The top and bottom rows have 2n-1 characters and the middle two rows have only one.

Hint: You should use previous implemented (coded and tested) methods to solve this easily :-)

@Test
publicvoid testHourGlass() {
StringShapes s = new StringShapes();
String anHourGlass = s.hourGlass(5, '#');
assertEquals("#########\n #######\n #####\n ###\n #\n #\n" +
" ###\n" + " #####\n #######\n#########\n", anHourGlass);
} / #########
#######
#####
###
#
#
###
#####
#######
#########

Grading Criteria 100 points Subject to change

•Your code must compile using the specified names

•Rick's tests on WebCat must pass

•You have no infinite loops caused by Rick's test. This generates a "Time-Out" error and your WebCat grade will be 0.