
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 floatingpoint number
a method that takes a floatingpoint 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

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.

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

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 floatingpoint number
public static float getAvg(int [] values);
a method that takes a floatingpoint 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);
*/

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

public static void roundOne(float val){
val *= 10;
val = (float)Math.round(val) / (float)10.0;
System.out.print(val);
}

Thanks.
Finally got it!
Similar Threads

Replies: 6
Last Post: 11012005, 09:05 AM

Replies: 1
Last Post: 10292002, 04:12 AM

By Og Maciel in forum Database
Replies: 0
Last Post: 08082001, 01:19 PM

By Ellen Khlystova in forum Database
Replies: 4
Last Post: 01252001, 03:55 PM

By john Harkin in forum Database
Replies: 0
Last Post: 05042000, 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

Forum Rules

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