水池注水排水问题
题目描述:
1.定义一个水管接口IPipe。有一个方法int flowWater()。
该方法的功能是返回单位时间内流入或流出的水量。流入为正数,流出为负数。
2.定义一个水管类Pipe,实现接口IPipe。
3.定义一个圆形水管类CirclePipe继承水管类。
4.定义一个长方形水管类RectanglePipe,也继承水管类。
5.定义一个水池类Pool:
(1)有成员变量volume(水池的容量)、remainder(剩余水量);
(2)有通过管道接口注水放水方法void pourWater(IPipe p);
因某一个水管p注水或放水,引起水池剩余水量的变化;
(3)有一个方法检查水池是否为空或溢出。
boolean isOverFlowOrNull()
6.定义一个水池测试类PoolTest。
(1)从键盘输入2个整数,一个是池子的容量,一个是剩余水量。
(2)从键盘输入一个整数n,表示连接水池的水管个数。
提示:IPipe pipe[]= new Pipe[n];
或 Pipe pipe[] = new Pipe[n];
(3)接着输入n行,每行表示一个水管的参数
形状:circle/rectangle;
尺寸:r/(a,b);
方向:in/out
(4)输出水池从初始状态到水池为空或溢出时,经过的时间。
注:题中所有的物理量都没有单位。
在Begin–end之间,完善程序。其他代码不需修改。
7.样例1
输入输入
2000 1000
3
circle 8 in
circle 5 out
rectangle 2 3 out
样例输出
10
8.样例2
输入输入
2000 1500
4
circle 3 in
rectangle 3 4 out
circle 5 out
rectangle 4 5 in
样例输出
38
import java.util.Scanner;
public class PoolTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/* --- Begin ----*/
/* --- End ----*/
}
}
/**
* 水管接口
*/
interface IPipe {
/**
* 返回单位时间内流入或流出的水量。流入为正数,流出为负数。
*/
int flowWater();
}
/**
* 水管类
*/
class Pipe implements IPipe {
public int flowWater() {
return 0;
}
}
/**
* 圆形水管
*/
class CirclePipe extends Pipe {
int r; //半径
String direct;//方向:in 入水管, out 出水管
public CirclePipe(int r, String direct) {
this.r = r;
this.direct = direct;
}
public int flowWater() {
//PI=3
if (direct.equals("in"))
return 3 * r * r;
return -3 * r * r;
}
}
/**
* 长方形水管
*/
class RectanglePipe extends Pipe {
int a, b;//水管截面的长、宽
String direct;//方向:in 入水管, out 出水管
public RectanglePipe(int a, int b, String direct) {
this.a = a;
this.b = b;
this.direct = direct;
}
public int flowWater() {
if (direct.equals("in"))
return a * b;
return -a * b;
}
}
/**
* 水池类
*/
class Pool {
int volume;//水池的容量
int remainder;//剩余水量
public Pool(int volume, int remainder) {
this.volume = volume;
this.remainder = remainder;
}
/**
* 注水或放水
* 返回
*/
void pourWater(IPipe p) {
remainder = remainder + p.flowWater();
}
/**
* 检查水池是否为空或溢出
*/
boolean isOverFlowOrNull() {
//如果水池为空或者溢出,返回true,否则返回false
if (remainder <= 0 || remainder >= volume)
return true;
return false;
}
}
解题思路:
在题目已给类的前提下,写如何实现代码还是比较简单的
step1:需要输入的内容——
volume(水池的容量)、remainder(剩余水量)
创建水池对象pool
水管数n //到这都直接通过扫描仪Scanner直接输入
水管类型 水管尺寸(半径 / 长、宽)方向
对于不同类型对应的水管尺寸不同,那么可以借助String 类中equals方法比较水管类型 进行下一步赋值对应的水管参数和方向
水管数——觉决定了输入(水管类型 水管尺寸(半径 / 长、宽)方向)的次数,使用for循环遍历
到这里完成的是输入和创建水池pool及水管对象;
step2:题中所有的物理量都没有单位。
所以只需要计算何时水池中剩余水量为空或者是满即可
也就是需要经多少次的水量变化(进水与排水)
而如何判断在Pool类中有boolean isOverFlowOrNull()方法
由于次数未知()使用while进行遍历
以水池状态是否为空(满)为条件
当为空(满)时结束循环;否则进行循环
循环中:
以for循环遍历每根管子的pourwater方法
然后time++;
输出time即可
import java.util.Scanner;
public class PoolTest {
public static void main(String[] args) {
/* --- Begin ----*/
Scanner input = new Scanner(System.in);
int volume = input.nextInt();
int remainder = input.nextInt();
Pool pool = new Pool(volume, remainder);
int n = input.nextInt();
Pipe pipe[] = new Pipe[n];
String X;
for (int i = 0; i < n; i++) {
X = input.next();
if (X.equals("circle")) {
int r = input.nextInt();
String diretion = input.next();
pipe[i] = new CirclePipe(r, diretion);
} else {
int a = input.nextInt();
int b = input.nextInt();
String diretion = input.next();
pipe[i] = new RectanglePipe(a, b, diretion);
}
}
int time = 0;
while (!(pool.isOverFlowOrNull())) {
for (int j = 0; j < n; j++) {
pool.pourWater(pipe[j]);
}
time++;
}
System.out.println(time);
/* --- End ----*/
}
}
/**
* 水管接口
*/
interface IPipe {
/**
* 返回单位时间内流入或流出的水量。流入为正数,流出为负数。
*/
int flowWater();
}
/**
* 水管类
*/
class Pipe implements IPipe {
public int flowWater() {
return 0;
}
}
/**
* 圆形水管
*/
class CirclePipe extends Pipe {
int r; //半径
String direct;//方向:in 入水管, out 出水管
public CirclePipe(int r, String direct) {
this.r = r;
this.direct = direct;
}
public int flowWater() {
//PI=3
if (direct.equals("in"))
return 3 * r * r;
return -3 * r * r;
}
}
/**
* 长方形水管
*/
class RectanglePipe extends Pipe {
int a, b;//水管截面的长、宽
String direct;//方向:in 入水管, out 出水管
public RectanglePipe(int a, int b, String direct) {
this.a = a;
this.b = b;
this.direct = direct;
}
public int flowWater() {
if (direct.equals("in"))
return a * b;
return -a * b;
}
}
/**
* 水池类
*/
class Pool {
int volume;//水池的容量
int remainder;//剩余水量
public Pool(int volume, int remainder) {
this.volume = volume;
this.remainder = remainder;
}
/**
* 注水或放水
* 返回
*/
void pourWater(IPipe p) {
remainder = remainder + p.flowWater();
}
/**
* 检查水池是否为空或溢出
*/
boolean isOverFlowOrNull() {
//如果水池为空或者溢出,返回true,否则返回false
if (remainder <= 0 || remainder >= volume)
return true;
return false;
}
}
第一次写文章,如果写的不好还请见谅