I have this program. The output of the program is “OH”.

How to shorten the program but with sticking to the same idea? Meaning, not making major changes to the program. Just having lesser codes.


import javax.swing.JOptionPane;

public class HouseB
{
public static void main(String args[])
{
int no_rows;
String str_no_rows;

str_no_rows = JOptionPane.showInputDialog("Enter row numbers (3 or 5):");
no_rows = Integer.parseInt(str_no_rows);

switch (no_rows)
{
case 3: {
print_x(0);
print_x(4);
print_x(0);
break;
}
case 5:
{
print_x(0);
print_x(4);
print_x(4);
print_x(4);
print_x(4);
print_x(4);
print_x(0);
break;
}
default:
JOptionPane.showMessageDialog(null, "Invalid data, Try again!");
break;
}


System.out.println(" ");


switch (no_rows)
{
case 3: {

print_x(4);
print_x(0);
print_x(4);
break;
}
case 5:
{
print_x(4);
print_x(4);
print_x(4);
print_x(0);
print_x(4);
print_x(4);
print_x(4);
break;
}
default:
JOptionPane.showMessageDialog(null, "Invalid data, Try again!");
break;
}
}


public static void print_x(int no_spaces)
{

if (no_spaces==0)
{

System.out.println("******");
}
else

System.out.print("*");

for (int i=0; i<no_spaces; i++)
{
System.out.print(" ");
}

System.out.println("*");
}
}