首页 > 教育

一道java考试试题求解

更新时间2020-01-03 05:08:18

A customer visits a restaurant and asks for service via a bell.

The waiter receives the bell ring and answers. 

The Waiter class implements the Bell interface, 

and the Customer class contains a method called served(),

representing that he is served by the waiter. 

served() calls Bell's answer().

 

Please complete the codes.

For example:

input: 10

output: 

Waiter 10 receives a bell ring.

the customer is served.


只能在shart 和 end之间添加代码。


源代码

import java.util.Scanner;

 

interface Bell {

void answer();

}

 

/******start******/

 

/******end******/

 

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int no = scanner.nextInt();

 

Bell bell=new Waiter(no);

Customer customer=new Customer(bell);

 

customer.served();

 

scanner.close();

}

}


完整的参考

/******start******/
class Waiter  implements Bell {
   public Waiter(int n) {
       System.out.printf("Waiter %d receives a bell ring. ",n);
   }
   public  void answer() {
       System.out.printf("the customer is served.");
   }
}
class Customer {
   Bell bell;
   public Customer(Bell b) {
       bell=b;
   }
   public void served() {
      bell.answer();
   }
}
/******end******/

相关标签:java

上一篇:学校买了360个毽子,全校有18个班,也买了220根跳绳,学校平均每班可以得多少个毽子,多少个跳

下一篇:4m-9n=0,n/m-m/n=