In: Computer Science
in java
Write a class named “Stock” to model a stock. The properties and
methods of the class are shown in figure below. The method “
percentage ” computes the percentage of the change of the current
price vs the previous closing price. Write a program to test the
“Stock” class. In the program, create a Stock object with the stock
symbol SUND, name Sun Microsystem V, previous closing price of 100.
Set a new current price randomly and display the price
percentage.
Stock
private String symbol
private String name
private double previousClosingPrice
private double currentPrice
public Stock()
public Stock(String symbol, String name)
public String getSymbol()
public String getName()
public double getPreviousClosingPrice()
public double getCurrentPrice()
public void setSymbol(String symbol)
public void setName(String name)
public void setPreviousClosingPrice(double price)
public void setCurrentPrice(double price)
public double percentage()
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Stock.java
public class Stock {
// Declaring variables
private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;
// Parameterized constructor
public Stock(String symbol, String name) {
this.symbol = symbol;
this.name = name;
}
// Setters and getters
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPreviousClosingPrice() {
return previousClosingPrice;
}
public void setPreviousClosingPrice(double
previousClosingPrice) {
this.previousClosingPrice =
previousClosingPrice;
}
public double getCurrentPrice() {
return currentPrice;
}
// This method will find the change in
percentage
double percentage() {
return ((currentPrice -
previousClosingPrice) / previousClosingPrice) * 100;
}
public void setCurrentPrice(double currentPrice)
{
this.currentPrice =
currentPrice;
}
}
=======================================
==========================================
// Test.java
public class Test {
public static void main(String[] args)
{
double currentPrice=0.0;
Stock s=new Stock("SUND","Sun
Microsystem V");
s.setPreviousClosingPrice(100);
currentPrice=(Math.random() *
(101)) + 100;
s.setCurrentPrice(currentPrice);
System.out.printf("Price Percentage
: %.2f%%\n",s.percentage());
}
}
==============================================
==========================================
output:
=====================Could you plz rate me
well.Thank You