题目内容:
在国际机场,我们会发现有多个货币兑换的窗口,这是由于各国货币的单位价值是不一样的。下面列出了某日国际货币的汇率表(相对于100人民币的各国货币值)。
货币 |
CNY |
HKD |
USD |
EUR |
汇率 |
100 |
118 |
15 |
13 |
例如,100元人民币可以兑换118香港币。请利用继承机制与Comparable接口实现不同数目货币对象价值的排序。下面给出了程序的基本框架,请给出相关的函数实现。(假设所有的输入数值和比较都是用整数)
import java.util.Arrays;
import java.util.Scanner;
class Currency {
private String name; //货币名称
private int originalValue; //原始值
private int value; //转换为人民币后的值
public static String[] CURRENCY_NAME = { "CNY", "HKD", "USD", "EUR" };
public static int[] CURRENCY_RATIO = { 100, 118, 15, 13 };
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOriginalValue() {
return originalValue;
}
public void setOriginalValue(int originalValue) {
this.originalValue = originalValue;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
class HKD extends Currency implements Comparable {
// 实现你的构造函数与Comparable中的接口
}
class USD extends Currency implements Comparable {
// 实现你的构造函数与Comparable中的接口
}
class EUR extends Currency implements Comparable {
// 实现你的构造函数与Comparable中的接口
}
public class Main {
public static void main(String[] args) {
Currency[] cs = new Currency[3];
//初始化
Scanner sc = new Scanner(System.in);
//利用hasNextXXX()判断是否还有下一输入项
int a = 0;
int b = 0;
int c = 0;
if (sc.hasNext()) {
a = sc.nextInt();
cs[0] = new HKD(a);
}
if (sc.hasNext()) {
b = sc.nextInt();
cs[1] = new USD(b);
}
if (sc.hasNext()) {
c = sc.nextInt();
cs[2] = new EUR(c);
}
//初始化结束
//请补充排序
//请补充输出结果
}
}
输入格式:
输入3个数字。
输出格式:
金额价值从小到大
输入样例:
100
100
100
输出样例:
HKD100
USD100
EUR100
时间限制:500ms内存限制:32000kb
答案:
import java.util.Arrays;
import java.util.Scanner;
class Currency {
protected String name;
private int originalValue;
protected int value;
public static String[] CURRENCY_NAME = { "CNY", "HKD", "USD", "EUR" };
public static int[] CURRENCY_RATIO = { 100, 118, 15, 13 };
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOriginalValue() {
return originalValue;
}
public void setOriginalValue(int originalValue) {
this.originalValue = originalValue;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Currency(String name, int originalValue, int value) {
super();
this.name = name;
this.originalValue = originalValue;
this.value = value;
}
public Currency() {
super();
}
}
class HKD extends Currency implements Comparable {
public HKD(int a) {
this.setName(CURRENCY_NAME[1]);
this.setOriginalValue(a);
int i= CURRENCY_RATIO[0];
int j= CURRENCY_RATIO[1];
float k=(float)i/j;
a =(int)(a*k);
this.setValue(a);
}
public int compareTo(Currency another) {
int i = 0;
if (i == 0) {
return value- another.value;
} else {
return i;
}
}
}
class USD extends Currency implements Comparable {
public USD(int b) {
this.setName(CURRENCY_NAME[2]);
this.setOriginalValue(b);
int i= CURRENCY_RATIO[0];
int j= CURRENCY_RATIO[2];
float k=(float)i/j;
b =(int)(b*k);
this.setValue(b);
}
public int compareTo(Currency another) {
int i = 0;
if (i == 0) {
return value- another.value;
} else {
return i;
}
}
}
class EUR extends Currency implements Comparable {
public EUR(int c) {
this.setName(CURRENCY_NAME[3]);
this.setOriginalValue(c);
int i= CURRENCY_RATIO[0];
int j= CURRENCY_RATIO[3];
float k=(float)i/j;
c =(int)(c*k);
this.setValue(c);
}
public int compareTo(Currency another) {
int i = 0;
if (i == 0) {
return value- another.value;
} else {
return i;
}
}
}
public class Main {
public static void main(String[] args) {
Currency[] cs = new Currency[3];
Scanner sc = new Scanner(System.in);
int a = 0;
int b = 0;
int c = 0;
if (sc.hasNext()) {
a = sc.nextInt();
cs[0] = new HKD(a);
}
if (sc.hasNext()) {
b = sc.nextInt();
cs[1] = new USD(b);
}
if (sc.hasNext()) {
c = sc.nextInt();
cs[2] = new EUR(c);
}
Arrays.sort(cs);
for (Currency currency : cs) {
System.out.println(currency.getName()+currency.getOriginalValue());
}
}
}
题目来自于网络 如有侵权请私信本人删除,谢谢