Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How do I return the current number of trades(number) from an array?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I return the current number of trades(number) from an array?

    This is what I have so far.
    I am just stuck on my method int numTrades(), I have to return the current number of trades from the array.

        //Fields
       double lastPChange; 
       double dailyPrices[];
       int numberOfDaysWithData;
     
     public void addTrade(double price)
     {
       dailyPrices[numberOfDaysWithData] = price;
       numberOfDaysWithData = numberOfDaysWithData + 1;
       this.numberOfDaysWithData++;
       lastPChange = (numberOfDaysWithData - 2);
     }
     
     public void numTrades()
     {
      dailyPrices[numberOfDaysWithData]
      return;
     }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,967
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: How do I return the current number of trades(number) from an array?

    You need to change the definition of numTrades() to show that it returns a value:
    public THEDATATYPE numTrades() {

    Change THEDATATYPE to be the type of data that the method returns, like String or double or int;

    Change the return statement to include the variable whose value is to be returned.

    See the tutorial: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I return the current number of trades(number) from an array?

    Thank you !
    This is what my code look likes now.
    What is the difference between using this.numberOfDaysWithData and numberOfDaysWithData?

    I know "this" refers to the object of the method.
    It would make since if i used this.numberOfDaysWithData to return the numTrades, right?

     public void addTrade(double price)
     {
       dailyPrices[numberOfDaysWithData] = price;
       numberOfDaysWithData = numberOfDaysWithData + 1;
       this.numberOfDaysWithData++;
       lastPChange = (numberOfDaysWithData - 2);
     }
     
     public int numTrades()
     {
      return this.numberOfDaysWithData;
     }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,967
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: How do I return the current number of trades(number) from an array?

    What is the difference between using this.numberOfDaysWithData and numberOfDaysWithData
    If there is NOT a local variable or arg named: numberOfDaysWithData then the this is redundant/not needed. If there is a local variable with the same name, then the this would use the class level variable vs the local one.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I return the current number of trades(number) from an array?

    Thank you !
    One more question, I have no idea what my professor is talking about with our last method.

    void checkBreakerAndPrint()
    basically it checks the breaker condition for the last added trade and if the breaker trips with a 10% or more change up or down

    What is my professor talking about?

Similar Threads

  1. Replies: 10
    Last Post: November 8th, 2012, 05:29 AM
  2. Replies: 2
    Last Post: November 7th, 2012, 09:45 PM
  3. Search for number in array and return index
    By Kevinius in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2011, 12:00 AM
  4. Replies: 1
    Last Post: December 4th, 2010, 04:26 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM

Tags for this Thread