Results 1 to 4 of 4

Thread: 1-D Array Help

  1. #1
    MrChessguy2 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default 1-D Array Help

    Hey, so I'm new to java and the AP Comp Sci class I'm taking is kicking my ass. I've been working on this problem for a couple hours but can't seem to figure out what to do:

    Problem: Create a list of 100 randomly generated numbers (between 1 and 100). Print the list out in order, print the list out in reverse order, print out how many of each number was generated.

    I'm at a loss for the second and third part, but it's the first part I've been unsuccessfully going at for a while. Here's the code as it is right now:


    //import arraylist
    import java.util.ArrayList;
    //import random generator
    import java.util.Random;
    //name class
    public class NumberArray {
    //string args
    public static void main (String[] args) {
    numbers = new ArrayList();
    }
    public void add(Number Num) {
    numbers.add(Num);
    }
    //method this up
    public double NumListGenerator(int count) {
    //make a random generator
    Random generator = new Random();
    //import the array
    int[] Num;
    //Set the array to 100
    Num = new int [100];
    //now let's do a for loop to come up with our 100 numbers
    for (count = 0; count < 100; count++) {
    //Obtain a new number
    Num[count] = generator.nextInt(100) + 1;
    }
    return Num[count];

    }
    }


    I've been reading a couple java books but they're not really helping, so any help is much appreciated!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: 1-D Array Help

    The term, "in order" seems ambiguous to me. Does it mean, in sorted order, or just in the order the array was populated?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    MrChessguy2 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: 1-D Array Help

    I think it's the order the numbers are generated?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: 1-D Array Help

    First you should place your code between code tags [code][/code]. I have done it here for you. And your comments are not
    helping as they are stating the obvious. You only need to comment when something is not obvious or could be misinterpreted.

    Java Code:
    import java.util.ArrayList;
    import java.util.Random;
    public class NumberArray {
    public static void main (String[] args) {
       numbers = new ArrayList();
    }
    public void add(Number Num) {
         numbers.add(Num);
    }
    public double NumListGenerator(int count) {
        Random generator = new Random();
        int[] Num;
        Num = new int [100];
        for (count = 0; count < 100; count++) {
            Num[count] = generator.nextInt(100) + 1;
         }
         return Num[count];
        }
    }
    First, you should stay away from methods in this case. They aren't necessary (unless the prof required them). Just
    do it all in main if you are permitted.

    The first thing you should do is simply create a list of 100 numbers between 1 and 100. Then print them out. Once you get that
    part done, then move on to the next part.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 10:39 PM
  2. Replies: 15
    Last Post: 05-14-2012, 01:42 PM
  3. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  4. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  5. Replies: 2
    Last Post: 09-06-2010, 02:03 AM

Tags for this Thread

Posting Permissions

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