Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class prog
- {
- private static Scanner keyboard = new Scanner(System.in);
- public static void main(String[] args)
- {
- int length = 0;
- int width = 0;
- char chara;
- String quit = " ";
- while (!quit.equals("no")) {
- length = getLength();
- width = getWidth();
- chara = getSymbol();
- if(length < 0|| width < 0) printUnfilledRectangle(length, width, chara);
- else printFilledRectangle(length, width, chara);
- quit = askIfMore();
- }
- }
- public static void printMiddleLines(int length, int width, char chara, char charb) {
- for (int county = 0; county < Math.abs(width)-2; county++) { // middle lines
- System.out.print(chara);
- for (int countx = 0; countx < Math.abs(length)-2; countx++) {
- System.out.print(charb);
- }
- System.out.print(chara);
- System.out.println();
- }
- }
- public static void printFilledRectangle(int length, int width, char chara) {
- printTopLine(length, chara);
- printMiddleLines(length, width, chara, chara);
- printBottomLine(length, chara);
- }
- public static void printUnfilledRectangle(int length, int width, char chara) {
- printTopLine(length, chara);
- printMiddleLines(length, width, chara, ' ');
- printBottomLine(length, chara);
- }
- public static void printTopLine(int length, char chara) {
- for (int countx = 0; countx < Math.abs(length); countx++) {//top line
- System.out.print(chara);
- }
- System.out.println();
- }
- public static void printBottomLine(int length, char chara) {
- for (int countx = 0; countx < Math.abs(length); countx++) {//bottom line
- System.out.print(chara);
- }
- System.out.println();
- }
- public static char getSymbol() {
- char chara;
- System.out.print("Enter the symbol to be used: ");
- chara =keyboard.next().charAt(0);
- return chara;
- }
- public static int getLength() {
- int length = 0;
- System.out.print("Enter the length of the rectangle (-10 to +10): ");
- length = keyboard.nextInt();
- while (Math.abs(length) < -10 || Math.abs(length) > 10||length==0) {
- System.out.println("Input Error -----");
- System.out.print("Enter the length of the rectangle (-10 to +10): ");
- length = keyboard.nextInt();
- }
- return length;
- }
- public static int getWidth() {
- int width;
- System.out.print("Enter the non zero length of the rectangle (-10 to +10): ");
- width = keyboard.nextInt();
- while (Math.abs(width) < -10 || Math.abs(width) > 10||width==0) {
- System.out.println("Input Error -----");
- System.out.print("Enter the non zero length of the rectangle (-10 to +10): ");
- width = keyboard.nextInt();
- }
- return width;
- }
- public static String askIfMore() {
- String quit = "";
- System.out.println("Do you want another rectangle, Yes or No: ");
- quit = keyboard.next();
- quit = quit.toLowerCase();
- return quit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement