设计一个名为Intsequlence的接口表示整数序列,该接 口包含boolean hasNext)和iint next)两个方法。定义一个名 为RandomlntSequence的类实现IntSequence接口,其中包 含一个private整型变量n。在hasNext 方法中随机生成一个 两位整数,存储到变量n中,然后返回true。在next0方法中 返回n的值。以及测试该接口的代码
时间: 2024-02-19 17:03:51 浏览: 19
好的,以下是完整的代码实现和测试类。
Intsequence接口:
```
public interface Intsequence {
boolean hasNext();
int next();
}
```
RandomlntSequence实现类:
```
import java.util.Random;
public class RandomlntSequence implements Intsequence {
private int n;
public boolean hasNext() {
Random random = new Random();
n = random.nextInt(90) + 10;
return true;
}
public int next() {
return n;
}
}
```
测试类:
```
public class TestIntsequence {
public static void main(String[] args) {
Intsequence sequence = new RandomlntSequence();
for (int i = 0; i < 10; i++) {
System.out.println(sequence.next());
}
}
}
```
在测试类中,我们创建了一个Intsequence类型的变量sequence,并将其实例化为RandomlntSequence类型。然后使用for循环调用sequence的next方法打印出10个随机生成的两位整数。
希望这个例子可以帮助你更好地理解如何使用接口和实现类。