Design a class named Stock that contains: A string data field named symbol for t
ID: 3627372 • Letter: D
Question
Design a class named Stock that contains:A string data field named symbol for the stock's symbol.
A string data field named name for the stock's name.
A double field named previousClosingPrice that stores the stock price for the previous day.
A double data field named currentPrice that stores the stock price for the current time.
A constructor that creates a stock with specified symbol and name.
A method named getChangePercent() that returns the percentage changed from previousClosingPrice to currentPrice.
Draw the UML diagram for the class. Implement the class. Write test program that creates a Stock object with the stock
symbol JAVA, the name Sun Microsystems Inc, and the previous closing price of 4.5. Set a new currentprice to 4.35
Explanation / Answer
please rate - thanks
public class Stock {
private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;
public Stock( String s,String n)
{
symbol=s;
name=n;
}
public void setPrevious(double p ) {
previousClosingPrice=p;
}
public void setCurrent(double p ) {
currentPrice=p;
}
public double getChangePercent() {
return (currentPrice-previousClosingPrice)/ previousClosingPrice;
}
}
----------------------------------------
import java.util.*;
public class StockTest{
public static void main(String[] args)
{Scanner input = new Scanner(System.in);
Stock s=new Stock("JAVA","Sun Microsystems Inc");
s.setPrevious(4.5);
s.setCurrent(4.35);
System.out.println("% change is "+s.getChangePercent());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.