Objectives:

  • Develop a Java application that uses responsibility separation in its class organization
  • Create an attractive console TUI that allows a user to interact with an application
  • Apply the use of decisions and loop to control the flow of an application

Overview:

So far in the course we've been working hard to structure our programs such that there is a separation of concerns - where different aspects of the software functionality are contained in separate classes. The goal here is to make the responsibilities contained within a class strongly related to one another (this is known as high cohesion). The responsibilities within a class should not be mixed with other responsibilities. In other words, a class should handle one responsibility and one responsibility only.

In this design pattern, the goal is to be sure that we do not couple the nonuser interface objects directly to the user interface objects. The idea is that the model will manage the domain layer of objects by managing the business logic and data, while the view provides the presentation layer of objects that displays the necessary data and allows the user to interact with the program.

By decoupling the model and view functionality, we can allow for:

  • The support of cohesive model definitions that focus on model processes instead of user interfaces
  • The separate development of model and user interface layers
  • New views to be easily connected to an existing model layer
  • Multiple views of the same model object(s)

For now, this separation of concerns means that our model classes should not be doing any kind of output formatting or display directly to the console. Instead the model classes should return data so that the class that called this method (like the view) can then take care of the formatting and displaying (or it could even use the data in a completely different way if it wanted).

While our previous assignments have had some mixing of model and view functionality, this assignment should have complete modelview separation so that the model class does not format the output or display anything to the console directly - this will all be done completely within the view.

Application Description:

This application will give you a chance to better explore the use of random numbers within a Java application that will generate a series of random integers between 099, inclusive and then provide a variety of metrics on those values. The idea is that the user will specify the number of random values to be generated and they will be presented with a sorted list of the actual values generated, a histogram of those values, and some numerical statistics (maximum, minimum, average, median, etc).

To make the application a little more user friendly, the interface will restrict the user from choosing to generate more than 2,500 values. It will also give the user control over the number of values to be printed per line - again with the restriction that no more than 10 values can be listed per line.

As one additional feature, the interface will allow users to enter a number less than 1 for the number of numbers, but in that case, the object will generate 100 numbers.

NOTE: We will continue with the assumption of a 'semiintelligent user' (the user will enter an integer when prompted to do so, but that we aren't guaranteed what number will be entered).

As always, please be sure to use the appropriate loop type in your code.

What to do:

Be sure to read through all items before you begin. This week we will list all of the requirements and give you a chance to practice what you've learned about iterative development in this course as you develop your application. You project will include:

Three packages named:

edu.westga.cs6311.statistics.controller
edu.westga.cs6311.statistics.model
edu.westga.cs6311.statistics.view

Add to the model package a class named RandomGenerator, which will have

  • Instance variables:
    • An ArrayList of integer values
    • A private static final variable of type int to hold the default number of numbers generated (we make this private so that code outside the class can't access it, static because we really only need one of these no matter how many objects are created, and final because we don't want this value to ever change)
  • Public Methods:
    • A 0parameter constructor used to initialize the instance variables
    • createNumbers - Uses the Math.random() method to generate a specific number of random values between 0 and 99, inclusive. This method should be overloaded such that there is a 0parameter version which generates and stores the default number of numbers (100) and a 1parameter version that accepts the number of random numbers to be generated. Note: When overloading this method be sure not to include duplicate code.
    • getMinimum
    • getMaximum
    • getAverage - Hint: Be sure this method returns a floating point value
    • getSortedNumbers - This method will return the sorted ArrayList of values
    • getMedian NOTE: Please see the Math Is Fun site linked to in this week's Reading list on Moodle for more details and an example calculation
    • getBucketCount - This method will constructor and return a 10element simple array containing the count of the numbers in each 10 value bucket. For example, element 0 will contain the number of values generated between 0 and 9, element 1 will contain the number of values generated between 10 and 19. NONGRADED CHALLENGE: See if you can do this without using a decision structure (without an if or a switch)
  • Private (Helper) Methods:
    • sort - This must be defined as a private helper method used to sort the values generated. To make life easier, please consider using the Collection.sort method. This week's Reading list on Moodle has a link to the Java API for this method.
    • Other private helper methods as necessary (you may or may not wish to have other methods here)

Add to the view package a class named StatisticsTUI, which will have

  • Instance variables:
    • An object from the RandomGenerator class
    • An object from the Scanner class
  • Public Methods:
    • A 1parameter constructor that accepts an object of type RandomGenerator. This constructor will be used to initialize the instance variables. Be sure to error check the parameter and if it is null, simply have the constructor return without doing anything
    • A run - this method serves as the 'director' of the user interface by calling on a variety of private helper methods to do their work, as appropriate. This application will greet the user, ask them for a number of random numbers to be generated and the number of numbers to be displayed per line. It will then generate the numbers, display them on the screen, display a histogram, and finally list the statistics about these random values.
  • Private (Helper) Methods:
    • displayResults - This method be used to display the results of the calculated statistics about the RandomGenerator object on the console. Be sure that the average is displayed showing exactly two decimal places (for example, 1.23 or 3.00)
    • displaySortedValues - This method should accept the number of numbers per line to be displayed and display the values on the console.
    • displayHistogram - This method will display one line of output for each of the 10 buckets (09, 1019, etc) listing (a) the range of values and (b) one asterisk (*) for each value generated in that range. See the sample output for an example.
    • Other private helper methods as necessary (you may or may not wish to have other methods here)

Add to the controller package a class named StatisticsDriver, which will have

  • Public Methods:
    • main This method will create an instance of the RandomGenerator class and pass that object to the StatisticsTUI class, then use the resulting object to call run.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.