This discussion is archived
6 Replies Latest reply: Oct 26, 2012 12:00 PM by 962826 RSS

Objects, Overloading, and Stock Prices

962826 Newbie
Currently Being Moderated
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());
          
     }

}
  • 1. Re: Objects, Overloading, and Stock Prices
    EJP Guru
    Currently Being Moderated
    previousClosingPrice is never set.
  • 2. Re: Objects, Overloading, and Stock Prices
    gimbal2 Guru
    Currently Being Moderated
    I don't see the previousClosingPrice actually being set anywhere. (EDIT: ninja'd!)

    Debugging calculations is not too hard - simply put some System.out statements to print out the individual variables used in the calculations; one of them is bound to be different from what you're expecting. Of course you could also use a debugger, but that tends to be a little out reach for people new to Java.
  • 3. Re: Objects, Overloading, and Stock Prices
    962826 Newbie
    Currently Being Moderated
    So is setting the price as simple as this? (Keeping in mind this is my first JAVA class I am taking)...
    package stockPrice;
    
    public class Stock {
    //Set Data fields
         public String symbol;
         public String sName;
         public double previousClosingPrice = 34.5;
         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;
              
         }
    
    }
  • 4. Re: Objects, Overloading, and Stock Prices
    rukbat Guru Moderator
    Currently Being Moderated
    +... thread moved from the Java Programming forum+
    to the New-To-Java forum.
    +(I suggest you stay in that N-T-J forum for the foreseeable future; you're still too much of a novice at this.)+
  • 5. Re: Objects, Overloading, and Stock Prices
    939520 Explorer
    Currently Being Moderated
    On a side note, lets say you made a good investment and your currentPrice went up relative to
    your previousClosingPrice. Shouldn't the percent change be a positive number (up)? Your equation below produces a negative number.
         double getChangePercent(){
              return (((previousClosingPrice - this.currentPrice)/previousClosingPrice)*100);
         }
  • 6. Re: Objects, Overloading, and Stock Prices
    962826 Newbie
    Currently Being Moderated
    You are right. Which is why I need help haha. I am not an expert quite yet.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points