Advertisement
Goodiny777

Rectangle Class and main() for test

Jan 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. public class Rectangle {
  2.     private int width, height;
  3.  
  4.     public Rectangle(int width, int height) {
  5.         this.setWidth(width);
  6.         this.setHeight(height);
  7.     }
  8.  
  9.     public Rectangle() { // עם בנאי בלי נתונים אז מפנה נתונים 10 ו-10 בעזרת this() אל הבנאי אשר מקבל 2 נתונים
  10.         this(10,10);
  11.     }
  12.  
  13.     public int getWidth() {
  14.         return width;
  15.     }
  16.  
  17.     public boolean setWidth(int width) {   //מבצע בדיקה אם הנתון חוקי ושם אותו לתכונה או נותן הודעה בהתאם
  18.         if (width > 0) {
  19.             this.width = width;
  20.             return true;
  21.         } else {
  22.             System.out.println("Wrong width!");
  23.             return false;
  24.         }
  25.     }
  26.  
  27.     public int getHeight() {
  28.         return height;
  29.     }
  30.  
  31.     public boolean setHeight(int height) {   //מבצע בדיקה אם הנתון חוקי ושם אותו לתכונה או נותן הודעה בהתא
  32.         if (height > 0) {
  33.             this.height = height;
  34.             return true;
  35.         } else {
  36.             System.out.println("Wrong height!");
  37.             return false;
  38.         }
  39.     }
  40.  
  41.     public int perimeter(){
  42.         return this.height*2+this.width*2;  //היקף
  43.     }
  44.  
  45.     public int sqaure(){
  46.         return  this.height*this.width;  //שטח
  47.     }
  48.  
  49.     public void print(){
  50.         this.print('*'); // כמו במקרה של בנאי אפשר לעשות פונקציות עם אותו שם ולשלוח נתונים בעזרת this.
  51.     }
  52.  
  53.     public void print(char sym){  //שיטה בלי שימוש במערך
  54.         for (int i = 0; i < this.height; i+=1) {      //מדפיס צלעות ימין ושמאל למטה ולמעלה כל מה שביניהם בריק
  55.             for (int j = 0; j < this.width; j += 1) {
  56.                 if ((j == 0) || (i==0) || (j == this.width-1) ||(i==this.height-1)) { //בגלל שהדפסנו 2 צלעות(איי = 0 וג'יי =0) צריך להדפיס סימן אחד פחות לאורך ואחד פחות לרוחב
  57.                     System.out.print(sym);
  58.                 } else {
  59.                     System.out.print(" ");
  60.                 }
  61.             }
  62.             System.out.println();
  63.         }
  64.     }
  65. }
  66.  
  67.  public static void main(String[] args) {
  68.     Rectangle rect = new Rectangle();  // בנאי בלי נתונים אורך ורוחב נהיו 10
  69.     System.out.println( rect.sqaure()+" "+ rect.perimeter()); //מדפיס היקף ושטח
  70.     rect.print(); // מצייר מלבן עם כוכביות
  71.  
  72.     rect = new Rectangle(40,3);// רוחב 40 אורך 3
  73.     System.out.println( rect.sqaure()+" "+ rect.perimeter()); //מדפיס היקף ושטח
  74.     rect.print('%'); //מדפיס עם סימן '%'
  75. {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement