Results 1 to 5 of 5
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default 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;
    		}			
    	}	
    }
    I then have to write a test program that creates a Stock object with a stock symbol Java, the name Sun Microsystems Inc, the previous closing price set to 4.5 and a new current price of4.35. Then I have to display the percentage change.

    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 problem is that I get these two error messages:

    "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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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;
    }
    Then you would have one getter for each item you want to view.

    Java Code:
    public String getName(){
      return name;
    }
    and similar methods for each additional instance variable. Then use the methods instead of accessing variables directly.

  4. #4
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Re: object not visible

    Quote Originally Posted by Junky View Post
    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.
    Ok. Thank you for pointing me in the right direction.

    Improving the world one idiot at a time!
    Hey, I resemble that remark! :)

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

Similar Threads

  1. JFrame, isn't visible sometimes..
    By AndroidAppNewbie in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 03:13 PM
  2. ERRORS: set visible
    By Johanis in forum New To Java
    Replies: 1
    Last Post: 11-07-2010, 12:39 PM
  3. Replies: 3
    Last Post: 10-20-2010, 07:24 AM
  4. GUI is visible but content is not.
    By seemant.bisht in forum AWT / Swing
    Replies: 3
    Last Post: 10-07-2009, 07:28 PM
  5. Tab or Table not visible
    By madhuvanthi2312 in forum SWT / JFace
    Replies: 1
    Last Post: 04-25-2009, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •