abstract ו-interfacesabstract & interfaces
מחלקות מופשטות, interfaces עם default ו-static methods, ומימוש של כמה interfaces.
Abstract classes, interfaces with default and static methods, and implementing several interfaces.
שכר לפי סוג עובדPay by employee type
תזכורת: מה ביקשוReminder: the task
abstract class Employee עם name ו-abstract int pay(). Hourly (תעריף × שעות) ו-Salaried (משכורת חודשית). במערך של Employee הדפיסו Noa: 5000 ו-Avi: 9000.
abstract class Employee with a name and abstract int pay(). Hourly (rate × hours) and Salaried (a monthly salary). In an Employee array print Noa: 5000 and Avi: 9000.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Employee[] staff = {new Hourly("Noa", 50, 100), new Salaried("Avi", 9000)};
for (Employee e : staff) {
System.out.println(e.getName() + ": " + e.pay());
}
}
}
abstract class Employee {
private final String name;
Employee(String name) {
this.name = name;
}
String getName() {
return name;
}
abstract int pay();
}
class Hourly extends Employee {
private final int rate;
private final int hours;
Hourly(String name, int rate, int hours) {
super(name);
this.rate = rate;
this.hours = hours;
}
@Override
int pay() {
return rate * hours;
}
}
class Salaried extends Employee {
private final int monthly;
Salaried(String name, int monthly) {
super(name);
this.monthly = monthly;
}
@Override
int pay() {
return monthly;
}
}Noa: 5000 Avi: 9000
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מחיר עם מע"מPrice with VAT
תזכורת: מה ביקשוReminder: the task
interface Priced עם price(), label() ו-default double withVat() (פי 1.17). Book ו-Coffee מממשות. הדפיסו Dune 70.20 ו-Coffee 14.04.
interface Priced with price(), label() and default double withVat() (times 1.17). Book and Coffee implement it. Print Dune 70.20 and Coffee 14.04.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Priced[] cart = {new Book("Dune", 60), new Coffee(12)};
for (Priced p : cart) {
System.out.println(p.label() + " " + String.format("%.2f", p.withVat()));
}
}
}
interface Priced {
double price();
String label();
default double withVat() {
return price() * 1.17;
}
}
class Book implements Priced {
private final String title;
private final double price;
Book(String title, double price) {
this.title = title;
this.price = price;
}
@Override
public double price() {
return price;
}
@Override
public String label() {
return title;
}
}
class Coffee implements Priced {
private final double price;
Coffee(double price) {
this.price = price;
}
@Override
public double price() {
return price;
}
@Override
public String label() {
return "Coffee";
}
}Dune 70.20 Coffee 14.04
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
צייד באגים: שכחו לממשBug hunter: forgot to implement
תזכורת: מה ביקשוReminder: the task
התוכנית לא מתקמפלת. צפו ב"איפה זה נשבר", ותקנו את Circle כך שיודפס 12.57.
The program doesn't compile. Watch "where it breaks", and fix Circle so it prints 12.57.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו איפה זה נשברWatch where it breaks
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Shape c = new Circle(2);
System.out.println(String.format("%.2f", c.area()));
}
}
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
private final double r;
Circle(double r) {
this.r = r;
}
@Override
double area() {
return Math.PI * r * r;
}
}12.57
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
למיין תלמידיםSorting students
תזכורת: מה ביקשוReminder: the task
Student implements Comparable<Student> לפי הציון (מהנמוך לגבוה). Arrays.sort על Noa 92, Avi 78, Dana 85, והדפיסו [Avi=78, Dana=85, Noa=92].
Student implements Comparable<Student> by grade (low to high). Arrays.sort Noa 92, Avi 78, Dana 85, and print [Avi=78, Dana=85, Noa=92].
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Student[] group = {new Student("Noa", 92), new Student("Avi", 78), new Student("Dana", 85)};
Arrays.sort(group);
System.out.println(Arrays.toString(group));
}
}
class Student implements Comparable<Student> {
private final String name;
private final int grade;
Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
@Override
public int compareTo(Student other) {
return Integer.compare(grade, other.grade);
}
@Override
public String toString() {
return name + "=" + grade;
}
}[Avi=78, Dana=85, Noa=92]
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
static ו-default ב-interfacestatic and default in an interface
תזכורת: מה ביקשוReminder: the task
interface Shape עם area(), default boolean isBiggerThan(Shape other), ו-static double totalArea(Shape[] shapes). Rect מממשת. עבור 2x3, 1x1, 4x5 הדפיסו 27.0 ואז true.
interface Shape with area(), default boolean isBiggerThan(Shape other), and static double totalArea(Shape[] shapes). Rect implements it. For 2x3, 1x1, 4x5 print 27.0 then true.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Shape[] shapes = {new Rect(2, 3), new Rect(1, 1), new Rect(4, 5)};
System.out.println(Shape.totalArea(shapes));
System.out.println(shapes[0].isBiggerThan(shapes[1]));
}
}
interface Shape {
double area();
default boolean isBiggerThan(Shape other) {
return area() > other.area();
}
static double totalArea(Shape[] shapes) {
double sum = 0;
for (Shape s : shapes) {
sum += s.area();
}
return sum;
}
}
class Rect implements Shape {
private final double w;
private final double h;
Rect(double w, double h) {
this.w = w;
this.h = h;
}
@Override
public double area() {
return w * h;
}
}27.0 true
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 20: interface בשם LendableShelf, step 20: a Lendable interface
תזכורת: מה ביקשוReminder: the task
interface Lendable עם name(), borrower(), lendTo, giveBack ו-default String status() (Dune: lent to Noa או DVD Alien: available). Book ו-Dvd מממשות. השאילו את Dune ל-Noa, הדפיסו את המצב של שניהם, החזירו, והדפיסו שוב את Dune.
interface Lendable with name(), borrower(), lendTo, giveBack and default String status() (Dune: lent to Noa or DVD Alien: available). Book and Dvd implement it. Lend Dune to Noa, print both statuses, give it back, and print Dune again.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Lendable[] items = {new Book("Dune"), new Dvd("Alien")};
items[0].lendTo("Noa");
for (Lendable x : items) {
System.out.println(x.status());
}
items[0].giveBack();
System.out.println(items[0].status());
}
}
interface Lendable {
String name();
String borrower();
void lendTo(String person);
void giveBack();
default String status() {
return borrower() == null ? name() + ": available" : name() + ": lent to " + borrower();
}
}
class Book implements Lendable {
private final String title;
private String borrower;
Book(String title) {
this.title = title;
}
public String name() {
return title;
}
public String borrower() {
return borrower;
}
public void lendTo(String person) {
borrower = person;
}
public void giveBack() {
borrower = null;
}
}
class Dvd implements Lendable {
private final String title;
private String borrower;
Dvd(String title) {
this.title = title;
}
public String name() {
return "DVD " + title;
}
public String borrower() {
return borrower;
}
public void lendTo(String person) {
borrower = person;
}
public void giveBack() {
borrower = null;
}
}Dune: lent to Noa DVD Alien: available Dune: available
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
Template MethodTemplate Method
תזכורת: מה ביקשוReminder: the task
ב-abstract class Report יש final String render() שבונה כותרת, גוף וסיום, ושתי מתודות מופשטות: title() ו-body(). SalesReport ממלאת אותן. הדפיסו:
== Sales == / Books: 12, Coffee: 30 / -- end --.
abstract class Report has final String render() building a header, a body and an ending, plus two abstract methods: title() and body(). SalesReport fills them in. Print:
== Sales == / Books: 12, Coffee: 30 / -- end --.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
Report r = new SalesReport();
System.out.println(r.render());
}
}
abstract class Report {
final String render() {
return "== " + title() + " ==\n" + body() + "\n-- end --";
}
abstract String title();
abstract String body();
}
class SalesReport extends Report {
@Override
String title() {
return "Sales";
}
@Override
String body() {
return "Books: 12, Coffee: 30";
}
}== Sales == Books: 12, Coffee: 30 -- end --
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
שני default באותו שםTwo defaults with the same name
תזכורת: מה ביקשוReminder: the task
ל-interfaces A ול-B יש default String hello() (מחזירות A ו-B). מחלקה שמממשת את שניהם חייבת להכריע. דרסו את hello כך שתחזיר A+B בעזרת A.super.hello() ו-B.super.hello().
Interfaces A and B both have default String hello() (returning A and B). A class implementing both must decide. Override hello to return A+B using A.super.hello() and B.super.hello().
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
System.out.println(new Both().hello());
}
}
interface A {
default String hello() {
return "A";
}
}
interface B {
default String hello() {
return "B";
}
}
class Both implements A, B {
@Override
public String hello() {
return A.super.hello() + "+" + B.super.hello();
}
}A+B
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.