Learning Objectives: . Practice the use of nested for loops . Use pseudo-code wi
ID: 3758536 • Letter: L
Question
Learning Objectives: . Practice the use of nested for loops . Use pseudo-code with a top-down refinement strategy to build algorithms · Implement a sentinel controlled loop to gather user input . Use a switch statement to respond to user choices Declare and use static methods pattern1 pattern2 pattern3 pattern4 Gg@@@ea@@g@ Gaee@e@e GGC G@e@G Gge@a@@a@GC Gg@@@Ga Ge@e@@@ce G@@ee@ G*GC CG C G@e@*@@@ GaeG@CaG G@eGG*@@a G@@e@@ GaeG*@@GC aeee @@e e*GC G@@e GaG*@G Gee@e@@@@ G@@e C C @ Description: Create a new project APatterns that includes 2 files: Patterns.java and PatternsTest.java The class Patterns includes 4 methods that print one pattern each. Each pattern includes a parameter called size. This parameter determines the size of the triangles in rows. The triangles below are of size 9. Here is the UML class diagram of class Patterns: Output Choose a pattern (1-4) or to quit: 1 Size: 4 @OOO Choose a pattern (1-4) or to quit: 2 Size: 3 @@oaa @aa Pattens Choose a pattern (1-4) or e to quit: ;3 Size: 7 + pattern1 (size int) +pattern2 (size int) + pattern3 (size int + pattern4 (size int @* a 0@@ @@a@*@@aa @Oaa*a@@@ Notice that the operations are underlined. That indicates that the methods Choose a pattern (1-4) or e to quit: 4 Size: 5 @@oa*@@a@ to quit: 4 are static. Turning in: Ensure that you included a comment with your name and the assignment number on top of each code file. Zip up your project and name the file APatterns.zip Turn it in via Canvas @*a Choose a pattern (1-4) or e to quit: eExplanation / Answer
//patterns.java
public class Patterns {
public void pattern1(int n) {
for (int i = 0; i < n; i++) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("@");
}
System.out.print("@");
for (int j = 0; j < i; j++) {
System.out.print("@");
}
System.out.println();
}
}
public void pattern2(int n) {
for (int i = n-1; i >= 0; i--) {
for (int j = n-1; j > i; j--) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("@");
}
System.out.print("@");
for (int j = 0; j < i; j++) {
System.out.print("@");
}
System.out.println();
}
}
public void pattern3(int n) {
for (int i = 0; i < n; i++) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("@");
}
System.out.print("*");
for (int j = 0; j < i; j++) {
System.out.print("@");
}
System.out.println();
}
}
public void pattern4(int n) {
for (int i = n-1; i >= 0; i--) {
for (int j = n-1; j > i; j--) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("@");
}
System.out.print("*");
for (int j = 0; j < i; j++) {
System.out.print("@");
}
System.out.println();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
//patternstest.java
package m6;
import java.util.Scanner;
public class PatternsTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Patterns p = new Patterns();
while (true) {
System.out.print("Choose a pattern (1-4) or 0 to quit: ");
int c = sc.nextInt();
if(c == 0) {
System.out.println("Good Bye");
return;
}
System.out.print("Size: ");
int size = sc.nextInt();
switch (c) {
case 1:
p.pattern1(size);
break;
case 2:
p.pattern2(size);
break;
case 3:
p.pattern3(size);
break;
case 4:
p.pattern4(size);
break;
default:
System.out.println(" Wrong input!! ");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.