1. Description of the problem
(1) Define the interface Compute.
The interface methods are: int sum( ), to find the sum of two integers; int max( ), to find the larger number among the two integers.
(2) Define the ComputeClass class.
The ComputeClass class is required to implement the Compute interface; two private properties a and b with int type provide a parameterized construction method ComputeClass (int a, int b). The sum() method returns the sum of attributes a and b, and max returns the larger of attributes a and b.
import java.util.Scanner;
public class ComputeTester {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
ComputeClass cc = new ComputeClass(a,b);
System.out.println("两数之和为:" + cc.sum());
System.out.println("较大值为:" + cc.max());
}
}
/* 定义接口Compute */
interface Compute{
int sum();
int max();
}
/* 定义Compute接口的实现类ComputeClass */
class ComputeClass implements Compute {
private int a;
private int b;
public ComputeClass(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int sum() {
return a + b;
}
@Override
public int max() {
return a > b ? a : b;
}
}