Untitled
import java.util.*; public class StockPeerDemo { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter the stock symbol: "); String stockSymbol = keyboard.nextLine(); System.out.print("Enter the stock's name: "); String stockName = keyboard.nextLine(); System.out.print("Enter the closing price of the stock the prior day: $"); double closingPrice = keyboard.nextDouble(); System.out.print("Enter the current price of the stock: $"); double currentPrice = keyboard.nextDouble(); StockPeer stock = new StockPeer(stockSymbol, stockName, closingPrice, currentPrice); double priceChangePercentage = stock.getChangePercent(); System.out.println("The stock symbol is: " + stockSymbol); System.out.println("The stock name is: " + stockName + "\n"); System.out.printf("The closing price of the stock the prior day is: $%.2f\n",closingPrice); System.out.printf("The current price of the stock is: $%.2f\n",currentPrice); System.out.printf("The price-change percentage is: %.2f%%",priceChangePercentage); } } public class StockPeer { private String stockSymbol; private String stockName; private double priorClosingPrice; private double currentPrice; public void symbol(String sym) { stockSymbol = sym; } public void name(String nam) { stockName = nam; } public void priorClosingPrice(double pcp) { priorClosingPrice = pcp; } public void currentPrice(double currentP) { currentPrice = currentP; } public StockPeer (String sym, String nam, double pcp, double currentP) { stockSymbol = sym; stockName = nam; priorClosingPrice = pcp; currentPrice = currentP; } public double getChangePercent() { return (priorClosingPrice-currentPrice)/priorClosingPrice * 100; } }
Leave a Comment