Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rectangle {
- private int width, height;
- public Rectangle(int width, int height) {
- this.setWidth(width);
- this.setHeight(height);
- }
- public Rectangle() { // עם בנאי בלי נתונים אז מפנה נתונים 10 ו-10 בעזרת this() אל הבנאי אשר מקבל 2 נתונים
- this(10,10);
- }
- public int getWidth() {
- return width;
- }
- public boolean setWidth(int width) { //מבצע בדיקה אם הנתון חוקי ושם אותו לתכונה או נותן הודעה בהתאם
- if (width > 0) {
- this.width = width;
- return true;
- } else {
- System.out.println("Wrong width!");
- return false;
- }
- }
- public int getHeight() {
- return height;
- }
- public boolean setHeight(int height) { //מבצע בדיקה אם הנתון חוקי ושם אותו לתכונה או נותן הודעה בהתא
- if (height > 0) {
- this.height = height;
- return true;
- } else {
- System.out.println("Wrong height!");
- return false;
- }
- }
- public int perimeter(){
- return this.height*2+this.width*2; //היקף
- }
- public int sqaure(){
- return this.height*this.width; //שטח
- }
- public void print(){
- this.print('*'); // כמו במקרה של בנאי אפשר לעשות פונקציות עם אותו שם ולשלוח נתונים בעזרת this.
- }
- public void print(char sym){ //שיטה בלי שימוש במערך
- for (int i = 0; i < this.height; i+=1) { //מדפיס צלעות ימין ושמאל למטה ולמעלה כל מה שביניהם בריק
- for (int j = 0; j < this.width; j += 1) {
- if ((j == 0) || (i==0) || (j == this.width-1) ||(i==this.height-1)) { //בגלל שהדפסנו 2 צלעות(איי = 0 וג'יי =0) צריך להדפיס סימן אחד פחות לאורך ואחד פחות לרוחב
- System.out.print(sym);
- } else {
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- }
- }
- public static void main(String[] args) {
- Rectangle rect = new Rectangle(); // בנאי בלי נתונים אורך ורוחב נהיו 10
- System.out.println( rect.sqaure()+" "+ rect.perimeter()); //מדפיס היקף ושטח
- rect.print(); // מצייר מלבן עם כוכביות
- rect = new Rectangle(40,3);// רוחב 40 אורך 3
- System.out.println( rect.sqaure()+" "+ rect.perimeter()); //מדפיס היקף ושטח
- rect.print('%'); //מדפיס עם סימן '%'
- {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement