Java Array and Parameter passing help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Java Array and Parameter passing help

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    Unhappy Java Array and Parameter passing help

    Hey guys, I used to programm in C++ but now my stupid uni got us doin Java..

    Problem is im having difficult passing parameters and taking them in. If this was C++, i would have did this in half an hour, but this is taking me longer. Any help will be appreciated

    For now, i need:

    /*
    a method that takes an array of integers and returns the average, as a floating-point number
    a method that takes a floating-point number as a parameter and outputs that number rounded to one decimal place
    a method that takes an array of integers as a parameter, together with a single integer, and returns the number of times the single integer appears in the array
    a method that takes an integer number as a parameter and outputs that number rounded to the nearest multiple of 5
    */

    This is what i have so far:

    public class try2
    {

    public static int[] setValues ()
    {
    float sum = 0;
    float avg = 0;
    int [] values = {3, 4, 2, 3, 3, 3, 4, 3, 3, 3, 4, 1, 1, 4};


    //This for loop and some of what follows it should be in the 'setAvg' method
    // but i cant seem to pass the information to it


    for (int count = 0; count < values.length ; count++)
    {

    sum +=values[count];
    }

    float finalavg = sum / values.length;


    System.out.println("Average is" + finalavg);



    return values;
    }

    //When i try to send the array (values) it doesnt work properly
    public static float setAvg ()
    {


    float finalavg = (sum / lengthv);


    return finalavg;



    }

    //The main method would be used for just calling other methods
    public static void main ()
    {

    int []scores;
    scores = setValues();





    }
    }

    Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    It looks like your class needs some organization. You need to make your data members global - remember that data members go out of scope when you reach the end of the function in which they are declared.

    Declare your array, sum, and avg variables outside of (before) any of the member functions. Then, these data members are available to each of those functions which follow it. Don't return an array with your "setValues()" method - defining the globally available variable and populating the array pointed to by that variable.

    You might produce the printout by using the value which is returned by your setAvg() method: System.out.println( "Average is " + try2.setAvg() );

    One more thing: you have made all of your methods "static". Remember the syntax of calling a static method.

  3. #3
    Join Date
    Dec 2005
    Posts
    4
    Thanks dude.. I'll try to do what you told me to.. Global variables..
    I think you are gonna hear from me soon.. i'll try it out and paste my new program later..
    Thanks man

  4. #4
    Join Date
    Oct 2005
    Posts
    107
    It looks like your methods aren't what is asked for. Here's the method signatures you could use as a starting point. It's not to different than C++ which was also my first language.

    /*
    a method that takes an array of integers and returns the average, as a floating-point number

    public static float getAvg(int [] values);

    a method that takes a floating-point number as a parameter and outputs that number rounded to one decimal place

    public static void printRoundOne(float value);

    a method that takes an array of integers as a parameter, together with a single integer, and returns the number of times the single integer appears in the array

    public static int count(int [] values, int value);

    a method that takes an integer number as a parameter and outputs that number rounded to the nearest multiple of 5

    public static void printRoundFive(int value);
    */

  5. #5
    Join Date
    Dec 2005
    Posts
    4
    public class crswrk
    {

    static int [] values = {3, 4, 2, 3, 3, 3, 4, 3, 3, 3, 4, 1, 1, 4};
    static float finalavg = 41 / values.length;


    /*public static int[] setValues ()
    {

    int [] values = {3, 4, 2, 3, 3, 3, 4, 3, 3, 3, 4, 1, 1, 4};
    //return values;

    }*/


    //a method that takes an array of integers and returns the average, as a floating-point number
    public static float getAvg (int [] values)
    {
    float sum=0;
    for (int count = 0; count < values.length ; count++)
    {

    sum +=values[count];
    }

    float finalavg = sum / values.length;

    return finalavg;

    }







    //a method that takes a floating-point number as a parameter and outputs that number rounded to one decimal place
    public static float printRoundOne(float roundNum)
    {
    System.out.println("F is " + finalavg);
    roundNum = finalavg*100;
    System.out.println("finalavg * 100 = " + roundNum);
    Math.round(roundNum);
    float finalRound = roundNum/100;


    return finalRound;
    }

    //a method that takes an array of integers as a parameter, together with a single integer, and returns the number of times the single integer appears in the array
    /*
    public static int count (int [] values, int val)
    {
    }
    */

    //a method that takes an integer number as a parameter and outputs that number rounded to the nearest multiple of 5
    public static void printRoundFive(int hal)
    {
    }

    public static void main ()
    {
    float g1=0;

    g1 = crswrk.getAvg(values);
    System.out.println("Avg is : " + g1);
    float g2=0;

    g2 = crswrk.printRoundOne(g1);
    System.out.println("roun " + g2);

    }

    }

    Hey guys.. This is what ive got so far.
    The problems im dealing with now have to do with the 'finalavg' variable. For these numbers, the average is 2.92857, but in the method 'printRoundOne' it recieves the 'finalavg' value of 2 (when i need it to be 2.9). Im still not sure about the rest of the methods either..

  6. #6
    Join Date
    Oct 2005
    Posts
    107
    public static void roundOne(float val){
    val *= 10;
    val = (float)Math.round(val) / (float)10.0;
    System.out.print(val);
    }

  7. #7
    Join Date
    Dec 2005
    Posts
    4
    Thanks.

    Finally got it!

Similar Threads

  1. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  2. Passing Array Values using Reflection
    By Vishal in forum .NET
    Replies: 1
    Last Post: 10-29-2002, 04:12 AM
  3. Passing a 2-D array from VB to Oracle Stored Proc
    By Og Maciel in forum Database
    Replies: 0
    Last Post: 08-08-2001, 01:19 PM
  4. Array as a parameter to stored procedure.
    By Ellen Khlystova in forum Database
    Replies: 4
    Last Post: 01-25-2001, 03:55 PM
  5. Replies: 0
    Last Post: 05-04-2000, 11:29 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center