# I have a problem!

• 02-25-2010, 03:39 PM
rasmus07
I have a problem!
// c) calculate the sum of all values in the array
// d) calculate the average of all values in the array
// e) search for the lucky number 7. If the array contains the
// number 7, the text “Lucky array” should be printed
// to the output.

Here is my code!!!

import java.util.*;
public class Program3
{
public static void main (String []arg)
{
Random generator=new Random();
// this create a empty array named arr
int [] arr = new int[10];

int sum;
double medel;
//a) this fills the array with value generated by the Random
for(int i=0;i <arr.length;i++)
arr[i]=generator.nextInt(100);
// b) this prints the array
for(int i=0;i <arr.length;i++)
System.out.println(arr[i]);
// c) calculate the sum of all values in the array
sum = 0;
for (int i=0; i<arr.length; i++)
{
sum += arr[i];
}

System.out.println("\nDen Totala summan är: " + sum);
// d) calculate the average of all values in the array
medel = 0;
for (int i=0; i<arr.length; i++)
{
medel += arr[i];
medel /= sum;
}
System.out.println("\nMedelvärdet är: " + medel/sum);

// e) search for the lucky number 7. If the array contains the
// number 7, the text “Lucky array” should be printed
// to the output.
for(int i = 0; i < arr.length; i++)
{
if(arr[i] == 7)
{
System.out.println("Lucky array ");
}
}

}

4. We can do it better! We should implement the functionalities in separate methods and class and call the methods in main or what ever you needed. The method fillArrayay() is ready, se my code below. Your work is to do the same for all other functionalities b, c, d, e

Here is the new code!!
Where do you put the different steps of the 2 different classes?

import java.util.*;
public class MyArrayMethods{
public static void fillArray( int[] array, int maxValue){
Random gen=new Random();
for(int i=0;i <array.length;i++)
array[i]=gen.nextInt(maxValue);
}
}

Prove that your methods work by calling these methods in the Programe2 class.

import java.util.*;
public class Program2 {
public static void main (String []arg)
{
// this creates a empty array named arr
int [] arr = new int[10];
// call the method fillArray
MyArrayMethods.fillArray( arr, 50);
}
}
• 02-25-2010, 04:01 PM
RamyaSivakanth
what problem are you facing in this code?
• 02-25-2010, 04:14 PM
rasmus07
I have no problem yet, but I do not understand where I need to add the new code and how it should look like as I have written.
• 02-25-2010, 04:32 PM
RamyaSivakanth
no replies for your post other than me...can u please explain in detail what you want exactly?
• 02-25-2010, 05:17 PM
rasmus07
What I want is to add b, c, d, and e in my code

// b) this prints the array
for(int i=0;i <arr.length;i++)
System.out.println(arr[i]);
/ / C) calculate the sum of all values in the array
/ / D) calculate the average of all values in the array
/ / E) search for the lucky number 7. If the array contains the
/ / Number 7, the text "Lucky array should be printed
/ / To the output.

How to put it and where!

import java.util .*;
public class MyArrayMethods (
public static void fill array (int [] array, int maxValue) (
Random gen = new Random ();
for (int i = 0; i <array.length; i + +)
array [i] = gen.nextInt (maxValue);
)
)

Prove that your methods work by calling these methods in the Programe2 class.

import java.util .*;
public class Program2 (
public static void main (String [] arg)
(
/ / This creates a empty array named arr
int [] arr = new int [10];
/ / Call the method fill Array
MyArrayMethods.fillArray (arr, 50);
/ / Here try your methods
)
)
• 02-26-2010, 12:28 AM
senorbum
Did you try anything yet?