Hw 4 ans
Wed Oct 16 2024 15:30:05 GMT+0000 (Coordinated Universal Time)
Saved by @shivamp
import java.util.Scanner;
public class Patterns {
// Main method to test the functionality of the patterns
public static void main(String[] args) {
// Call method to print design patterns
printPatterns();
// Call method to print horizontal bars
printHorizontalBars();
// Call method to print vertical bars
printVerticalBars();
} // end main
// Method to print various patterns using nested loops
public static void printPatterns() {
System.out.println("printPatterns() method called....");
// First pattern
for (int row = 1; row <= 8; row++) {
for (int col = 1; col <= 8; col++) {
if (row + col <= 9) {
System.out.print("# ");
} else {
System.out.print(" ");
}
}
System.out.println(); // Move to next line after each row
}
// Second pattern
for (int row = 1; row <= 8; row++) {
for (int col = 1; col <= 8; col++) {
if (row <= 9 - col) {
System.out.print("# ");
} else {
System.out.print(" ");
}
}
System.out.println(); // Move to next line after each row
}
// Third pattern
for (int row = 1; row <= 8; row++) {
for (int col = 1; col <= 8; col++) {
if (row == 1 || row == 8 || col == 1 || col == 8) {
System.out.print("# ");
} else if (row == col) {
System.out.print("# ");
} else {
System.out.print(" ");
}
}
System.out.println(); // Move to next line after each row
}
// Fourth pattern
for (int row = 1; row <= 8; row++) {
for (int col = 1; col <= 8; col++) {
if (row == 1 || row == 8 || col == 1 || col == 8 || row + col == 9) {
System.out.print("# ");
} else {
System.out.print(" ");
}
}
System.out.println(); // Move to next line after each row
}
}
// Method to print horizontal bars based on user input
public static void printHorizontalBars() {
Scanner sc = new Scanner(System.in);
int num1, num2, num3, num4;
System.out.println("printHorizontalBars() method called....");
System.out.println("Enter four integers:");
// Read the numbers from the keyboard
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();
num4 = sc.nextInt();
// Print the horizontal bars
printStars(num1);
printStars(num2);
printStars(num3);
printStars(num4);
}
// Helper method to print a given number of stars
private static void printStars(int count) {
for (int i = 0; i < count; i++) {
System.out.print("* ");
}
System.out.println(); // Move to next line after printing stars
}
// Method to print vertical bars based on user input
public static void printVerticalBars() {
Scanner sc = new Scanner(System.in);
int num1, num2, num3, num4;
System.out.println("printVerticalBars() method called....");
System.out.println("Enter four integers:");
// Read the numbers from the keyboard
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();
num4 = sc.nextInt();
// Find the maximum number of stars in any column
int max = Math.max(num1, Math.max(num2, Math.max(num3, num4)));
// Print the vertical bars
for (int i = max; i > 0; i--) {
if (i <= num1) System.out.print("** ");
else System.out.print(" ");
if (i <= num2) System.out.print("** ");
else System.out.print(" ");
if (i <= num3) System.out.print("** ");
else System.out.print(" ");
if (i <= num4) System.out.print("** ");
else System.out.print(" ");
System.out.println(); // Move to the next line after each level
}
// Print the row of dashes
System.out.println("------------------------------");
}
}



Comments