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: 3666819 • 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 percent- age 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

Java program code: Create two java classes, Stock.java and StockDemo.java

//Stock.java
public class Stock
{
  
//A string data field named symbol for the stocks symbol.
private String symbol;
//A string data field named name for the stocks name
private String name;
//A double data field named previousClosingPrice
//that stores the stock price for the previous day

private double previousClosingPrice;
//A double data field named currentPrice that stores the stock price for the current time.
private double currentPrice;
  
//A constructor that creates a stock with a specified symbol and name.
public Stock(String symbol,String name)
{
this.symbol=symbol;
this.name=name;
}
  
//set and get methods
public void setCurrentPrice(double currentPrice)
{
this.currentPrice=currentPrice;
}
  
public void setPreviousPrice(double previousClosingPrice)
{
this.previousClosingPrice=previousClosingPrice;
}

public double getCurrentPrice()
{
return currentPrice;
}
  
public double getPreviousPrice()
{
return previousClosingPrice;
}
  
//A method named get ChangePercent() that returns the
//percent- age changed from previous ClosingPrice to
//currentPrice.

public double getChangePercent()
{
return ((getPreviousPrice()-getCurrentPrice())/getCurrentPrice())*100;
}
  
//toString method for display output
public String toString()
{
  
return "Stock Symbol : "+symbol+" Stock Name : "+name;
}
}

//StockDemo.java

//Header file
import java.text.DecimalFormat;
//StockDemo.java for run the program
public class StockDemo
{
   //main function
public static void main(String[] args)
{
  
//Create a formatter to set percentate for two decimal places
DecimalFormat formatter=new DecimalFormat("0.00");
  
//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.

Stock stock=new Stock("ORCL", "Oracle Corporation");
stock.setPreviousPrice(34.50);
stock.setCurrentPrice(34.35);
System.out.println(stock.toString());
System.out.print("Change Percentage of Previous and current Stock : ");
//call getChangePercent method and display rate of change
System.out.print(formatter.format(stock.getChangePercent()));
  
}
}

Output of the program code:

Stock Symbol : ORCL
Stock Name : Oracle Corporation
Change Percentage of Previous and current Stock : 0.44