package stock; /** * * @author AHMED */ public class Stock { public static void
ID: 3939120 • Letter: P
Question
package stock;
/**
*
* @author AHMED
*/
public class Stock {
public static void main(String[] args)
{
Stock MSFT = new Stock("MSFT","Microsoft",34.5,34.35);
System.out.println("Price change in Percentage: "
+ MSFT.changePercent()
+ MSFT.getCurrentPrice()
+ MSFT.previousClosingPrice() );
}
}
class Stock {
String symbol;
String name;
double previousClosingPrice;
double currentPrice;
public Stock(String symbol, String name, double previouseClosingPrice, double currentPrice)
{
stock.symbol = symbol;
stock.name = name;
stock.previousClosingPrice = previousClosingPrice;
stock.currentPrice = currentPrice;
}
public double changePercent()
{
return ((currentPrice - previousClosingPrice) / previousClosingPrice) *100;
}
public double getPreviousClosingPrice()
{
return stock.previousClosingPrice ;
}
public double getCurrentPrice()
{
return stock.currentPrice;
}
public void setCurrentPrice(double newCurrentPrice)
{
stock.currentPrice = newCurrentPrice;
}
public void setPreviousClosingPrice(double price)
{
stock.previousClosingPrice = price;
}
}
Explanation / Answer
public class StockMain {
public static void main(String[] args)
{
Stock MSFT = new Stock("MSFT","Microsoft",34.5,34.35);
System.out.println("Price change in Percentage: "
+ MSFT.changePercent()
+ MSFT.getCurrentPrice()
+ MSFT.getPreviousClosingPrice() );
}
}
class Stock {
Stock stock;
String symbol;
String name;
double previousClosingPrice;
double currentPrice;
public Stock(String symbol, String name, double previouseClosingPrice, double currentPrice)
{
stock.symbol = symbol;
stock.name = name;
stock.previousClosingPrice = previousClosingPrice;
stock.currentPrice = currentPrice;
}
public double changePercent()
{
return ((currentPrice - previousClosingPrice) / previousClosingPrice) *100;
}
public double getPreviousClosingPrice()
{
return stock.previousClosingPrice ;
}
public double getCurrentPrice()
{
return stock.currentPrice;
}
public void setCurrentPrice(double newCurrentPrice)
{
stock.currentPrice = newCurrentPrice;
}
public void setPreviousClosingPrice(double price)
{
stock.previousClosingPrice = price;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.