注意:此页面搜索的是所有试题
题目内容
(国际开放大学Java语言程序设计)
某商场为用户设计一套积分系统,用以根据用户不同的级别,基于不同的规则计算积分:
计算规则如下:
1. VIP用户,消费1元,积分:按照消费金额*2 计算积分 向上取整
2. 普通用户 Level>=2级, 积分: 按照消费金额*1.5 计算,向上取整。
3. 普通用户Level<2级,积分: 按照消费金额 * 1计算,向上取整
用户等级的计算规则:
1. Vip用户默认级别为5:
2. 普通用户消费金额 0~20000, level为1
3. 普通用户消费金额 20000~30000, level为2
4. 普通用户消费金额 30000~40000, level为3
5. 普通用户消费金额 40000~50000, level为4
请阅读如下代码,补齐缺少的代码片段。
public 【1】ILevelCalculator {
//计算积分
public Integer calcIntegral(float amount);
}
public abstract class BaseCustomer implements ILevelCalculator {
private String name; //用户姓名
private float amount;//消费金额
private int level;//用户等级
private int integral;//积分
public BaseCustomer(String name, int level) {
this.name = name;
this.level = level;
this.integral = 0;
}
public Integer updateIntegral(Integer newAddedIntegral) {
this.integral += newAddedIntegral;
return this.integral;
}
public void updateAmount(float amount) {
this.amount += amount;
//vip用户直接跳过level设置
if (this.level>4) {
System.out.println("vip customer");
}
else if (this.amount>40000) {
this.level = 4;
}
else if (this.amount>30000) {
this.level = 3;
}
else if (this.amount > 20000) {
this.level = 2;
}
else {
this.level = 1;
}
//计算新增的积分
Integer integral = calcIntegral(amount);
this.updateIntegral(integral);
}
//getter/setter方法省略
}
public class VipCustomer 【2】 BaseCustomer {
public 【3】(String name) {
super(name, 5);
}
@Override
public Integer calcIntegral (float amount) {
return (int)Math.ceil(amount *2);
}
}
public class CommonCustomer extends BaseCustomer {
public CommonCustomer(String name, int level) {
super(name, level);
}
@Override
public Integer calcIntegral(float amount) {
return (this.getLevel()>=2) ? (int)Math.ceil(amount*1.5) : (int)Math.ceil(amount*1);
}
}
测试方法:
public static void main(String[] args) {
BaseCustomer vipCustomer = new VipCustomer("vip customer1");
BaseCustomer commonCustomer = new CommonCustomer("common1", 1);
vipCustomer.updateAmount(10000);
commonCustomer.updateAmount(25000);
System.out.println("VipCustomer积分为:" + vipCustomer.getIntegral());
System.out.println("CommonCustomer积分为:" + commonCustomer.getIntegral());
}
1. 选择合适的Java关键字()
A. class B. interface C. abstract D extends
2. 选择合适的Java关键词: ()
A. Implements
B. abstract
C. extends
D. static
3. 选择合适的方法名:()
A. VipCustomer
B. updateAmount
C. calcIntegral
D. BaseCustomer
4. VipCustomer的积分是多少()
A. 20000 B. 50000 C. 10000 D 37500
5. CommonCustomer的积分是多少()
B. 20000 B. 50000 C. 10000 D 37500
计算规则如下:
1. VIP用户,消费1元,积分:按照消费金额*2 计算积分 向上取整
2. 普通用户 Level>=2级, 积分: 按照消费金额*1.5 计算,向上取整。
3. 普通用户Level<2级,积分: 按照消费金额 * 1计算,向上取整
用户等级的计算规则:
1. Vip用户默认级别为5:
2. 普通用户消费金额 0~20000, level为1
3. 普通用户消费金额 20000~30000, level为2
4. 普通用户消费金额 30000~40000, level为3
5. 普通用户消费金额 40000~50000, level为4
请阅读如下代码,补齐缺少的代码片段。
public 【1】ILevelCalculator {
//计算积分
public Integer calcIntegral(float amount);
}
public abstract class BaseCustomer implements ILevelCalculator {
private String name; //用户姓名
private float amount;//消费金额
private int level;//用户等级
private int integral;//积分
public BaseCustomer(String name, int level) {
this.name = name;
this.level = level;
this.integral = 0;
}
public Integer updateIntegral(Integer newAddedIntegral) {
this.integral += newAddedIntegral;
return this.integral;
}
public void updateAmount(float amount) {
this.amount += amount;
//vip用户直接跳过level设置
if (this.level>4) {
System.out.println("vip customer");
}
else if (this.amount>40000) {
this.level = 4;
}
else if (this.amount>30000) {
this.level = 3;
}
else if (this.amount > 20000) {
this.level = 2;
}
else {
this.level = 1;
}
//计算新增的积分
Integer integral = calcIntegral(amount);
this.updateIntegral(integral);
}
//getter/setter方法省略
}
public class VipCustomer 【2】 BaseCustomer {
public 【3】(String name) {
super(name, 5);
}
@Override
public Integer calcIntegral (float amount) {
return (int)Math.ceil(amount *2);
}
}
public class CommonCustomer extends BaseCustomer {
public CommonCustomer(String name, int level) {
super(name, level);
}
@Override
public Integer calcIntegral(float amount) {
return (this.getLevel()>=2) ? (int)Math.ceil(amount*1.5) : (int)Math.ceil(amount*1);
}
}
测试方法:
public static void main(String[] args) {
BaseCustomer vipCustomer = new VipCustomer("vip customer1");
BaseCustomer commonCustomer = new CommonCustomer("common1", 1);
vipCustomer.updateAmount(10000);
commonCustomer.updateAmount(25000);
System.out.println("VipCustomer积分为:" + vipCustomer.getIntegral());
System.out.println("CommonCustomer积分为:" + commonCustomer.getIntegral());
}
1. 选择合适的Java关键字()
A. class B. interface C. abstract D extends
2. 选择合适的Java关键词: ()
A. Implements
B. abstract
C. extends
D. static
3. 选择合适的方法名:()
A. VipCustomer
B. updateAmount
C. calcIntegral
D. BaseCustomer
4. VipCustomer的积分是多少()
A. 20000 B. 50000 C. 10000 D 37500
5. CommonCustomer的积分是多少()
B. 20000 B. 50000 C. 10000 D 37500
参考答案