Question: For this assignment, you will write two programs: a RandomList class that stores random numbers, and a RandomListClient class that uses the RandomList class. You'll

For this assignment, you will write two programs: a RandomList class that stores random numbers, and a RandomListClient class that uses the RandomList class. You'll get a chance to work out details about static versus non-static, creating instance variables, and more.

Part I: RandomList

RandomList should be a class that represents a list with random numbers in it. This class should have a method named getSum that allows someone to get the sum of the numbers in the list. Your RandomList class should live in the file RandomList.java.

More details on your code design:

  1. Your constructor for the RandomList should take a single argument that indicates how many random numbers to put in the list. The constructor should put this many random numbers in the list. You'll store the list as an instance variable in your class. You're welcome to use the Java List interface and any of its implementations in your code (see "All Known Implementing Classes" on that web page). For example, if your instance variable was named numberList, you might initialize this instance variable with the following line in your constructor:

    numberList = new ArrayList<>(); 

  2. We want the random numbers to be less than or equal to zero, but not smaller than MINIMUM_SIZE. This MINIMUM_SIZE is some constant value (which you can set to be whatever you like for your program), and it should be the same for all instances of RandomList. (Hint: What does this tell you about whether it should be static or non-static?)

    For example, if MINIMUM_SIZE is equal to -3, then each number I have in my list should have an equal chance of being -3, -2, -1, and 0. You'll likely find the Javadoc for the Random class very helpful.

  3. To allow the user to interact with RandomList, make it take a single command line argument: the number of numbers that should be stored in the list. For example, I might invoke the program as follows:

    java RandomList 5

    The program should make a list that stores that many random numbers. Arguments from the command line are stored in the String[ ] parameter of the main method. Note that the command line arguments are stored as Strings, not ints. To convert the String into an int, you may find the parseInt method in Integer useful. Hint: parseInt is a static method; how to call this method?

    Then, the program should display the numbers in the list to the user, and following that, display the sum of all of the numbers in the list. Remember, your class should have a method getSum to get the sum of the numbers in the list; this method does not have any parameters.

  4. Finally, add a method that allows you to get the number of instances of RandomList that have been constructed. If I wanted to call this method, I would do so as follows:

    RandomList.getInstanceCount()

    This isn't functionality that is built into Java; you'll need to count instances as you construct them. (Hint: Is the number of instances that have been constructed something specific to a particular instance, or generally about the class as a whole? What does that tell you about whether the variable keeping track of the number of instances should be static or non-static?)

Example output: RandomList

Here's an example of my program's output. I typed:

java RandomList 6

at the command line, and it printed:

 Numbers in list: [-1, -2, -1, -4, 0, -4] Sum of the numbers: -12 How many RandomList instances did this program create? It created 1. 

You might format things slightly differently, but you should make sure your program prints all three things.

Code Tips:

  1. Remember that you have to construct variables before you use them. If you run into null pointer exceptions or exceptions about not using a variable before it has been initialized, one possible cause is that you have declared a variable (e.g., List numberList;) but you haven't assigned it a value (e.g. numberList = new ArrayList<>();).
  2. The documentation is very helpful for figuring out what methods are available for a particular type of object.

Part II: RandomListClient

So far, you've only used code you wrote in a single class (e.g., writing a main method for a class) or used classes that other people have made. In this portion of the assignment, you'll create and use instances of RandomList in a new class, which you'll call RandomListClient. A client is someone who uses a class. This is just an opportunity to practice being a client, so it won't be very involved.

Write a main method in RandomListClient (which is stored in RandomListClient.java) that constructs RandomList instances of length 5 (i.e., each RandomList stores 5 integers) until one of these instances has a sum less than -10. When a RandomList has a sum less than -10, the program should print the numbers in that list, the sum of the numbers, and the total number of instances of RandomList that have been constructed. (Hint: For all of these parts, you should try to use methods from RandomList to accomplish your taskthere shouldn't be a lot of new code you have to write!)

Example output: RandomListClient

Here's an example of the output of my program:

 The first list with a sum of less than -10 had the following numbers: [-3, -2, -2, -3, -3] This list has a sum of -13. A total of 2 lists were created. 

Note 1: Make sure that the value of MINIMUM_SIZE is such that RandomListClient is not guaranteed to run in an infinite loop.

Note 2: It would have been entirely possible to write the code for RandomListClient in RandomList and have it be executed if the user didn't provide a command line argument. (If you're not sure how this would work, try to figure it out!) However, you're putting them in two files for this assignment to see that there's no magic in writing and using your own classes. As long as the two files are in the same directory (folder), they will each know about the other and no import statements are needed.

ASSIGNMENT REQUIREMENTS

This is a partial list of the things that we'll be looking for when evaluating your work:

  • Program compiles
  • Running RandomList from the command line prints out a list of the given size and its sum
  • Running RandomListClient from the command line works as specified above
  • A constructor, getSum, and getInstanceCount are all defined in RandomList
  • Comments are used to indicate the logic of the program
  • Program has a short description of the class at the top of each class in comments
  • Program uses correct indentation
  • Program follows typical Java style rules
  • Program uses an appropriate object oriented design, including good choices about what is static and what is non-static, what is private, etc.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!