Fruit (מחלקת-על, weight מוגן) ו-Apple (יורשת, color פרטי, validWeight/ValidWeight בודקת טווח משקל 80–140).
Fruit (מחלקת-על, weight מוגן) ו-Apple (יורשת, color פרטי, validWeight/ValidWeight בודקת טווח משקל 80–140).
public class Fruit {
protected int weight;
public Fruit(int val) { weight = val; }
public int getWeight() { return weight; }
}
public class Apple extends Fruit {
private String color;
public Apple(int val, String col) {
super(val);
color = col;
}
public boolean validWeight() {
return weight > 80 && weight < 140;
}
}
סעיף א
לפניכם חמישה היגדים. קבעו לכל אחד מהם אם הוא נכון או אינו נכון, ונמקו את קביעתכם:
- המחלקה Fruit יורשת את הפעולה validWeight()/ValidWeight() מהמחלקה Apple.
- המחלקה Apple יורשת את כל התכונות ואת כל הפעולות של המחלקה Fruit.
- המחלקה Apple יכולה לגשת ישירות לתכונה weight של המחלקה Fruit.
- המחלקה Fruit יכולה לגשת לתכונה color של המחלקה Apple.
- למחלקה Apple אין פעולה toString()/ToString().
סעיף ב
לפניכם קטע קוד מהפעולה הראשית (main/Main) של המחלקה TestFruit: Fruit first = new Apple(100, "RED"); Fruit second = new Fruit(90); Apple third = new Apple(100, "RED"); בעבור כל אחת מההוראות שלפניכם קבעו אם היא תקינה או אינה תקינה. אם היא אינה תקינה, כתבו אם זו שגיאת ריצה או שגיאת הידור (קומפילציה):
- boolean b = first.validWeight();
- boolean b = second.validWeight();
- boolean b = ((Apple)first).validWeight();
- boolean b = ((Apple)second).validWeight();
- boolean b = ((Apple)first).color.equals(third.color);
סעיף ג
כתבו פעולה המקבלת מערך עצמים מטיפוס Object. הפעולה תדפיס כמה עצמים הם מטיפוס Apple, כמה עצמים מטיפוס Fruit ואינם מטיפוס Apple, וכמה עצמים הם לא מטיפוס Fruit.
public static void countTypes(Object[] arr)
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.