Results 1 to 6 of 6
  1. #1
    rasmus07 is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Angry 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);
    // Here try your methods
    }
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    830
    Rep Power
    6

    Default

    what problem are you facing in this code?
    Ramya:cool:

  3. #3
    rasmus07 is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    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. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    830
    Rep Power
    6

    Default

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

  5. #5
    rasmus07 is offline Member
    Join Date
    Feb 2010
    Posts
    7
    Rep Power
    0

    Default

    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. #6
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 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
  •