9.2(Stock类)遵照9.2节中Circle类的例子,设计一个名为Stoke的类。这个类包括:
画出该类的UML图并实现这个类。编写一个测试程序,创建一个Stoke对象,他的股票代码是ORCL,股票的名字为Oracle Corporation,前一日收盘价是34.5。设置新的当前值为34.35,然后显示市值变化的百分比。
9.2(Stoke class)Follow the example of the circle class in Section 9.2 to design a class called stoke. This class includes:
Draw the UML diagram of the class and implement the class. Write a test program, create a stoke object, his stock code is orcl, the name of the stock is Oracle Corporation, the previous day’s closing price was 34.5. Set the new current value to 34.35, and then display the percentage of market value change.
参考代码:
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