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 13 of 13

Thread: Arrays

  1. #1
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Arrays

    Modify the stats class created in the previous assignment to allow it to store and analyze data sets of different sizes. it should now have an array that holds 100 double values instead of 12. An instance variable numElt will keep track of how many elements in the atrray are being used by a particular instance of the class. All of the Stats class functions should be revised to work with numElt elements instead of 12.

    Can someone please guide me as to what exactly i need to do. im kinda confused.

    public class Stats 
    {   
        // records array field created to store twelve values
        private double [] records = new double [12];
     
        // method to set values in array
        public void setValue(int rainfall, double actValue )
        {
            records[rainfall] = actValue;
        }
     
        public void processRainfallStats()
        {
            System.out.printf("The total rainfall for the year is %.2f\n\n", getTotal());
     
            System.out.printf("The average rainfall for the year is %.2f\n\n", getAverage());
     
            System.out.printf("The least rainfall for the year is %.2f\n\n", getSmallest());
     
            System.out.printf("The most rainfall for the year is %.2f\n\n", getLargest());
        }       
     
        // method to calculate total
         public double getTotal()
        {   
            double total = 0;
     
            for (double tot : records)
            {total += tot;}
            return total;
        }
     
         // method to calculate average rainfall in records array
        public double getAverage()
        {
            double average;
     
            int total = 0;
     
            for (double avg: records)
            {total += avg;}
            average = total / records.length;
     
            return average;
        }
     
        // method to find largest index value in records array
        public double getLargest()
        {
            double largest = records[0]; // assuming that records[0] is the largest
     
            for (double Lag : records)
            {
                if (Lag > largest)
                {largest = Lag;}
            }
            return largest;
        }
     
        // method to find smallest index value in records array
        public double getSmallest()
        {
            double smallest = records[0]; // assuming that records[0] is the smallest
     
            for (double small: records)
            { if (small < smallest)
                {smallest = small;}
            }
            return smallest;
        }
     
     
    }


    package leedanielassignment4;
     
    import java.util.Scanner;
     
    public class StatsTest 
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
     
            // new object of Stats class created
            Stats myStats = new Stats();
     
            int rainfall;
            double actValue;
     
            // new array of months to be store months
            String [] months = new String[] {"January","February","March","April","May","June","July","August",
                                                "September","October","November","December"};
     
            for (int i = 0; i < months.length; i++)
            {
                // print method used to set value to month 
                System.out.printf("Please eneter the amount of rainfall for %s:\n ", months[i]);
     
                rainfall = i;
                actValue = input.nextDouble();
     
                    if(actValue < 0)
                        {actValue = 0;}
                // object calling method in class to initiate values
                myStats.setValue(rainfall, actValue);
            }    
         // object calls method of stats class to print info   
         myStats.processRainfallStats();
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays

    What part of the assignment are you having problems with?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Arrays

    im wondering if i hav to make numelt as a copy of the array and then assign it to all records functions? or do something completely different from what im suggesting i do

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays

    instance variable numElt will keep track of how many elements in the array are being used
    numElt would be an int that has the count of the number of elements in the array that are being used. If the array had 100 elements and there were 5 elements in the array, numElt would be 5
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Arrays

    sorry but i still doont get it

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays

    If the array has 100 slots and the code is only going to use some of them, how do you keep track of how many slots of the array have been used?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Arrays

    using a loop

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays

    That is not very efficient to use a loop every time you want to know how many slots are used. Why not have the count of the number of used slots in a variable like: numElt?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Arrays

    i be tryna figure this thing out trying all kinds of things but still aint gettin it.. but thanks

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays

    Try writing a simple program that defines an array and an int variable: numElt. Add some elements to the array but don't fill it and set the value of numElt to the number of elements that were added to the array
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    LeeDD (March 6th, 2013)

  12. #11
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Arrays

    aight thanks norm!! i was kinda close to this idea

    --- Update ---

    keeping track means just putting a limit on how much elements the array will use?

    --- Update ---

    is that what yu are saying is what im tryna do, will both of em giv me the result i want?

    double [] Stats = new double [12]; // this is an array of 12 elements
    int numElt = 10;
     
    double [] Stats = {6, 2, 3, 56, 5, 67,  7, 23, 0, 0, 0, 0}; // this is a array of 12 elements, only 8 elements have values higher than 0
    int numElt = 8;

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays

    Now consider where the next empty slot is in the array you just defined. You can use the numElt variable to access it.
    Use it to add an element to that array and increment numElt after the element is added.
    theArrray[numELt] = newData;  // put data into the array at empty slot
    numELt++; // move to next empty slot and counts number in array
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Arrays

    Thanks.. very helpful.. solved. i was reading wrong!!

Similar Threads

  1. Need help with arrays
    By Burnett98 in forum What's Wrong With My Code?
    Replies: 27
    Last Post: January 23rd, 2013, 05:23 PM
  2. Arrays
    By oonagh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 7th, 2012, 10:03 AM
  3. 2D arrays
    By Fluffy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2012, 05:11 PM
  4. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  5. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM