- 一个名为symbol的字符串数据域表示股票代码。
- 一个名为name的字符串数据域表示股票名字。
- 一个名为previousClosingPrice的double类型区域,它贮存的是前一日的股票值。
- 一个名为currentPrice的double区域类型,它贮存的是当前的股票值。
- 一个名为getChangePercent()的方法,放回从previousClosingPrice到currentPrice变化的百分比。
画出该类的UML图并实现这个类。编写一个测试程序,创建一个Stock对象,它的股票代码是ORCL,股票名为Oracle Corporation,前一日收盘价是34.5.设置新的当前值为34.55,然后显示市值变化的百分比。
package chapter09;
public class Code_02 {
public static void main(String[] args) {
Stock stock = new Stock("ORCL","Oracle Corporation");
System.out.println("The change percent is: " + stock.getChangePercent(34.5,34.35));
}
}
class Stock{
private String symbol;
private String name;
private double preciousClosingPrice;
private double currentPrice;
Stock(String newSymbol,String newName){
this.name = newName;
this.symbol = newSymbol;
}
public String getChangePercent(double preciousClosingPrice,double currentPrice){
return (currentPrice - preciousClosingPrice) / (preciousClosingPrice * 100) + "%";
}
}
输出
The change percent is: -4.347826086956481E-5% Process finished with exit code 0