Results 1 to 5 of 5
Thread: object not visible
- 09-05-2011, 01:20 AM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
object not visible
I have to create a class named stock that contains the following:
- A string data field named symbol for a stock's stock symbol.
- A string data field named name for a stock's 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 specified symbol and name.
A method named getChangePercent() that returns the percentage changed from previousClosingPrice to currentPrice.
This is what I have come up with:
Java Code:package Stock; public class Stock { //declare variables private String symbol; private String name; private double previousClosingPrice; private double currentPrice; //Construct a two-arg constructor Stock(String newSymbol, String newName){ symbol = newSymbol; name = newName; } //invoke a getter method to return the stock price percentage change double getChangePercent(double previousClosingPrice, double currentPrice){ //perform logic functions to find percentage of stock change if (currentPrice >= previousClosingPrice){ double percentChanged = currentPrice / previousClosingPrice; return percentChanged; } else { double percentChanged = previousClosingPrice / currentPrice; return percentChanged; } } }
This is what I have come up with:
Java Code:package Stock; public class TestStock { //invoke main method public static void main(String [] args){ Stock stock1 = new Stock ("Java", "Sun Microsystems Inc"); System.out.println("The stock symbol of " + stock1.name + " is " + stock1.symbol + " and the stock has had a " + stock1.getChangePercent(4.5, 4.35)+ " % change since yesterday's " + "closing price"); } }
"The field stock.name is not visible" and
The field stock.symbol is not visible."
I am assuming that they are not visible due to the private access control modifer. However, that didn't fix the problem.
I would appreciate it if someone could tell me what I am doing wrong.
Please excuse the poor formatting. It does not appear that way in eclipse.
- 09-05-2011, 01:31 AM #2
Re: object not visible
Yes you cannot access the instance variables outside the class if they are declared private. What people usually do is provide getter methods that simply return the value of the variable.
Also, I doubt the getChangePercentage method is supposed to accept two values passed in as parameters. Otherwise what is the point of storing them in instance variables? You might want to get clarification but you probably want setter methods for them. Then the getChangePercentage method will perform the calculation of the values stored in the instance variables and not use parameters.
- 09-05-2011, 01:33 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 12
Re: object not visible
You can't access private instance variables from another class directly as you have. You must provide getters to view them, and setters to change them. You should also do either of the following, create a constructor which takes previous price and current price as well, or create setters which you use to set the values. Then the getpercentChange should look like this
Java Code:public double getPercentChange(){ if(condition){ return previous/current; } return current/previous; }
Java Code:public String getName(){ return name; }
- 09-05-2011, 01:35 AM #4
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
- 09-05-2011, 01:43 AM #5
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Similar Threads
-
JFrame, isn't visible sometimes..
By AndroidAppNewbie in forum New To JavaReplies: 2Last Post: 03-06-2011, 02:13 PM -
ERRORS: set visible
By Johanis in forum New To JavaReplies: 1Last Post: 11-07-2010, 11:39 AM -
how to make the circle visible clearly, which is drawn by using Graphics object
By prasad.vara in forum AWT / SwingReplies: 3Last Post: 10-20-2010, 06:24 AM -
GUI is visible but content is not.
By seemant.bisht in forum AWT / SwingReplies: 3Last Post: 10-07-2009, 06:28 PM -
Tab or Table not visible
By madhuvanthi2312 in forum SWT / JFaceReplies: 1Last Post: 04-25-2009, 09:00 AM
Bookmarks