作者:yan | 来源:互联网 | 2023-05-19 04:56
(股票类Stock)遵照9.2节中Circle类的例子,设计一个名为Stock的类。这个类包括:一个名为symbol的字符串数据域表示股票代码。一个名为name的
(股票类 Stock)遵照 9.2 节中 Circle 类的例子,设计一个名为 Stock 的类。这个类包括:
public class Practice_9_2 {
public static void main(String[] args) {
Stock stock = new Stock("ORCL", "Oracle Corporation");
stock.previousClosingPrice = 34.5;
stock.currentPrice = 34.35;
System.out.println("Change Percent: " + stock.getChangePercent() + "%");
}
}
class Stock {
String symbol;
String name;
double previousClosingPrice;
double currentPrice;
public Stock(String symbol, String name) {
this.name = name;
this.symbol = symbol;
}
public double getChangePercent() {
return (Math.max(previousClosingPrice, currentPrice) - Math.min(previousClosingPrice, currentPrice)) / previousClosingPrice * 100;
}
}
输出结果为:
Change Percent: 0.434782608695648%