Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Following the example of the Circle class, design a class named Stock that conta

ID: 3666991 • Letter: F

Question

Following the example of the Circle class, design a class named Stock that contains:

- A string data field named symbol for the stocks symbol.

- A string data field named name for the stocks name.

- A double data 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 a specified symbol and name.

- A method named get ChangePercent() that returns the percentage changed from previous ClosingPrice to currentPrice.

Write a test program that creates a Stock object with the stock symbol ORCL, the name Oracle Corporation,and the previous closing price of 34.5. Set a new current price to 34.35 and display the price-change percentage.

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class Stock {

   String symbol;
   String name;
   double previousClosingPrice;
   double currentPrice;

   /**
   * method to return percentage changed from previous ClosingPrice to
   * currentPrice
   *
   * @return
   */
   public double ChangePercent() {

       double percentage = (currentPrice - previousClosingPrice)
               / previousClosingPrice;

       return percentage;

   }

   /**
   * @return the symbol
   */
   public String getSymbol() {
       return symbol;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @return the previousClosingPrice
   */
   public double getPreviousClosingPrice() {
       return previousClosingPrice;
   }

   /**
   * @return the currentPrice
   */
   public double getCurrentPrice() {
       return currentPrice;
   }

   /**
   * @param symbol
   * the symbol to set
   */
   public void setSymbol(String symbol) {
       this.symbol = symbol;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @param previousClosingPrice
   * the previousClosingPrice to set
   */
   public void setPreviousClosingPrice(double previousClosingPrice) {
       this.previousClosingPrice = previousClosingPrice;
   }

   /**
   * @param currentPrice
   * the currentPrice to set
   */
   public void setCurrentPrice(double currentPrice) {
       this.currentPrice = currentPrice;
   }

   /**
   * @param symbol
   * @param name
   */
   public Stock(String symbol, String name) {

       this.symbol = symbol;
       this.name = name;
   }

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       Stock stock = new Stock("ORCL", "Oracle Corporation");
       stock.setPreviousClosingPrice(34.5);
       stock.setCurrentPrice(34.5);

       System.out.println("The price-change percentage is :"
               + stock.ChangePercent());

   }

}

OUTPUT:

The price-change percentage is :0.0