The problem - Write a program that has the following:
Data:
A string field named symbol for the Stock’s symbol.
A a string named sName for the Stock’s name.
A double field named previousClosingPrice that stores the stock’s prices form the previous day.
A double data field named currentPrice that stores the stock price for the current time.
Behaviors: (i.e. methods)
A default Constructor.
An overloaded Constructor that takes a stock with a specific symbol, name, and last closing price. ** Note values for current stock price will have to be initialized to zero.
A toString(); function that returns the current stock’s name, Symbol, and current price.
A method named getChangePercent(); that returns the percentage changed between previousClosingPrice and currentPrice.
A method named setCurrentPrice(double); that allows the setting of the current stock price.
Write a test program that creates a Stock object with the symbol ORCL, the name Oracle Corporation, and the previous closing price of 34.5.
Also, have the test program call the toString(); method to see it’s definition.
Lastly, set the new stock price to 34.35 and then display the price change percentage.
My problem is the math aspect to calculate the percentage. My result is "Infinity", which is obviously wrong. What am I missing here?
package stockPrice;
public class Stock {
//Set Data fields
public String symbol;
public String sName;
public double previousClosingPrice;
public double currentPrice = 0;
//Default Constructor
public Stock(){
}
//Constructor with previous closing price parameter
public Stock(String newSymbol, String newSName, double newPreviousClosingPrice) {
this.symbol = newSymbol;
this.sName = newSName;
this.currentPrice = newPreviousClosingPrice;
}
//Return Percent Change
double getChangePercent(){
return (((previousClosingPrice - this.currentPrice)/previousClosingPrice)*100);
}
//Set a new Current Price
double setCurrentPrice(double newCurrentPrice){
this.currentPrice = newCurrentPrice;
return this.currentPrice;
}
@Override
public String toString() {
// TODO Auto-generated method stub
String total = "The symbol is: " + this.symbol + " and the name is: " + this.sName + " and the previous closing price is: " + this.currentPrice;
return total;
}
}
Driver:
package stockPrice;
public class Driver{
public static void main(String[] args) {
//Driver Program for all testing and Instantiation
Stock stock1 = new Stock ("ORCL", "Oracle Corporation", 34.5);
System.out.println(stock1.toString());
stock1.setCurrentPrice(34.35);
System.out.println(stock1.toString() + ". The percent change is: " + stock1.getChangePercent());
}
}