Advertisement
a1ananth

Temp paste

Oct 23rd, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. import java.util.*;
  2. public class prog
  3. {
  4.     private static Scanner keyboard = new Scanner(System.in);
  5.    
  6.     public static void main(String[] args)
  7.     {
  8.         int length = 0;
  9.         int width = 0;
  10.         char chara;
  11.         String quit = " ";
  12.  
  13.         while (!quit.equals("no")) {
  14.             length = getLength();
  15.             width = getWidth();
  16.             chara = getSymbol();
  17.            
  18.             if(length < 0|| width < 0) printUnfilledRectangle(length, width, chara);
  19.             else printFilledRectangle(length, width, chara);
  20.            
  21.             quit = askIfMore();
  22.         }
  23.     }
  24.    
  25.     public static void printMiddleLines(int length, int width, char chara, char charb) {
  26.         for (int county = 0; county < Math.abs(width)-2; county++) { // middle lines
  27.             System.out.print(chara);
  28.             for (int countx = 0; countx < Math.abs(length)-2; countx++) {
  29.                 System.out.print(charb);
  30.             }
  31.             System.out.print(chara);
  32.             System.out.println();
  33.         }
  34.     }
  35.    
  36.     public static void printFilledRectangle(int length, int width, char chara) {
  37.         printTopLine(length, chara);
  38.         printMiddleLines(length, width, chara, chara);
  39.         printBottomLine(length, chara);
  40.     }
  41.    
  42.     public static void printUnfilledRectangle(int length, int width, char chara) {
  43.         printTopLine(length, chara);
  44.         printMiddleLines(length, width, chara, ' ');
  45.         printBottomLine(length, chara);
  46.     }
  47.    
  48.     public static void printTopLine(int length, char chara) {
  49.         for (int countx = 0; countx < Math.abs(length); countx++) {//top line
  50.             System.out.print(chara);
  51.         }
  52.        
  53.         System.out.println();
  54.     }
  55.    
  56.     public static void printBottomLine(int length, char chara) {
  57.         for (int countx = 0; countx < Math.abs(length); countx++) {//bottom line
  58.             System.out.print(chara);
  59.         }
  60.        
  61.         System.out.println();
  62.     }
  63.    
  64.     public static char getSymbol() {
  65.         char chara;
  66.        
  67.         System.out.print("Enter the symbol to be used: ");
  68.         chara =keyboard.next().charAt(0);
  69.        
  70.         return chara;
  71.     }
  72.    
  73.     public static int getLength() {
  74.         int length = 0;
  75.         System.out.print("Enter the length of the rectangle (-10 to +10): ");
  76.         length = keyboard.nextInt();
  77.  
  78.         while (Math.abs(length) < -10 || Math.abs(length) > 10||length==0) {
  79.             System.out.println("Input Error -----");
  80.  
  81.             System.out.print("Enter the length of the rectangle (-10 to +10): ");
  82.             length = keyboard.nextInt();
  83.         }
  84.        
  85.         return length;
  86.     }
  87.    
  88.     public static int getWidth() {
  89.         int width;
  90.        
  91.         System.out.print("Enter the non zero length of the rectangle (-10 to +10): ");
  92.         width = keyboard.nextInt();
  93.  
  94.         while (Math.abs(width) < -10 || Math.abs(width) > 10||width==0) {
  95.             System.out.println("Input Error -----");
  96.            
  97.             System.out.print("Enter the non zero length of the rectangle (-10 to +10): ");
  98.             width = keyboard.nextInt();
  99.         }
  100.        
  101.         return width;
  102.     }
  103.    
  104.     public static String askIfMore() {
  105.         String quit = "";
  106.        
  107.         System.out.println("Do you want another rectangle, Yes or No: ");
  108.         quit = keyboard.next();
  109.         quit = quit.toLowerCase();
  110.        
  111.         return quit;
  112.     }
  113. }
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement