Instructions
ObjectiveWrite a Java assignment program to create patterns of various kinds using the programming language.
Requirements and Specifications
Here in this program, we are going to create patterns of various kinds using different signs and characters.
Source CodeDRAW
public class Draw {
public static String boxes(int height, int width, char c1, char c2) {
// checking arguments
if (height < 7 || height > 20 || width < 7 || width > 20) {
throw new IllegalArgumentException();
}
// creating char array representing final picture
char[][] arr = new char[height][width];
int currHeight = height;
int currWidth = width;
int step = 0;
// drawing frame by frame, until fram size is positive
while (currHeight > 0 && currWidth > 0) {
// choosing character according to step parity
char c = step % 2 == 0 ? c1 : c2;
// drawing horizontal lines
for (int i = 0; i < currWidth; i++) {
arr[step][step + i] = c;
arr[step + currHeight - 1][step + i] = c;
}
// drawing vertical lines
for (int i = 0; i < currHeight; i++) {
arr[step + i][step] = c;
arr[step + i][step + currWidth - 1] = c;
}
// next frame will have smaller size
currHeight -= 2;
currWidth -= 2;
step++;
}
return createString(arr, height, width);
}
public static String stripes(int height, int width, char c1, char c2, boolean isHorizontal) {
// validating arguments
if (height < 5 || height > 25 || width < 5 || width > 25) {
throw new IllegalArgumentException();
}
if (isHorizontal && height % 3 != 0) {
throw new IllegalArgumentException();
}
if (!isHorizontal && width % 3 != 0) {
throw new IllegalArgumentException();
}
// creating char array representing final picture
char[][] arr = new char[height][width];
// filling each cell of it
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// calculating necessary symbol
if (isHorizontal) {
arr[i][j] = (i / (height / 3) == 1) ? c2 : c1;
} else {
arr[i][j] = (j / (width / 3) == 1) ? c2 : c1;
}
}
}
return createString(arr, height, width);
}
private static String createString(char[][] arr, int height, int width) {
// creating final String using StringBuilder
StringBuilder builder = new StringBuilder();
for (int i = 0; i < height; i++) {
// adding next char array row to string
builder.append(new String(arr[i]));
if (i < height - 1) {
// if row is not last, adding \n
builder.append("\n");
}
}
// returning result
return builder.toString();
}
}
TEST QUESTION
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
public class TestsQuestionThree {
@Test
public void testPartA() {
try {
Draw.class.getDeclaredMethod("boxes", int.class, int.class, char.class, char.class);
} catch (NoSuchMethodException ex) {
fail("boxes method signature incorrect");
}
try {
Draw.class.getDeclaredMethod("stripes", int.class, int.class, char.class, char.class, boolean.class);
} catch (NoSuchMethodException ex) {
fail("stripes method signature incorrect");
}
try {
Draw.boxes(-1, -1, 'x', 'o');
fail("Boxes: invalid input");
} catch (Exception ex) {}
try {
Draw.stripes(-1, -1, 'x', 'o', false);
fail("Stripes: invalid input");
} catch (Exception ex) {}
}
@Test
public void testPartB() {
//example 1
String s = Draw.boxes(7,7,'+', 'o');
String answer = "+++++++\n" +
"+ooooo+\n" +
"+o+++o+\n" +
"+o+o+o+\n" +
"+o+++o+\n" +
"+ooooo+\n" +
"+++++++";
assertEquals(answer, s);
//example 2
s = Draw.boxes(8,15,'|', '*');
answer = "|||||||||||||||\n" +
"|*************|\n" +
"|*|||||||||||*|\n" +
"|*|*********|*|\n" +
"|*|*********|*|\n" +
"|*|||||||||||*|\n" +
"|*************|\n" +
"|||||||||||||||";
assertEquals(answer, s);
}
@Test
public void testPartC() {
String s = Draw.stripes(5, 15,'+', 'o', false);
String answer = "+++++ooooo+++++\n" +
"+++++ooooo+++++\n" +
"+++++ooooo+++++\n" +
"+++++ooooo+++++\n" +
"+++++ooooo+++++";
assertEquals(answer, s);
s = Draw.stripes(9,15,'-', '|', true);
answer = "---------------\n" +
"---------------\n" +
"---------------\n" +
"|||||||||||||||\n" +
"|||||||||||||||\n" +
"|||||||||||||||\n" +
"---------------\n" +
"---------------\n" +
"---------------";
assertEquals(answer, s);
}
}
Similar Samples
Discover our collection of programming homework samples at ProgrammingHomeworkHelp.com. These examples demonstrate our proficiency in tackling diverse programming challenges across various languages and domains. Explore them to gain insights into our approach and expertise, ensuring confidence in choosing us for your programming assignment needs.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java