Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: I need help in this coding

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help in this coding

    Hello I am new in java. been working on it for 2months. I need to solve this by next week try going to youtube to learn but could not figure it out. Any help from the java community would be great.



    Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

    A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn’t 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

    Then, the final array should be printed as output. There is no user input for this program.

    Your program must include, at least, the following methods:

    • insertNumbers, which will take as input one integer array and store the random numbers in it.
    • createTremor, which will generate the random number as the location and return it.

    A sample run of the program is shown below:

    Sample output #1:
    Array: 1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
    Random position: 5
    Final Array: 1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: I need help in this coding

    What have you tried so far? Post your effort and any problems your having
    and people will help you.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I need help in this coding

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    To help you, we need to see what you've been doing for 2 months with any specific questions, problems, or errors you'd like help with.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help in this coding

    Hello sorry this is all I understand about array.

    for this part of the question Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

    my code is

    int array[]= new int[20];

    array [1]= 3;
    array [2]= 4;
    array [6]= 1;
    // and so on....

    how do I make a array which is able to support it randomly rather than typing them out from 1 - 5

    help..

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I need help in this coding

    You probably meant that you've been studying Java for 2 months, not that you've been working on this problem for two months. Sorry if I interpreted your meaning incorrectly.

    As for how to do what you've asked, use a loop to iterate the array:

    for ( int i = 0 ; i < myArray.length ; i++ )
    {
        // assign values to the array using statements like:
        myArray[i] = 
    }
    And read up on generating random numbers by reviewing the Random class and the Math.random() method. Choose whichever one of those meets your needs the best, though I recommend using Random.

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help in this coding

    This is my code so far am I in the right track I am really confused about this pls help me

    int insertNumbers[]= new int[20];

    insertNumbers[0]= 0;
    insertNumbers[1]= 1;
    insertNumbers[2]= 2;
    insertNumbers[3]= 3;
    insertNumbers[4]= 4;
    insertNumbers[5]= 5;
    insertNumbers[6]= 6;
    insertNumbers[7]= 7;
    insertNumbers[8]= 8;
    insertNumbers[9]= 9;
    insertNumbers[10]= 10;
    insertNumbers[11]= 11;
    insertNumbers[12]= 12;
    insertNumbers[13]= 13;
    insertNumbers[14]= 14;
    insertNumbers[15]= 15;
    insertNumbers[16]= 16;
    insertNumbers[17]= 17;
    insertNumbers[18]= 18;
    insertNumbers[19]= 19;


    int createTremor = 1 + (int)(Math.random()*5);

    System.out.println(" " +insertNumbers[createTremor]);



    }

    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help in this coding

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help in this coding

    public static void main(String[] args) {
            // TODO code application logic here
            int randNum = 0;
            int arr[] = new int [20];
            System.out.print("Array: ");
            insertNumbers(arr);
            System.out.println(" ");
            int pos = createTremor(num);
        }  
        public static void insertNumbers(int arr[])
            {
                for(int i=0; i<arr.length;i++)
                {
                    int randNum=(int)(Math.random()*5)+1;
                    arr[i]=randNum;
                    System.out.print(" " + randNum);
                }
            }
        public static int createTremor(int num[])
        {
            int pos=(int)(Math.random()*20);
            return pos;
        }

    I also need help with this question. This is my code so far. But it cannot display the desired output.

  9. #9
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: I need help in this coding

    How was trying to figure just about the same thing, only it is for MD5 hash and it is a series of random numbers or letters. Key/values that goes together with an object. When the object is valued with a value, it is then hashed to an email. As you already know this is an interface that deals with a table of values. Just working it on NetBeans 8.0 to see if I can integrate the object into other framework structures. (or someone's web page)

    Anybody know where I can find more information on this fascinating topic? Thanks a lot! Same type of problem as above, only more complex.

  10. #10
    Junior Member
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help in this coding

     
    public static void main(String[] args) {
            // TODO code application logic here
            int randNum = 0;
            int arr[] = new int [20];
            System.out.print("Array: ");
            insertNumbers(arr);
            System.out.println(" ");
            int pos = createTremor(arr);
            System.out.println("Random position: "+ pos);
     
            if(randNum == 0)
            {
                arr[1] = 0;
                arr[2] = 0;
                arr[3] = 0;
            }
            else if (randNum == 1)
            {
                arr[0]=0;
                arr[2] = 0;
                arr[3] = 0;
                arr[4] = 0;
            }
            else if (randNum == 2)
            {
                arr[0]=0;
                arr[1] = 0;
                arr[3] = 0;
                arr[4] = 0;
                arr[5] = 0;
            }
            else if (randNum == 19)
            {
                arr[16] = 0;
                arr[17] = 0;
                arr[18] = 0;
            }
            else if (randNum == 18)
            {
                arr[15] = 0;
                arr[16] = 0;
                arr[17] = 0;
                arr[19] = 0;
            }
            else if (randNum == 17)
            {
                arr[18]=0;
                arr[19] = 0;
                arr[16] = 0;
                arr[14] = 0;
                arr[15] = 0;
            }
            else
            {
                arr[randNum+1] = 0;
                arr[randNum+2] = 0;
                arr[randNum+3] = 0;
                arr[randNum-1] = 0;
                arr[randNum-2] = 0;
                arr[randNum-3] = 0;
            }
        }  
        public static void insertNumbers(int arr[])
            {
                for(int i=0; i<arr.length;i++)
                {
                    int randNum=(int)(Math.random()*5)+1;
                    arr[i]=randNum;
                    System.out.print(" " + randNum);
                }
            }
        public static int createTremor(int num[])
        {
            int pos=(int)(Math.random()*20);
            return pos;
        }

    I edited my code again. But the output is wrong.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help in this coding

    the output is wrong.
    Please post the output and explain what is wrong with it and show what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help in this coding

    I GOT IT!
    public class PRSPAssignQn3 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int arr[]=new int[20];
     
            System.out.print("Array: "); 
            insertNumbers(arr);
     
            for(int x:arr) 
            System.out.print(" " + x);
     
            System.out.println(" "); 
            createTremor(arr); 
     
            System.out.print("Final Array: "); 
            for (int y: arr)
            System.out.print(" " + y);
        }
     
        public static void insertNumbers(int arr[]){
     
            for(int i=0;i<arr.length;i++){ 
                int num=(int)(Math.random()*(5))+1;
                arr[i]=num;
            }
        }
     
        public static void createTremor(int num[]){
            int RandomPos = (int)(Math.random()*(20))+0; 
            System.out.println("Random Position: " + RandomPos);
     
     
            for (int i=0;i<num.length;i++){
                if (RandomPos == 19){
                    num[RandomPos-3]=0;
                    num[RandomPos-2]=0;
                    num[RandomPos-1]=0;
                }
                else if (RandomPos == 18){
                    num[RandomPos-3]=0;
                    num[RandomPos-2]=0;
                    num[RandomPos-1]=0;
                    num[RandomPos+1]=0;
                }
                else if (RandomPos == 17){
                    num[RandomPos-3]=0;
                    num[RandomPos-2]=0;
                    num[RandomPos-1]=0;
                    num[RandomPos+1]=0;
                    num[RandomPos+2]=0;
                }
                else if (RandomPos == 2){
                    num[RandomPos+3]=0;
                    num[RandomPos+2]=0;
                    num[RandomPos+1]=0;
                    num[RandomPos-1]=0;
                    num[RandomPos-2]=0;
                }
                else if (RandomPos == 1){
                    num[RandomPos+3]=0;
                    num[RandomPos+2]=0;
                    num[RandomPos+1]=0;
                    num[RandomPos-1]=0;
                }
                else if (RandomPos == 0){
                    num[RandomPos+3]=0;
                    num[RandomPos+2]=0;
                    num[RandomPos+1]=0;
                }
                else {
                    num[RandomPos+3]=0;
                    num[RandomPos+2]=0;
                    num[RandomPos+1]=0;
                    num[RandomPos-1]=0;
                    num[RandomPos-2]=0;
                    num[RandomPos-3]=0;
                }
            }        
        }
    }
    And the output is correct.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help in this coding

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with coding
    By wil09045304 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 26th, 2013, 08:31 AM
  2. Need help with some coding
    By Firearrow5235 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 13th, 2013, 06:57 AM
  3. [SOLVED] I need help with my coding
    By knockturnal22 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 16th, 2012, 04:19 PM
  4. Need Help With Coding
    By noles227 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2012, 07:35 PM
  5. Replies: 2
    Last Post: February 19th, 2012, 07:36 AM