# Thread: I have a problem!

1. Member
Join Date
Feb 2010
Posts
7
Rep Power
0

## 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);
}
}

2. what problem are you facing in this code?

3. Member
Join Date
Feb 2010
Posts
7
Rep Power
0
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.

4. no replies for your post other than me...can u please explain in detail what you want exactly?

5. Member
Join Date
Feb 2010
Posts
7
Rep Power
0
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
)
)

6. Member
Join Date
Aug 2009
Posts
76
Rep Power
0
Did you try anything yet?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•