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 8 of 8

Thread: My code so far Need Help

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

    Default My code so far Need Help

    public static void main(String[] args)
    int randNum = 0;
     
     
            int arr[] = new int [20];
     
            System.out.print("Array: ");
     
            for (int a =0; a<arr.length;a++)
     
            {
                randNum = (int)(Math.random()*(4)+1);
                arr[a] = randNum;
     
                System.out.print(" " +randNum);
            }
     
     
     
            System.out.println(" ");
            System.out.print("Random position: " + randNum);
     
        }
     
    }
    This is my Output

    Array: 3 4 4 4 4 4 3 4 2 1 3 2 3 4 4 3 3 3 2 2
    Random position: 2

    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.

    Any advice on how I can generate example this
    Final Array: 1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1
    Last edited by Jay123; July 21st, 2014 at 07:38 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: My code so far Need Help

    Please post your code correctly using code or highlight tags which are explained near the top of this link.

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

    Default Re: My code so far Need Help

    sorry about that I am new. I hope this is better

    --- Update ---

    Anyone can help me out I just don't quite understand how to get the final output which the question ask.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: My code so far Need Help

    I don't understand the assignment. Can you copy and paste all of the directions?

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

    Default Re: My code so far Need Help

    ok sure this is what they want from my assignment.

    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

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: My code so far Need Help

    Now that you've posted the entire text of the program's (assignment's) requirements - which are very clear, I'm going to show you how I think you should attack this program. I recommend you start with a skeleton like I've pasted below with the assignment's requirements pasted in as comments. I chopped them up and moved them around a bit to be where I think they belong, but you could do it differently. Once you have this outline that includes all requirements, start coding:
    /* 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.  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.
     */
    public class TremorDemo
    {
        // instance variables
        // create an integer array of size 20. 
     
     
        // default constructor
        public TremorDemo()
        {
            // generate and insert (initialize the array with) random integers
            // between 1 and 5, inclusive, into the array.
     
            // the program should print the array as output. 
     
            // the final array should be printed as output.
     
        } // end default constructor
     
        public static void main( String[] args )
        {
            new TremorDemo();
     
        } // end method main()
     
        // required and supporting methods
     
        // write the signature for each required method with the
        // description of the method given in the assignment, like this:
     
        // method insertNumbers() takes as input one integer array and stores the
        // random numbers in it.
        private void insertNumbers( int[] tremorArray )
        {
     
     
        } // end method insertNumbers()
     
     
    } // end class TremorDemo
    Then the key to getting the program right is to code each piece, testing and fixing each piece until it's right, then move to the next piece and repeat until done.

    Good luck!

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

    Default Re: My code so far Need Help

    Thank you. can I ask the part where they ask to reset 3 numbers from left to right is there any like advice on what video or code I should be using I cant figure that part out

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: My code so far Need Help

    Show the code you've written to generate random number 1 to 5, inclusive, the insertNumbers() method. The code you showed originally was not correct.

    Use your brain, not a video: To define the point at which the tremor begins, a random number is generated 0 to 19, exclusive. A for() loop could be used to 'visit' the 3 elements to the left of the index (less than) defined by the random number, the element at the index, and the 3 elements to the right of the index (greater than). That's just simple math, startIndex = index - 3, endIndex = index + 3. Care must be taken to ensure that startIndex >= 0 and endIndex <= 19. The whole thing: a couple simple equations, a couple if() statements, and a for() loop. Easy stuff.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 10:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 09:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 09:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM