1 Replies - 170 Views - Last Post: 02 December 2017 - 12:00 AM Rate Topic: -----

#1 Falcun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-December 17

Counting sort to random number generator.

Posted 01 December 2017 - 11:21 PM

Made a random number generator and now I wanted to add counting sort to it but I'm very new to Java and honestly have no idea even where to start with that. The code I have below is just the generator. Any help will be appreciated. Thank you.

package RandomNumberGen;

//Allow for keyboard input
import java.util.*;

public class RandomNumberGenerator{
   //User Input
   static Random rand = new Random();
   static Scanner userInput = new Scanner(System.in);  
   //User input max/min values
   static int maximum;
   static int minimum;
   //Number of randomly generated numbers
   static int numberOfRandNum = 20;
   //Sum of Random Numbers
   static int sum;
   //Random total
   static double average;
   //The string to represent the integers input
   static String input;
   //The user responses that are strings
   static String restart;
   static String display;
   static String sameNums;
   //Booleans
   static boolean displayRandNums;
   static boolean sameNumbers = false;

   public static void main(String[] args)
   throws InterruptedException {
      //Start the process
      startCode();
      userInput.close();
   }
   
   public static void startCode()
   throws InterruptedException {
   /*
      This method is the main body of the code. It takes
      the user input and assigns it to the variables and interprets it.
   */
      
      //User prompts
      System.out.print("Enter a maximum number: ");
      maximum = UserInputInt(maximum);
      System.out.print("Enter a minimum number: ");
      minimum = UserInputInt(minimum);
      //Don't allow the minimum to be higher than the maximum
      if(maximum <= minimum)
      {
         System.out.println("Error: maximum must be greater than minimum");
         //Restart the process
         startCode();
      }
      //User prompts cont.
      System.out.print("How many random numbers do you want to average? ");
      numberOfRandNum = UserInputInt(numberOfRandNum);
      //Only allow non-negative array values
      if(numberOfRandNum <= 0)
      {
         System.out.println("Please enter a number greater than zero");
         startCode();
      }
      //User prompt for displaying every number generated
      System.out.print("Would you like to see the random numbers?(Y/N) ");
      display = userInput.next();
      display = display.toLowerCase();
      //Interprets response
      if(display.equals("y") || display.equals("yes"))
      {
         //If the user wants to see the numbers, set the bool to true
         displayRandNums = true;
      }
      else if(display.equals("n") || display.equals("no"))
      {
         //If the user doesn't want to see the numbers, set the bool to false
         displayRandNums = false;
      }
      else
      {
         //If the user inputs an invalid input, tell them that, and restart
         System.out.println("Input not understood.");
         startCode();
      }
      //Collect the random numbers
      getRandom();
      endPrompts();
      
   }
   
   public static void getRandom()
   {
      /*
         This method is designed to generate a random number
         between the maximum and the minimum values input
         by the user
       */
      sum = 0;
      //Create an array to store the values of the random numbers
      int[] randomNumbers = new int[numberOfRandNum];
      //Generate the random numbers
      for(int x=0; x<numberOfRandNum; x++)
      {
         randomNumbers[x] = Math.abs(rand.nextInt() % (maximum - minimum +1)) + minimum;
         sum = sum + randomNumbers[x];
         //Average them
         average = ((double) sum / (double) randomNumbers.length);
         //Round answer to 3 decimal places
         average = Math.round(average*1000)/1000.0;
         
         //Allows the user to see the numbers that are randomly generated
         if(displayRandNums)
            System.out.println(randomNumbers[x]);
      }
   }
   
   public static void endPrompts()
   throws InterruptedException{
      //Print average
      System.out.println("The average is: " + average);
      //Does the user want to repeat
      System.out.print("Would you like to try again? (Y/N) ");
      restart = userInput.next();
      restart = restart.toLowerCase();
      //Interpret user response
      if(restart.equals("y") || restart.equals("yes"))
      {
         //User prompt to use the same numbers as the last attempt
         System.out.print("Would you like to use the same numbers? (Y/N) ");
         sameNums = userInput.next();
         sameNums = sameNums.toLowerCase();
         if(sameNums.equals("y") || sameNums.equals("yes"))
         {
            //If the user says yes, keep the values and try again
            sameNumbers = true;
            getRandom();
            endPrompts();
         }
         else if(sameNums.equals("n") || sameNums.equals("no"))
         {
             //If the user wants to use different values, start the entire code over
             sameNumbers = false;
             startCode();
         }
         else
         {
            //If the user doesn't input one of the approved inputs, restart
            System.out.println("Input not understood.");
            startCode();
         }
         }
      else if(restart.equals("n") || restart.equals("no"))
      {
         //If the user doesn't want to continue, display end message
         System.out.println("Thanks for playing!");
         System.exit(0);
      }
      else
      {
         //If the user doesn't input one of the approved inputs, exit
         System.out.println("Input not understood. Goodbye!");
         System.exit(0);
      }
     
   }
   
   public static boolean numberOrNot(String input)
   {
      /* 
         This method is designed to check to make sure the user inputs an integer
         instead of any other data type.
      */
         try
         {
            //Try to convert the string to an int
            Integer.parseInt(input);
         }
         //If it cannot convert the string to int, tell the program it cannot
         catch(NumberFormatException ex)
         {
            return false;
         }
         //If it wasn't stopped by the error, tell the program it can convert it
         return true;
  }
  
  public static int UserInputInt(int userInt)
  {
      /*
         This method is designed to convert the user input to an integer if it
         can, or tell the user to input an integer.
      */
      //Have the user input a String
      input = userInput.next();
      //Call the numberOrNot method to check if the user input an integer
      if(numberOrNot(input))
      {
         //If it can convert the string to an int, do it.
         userInt = Integer.parseInt(input);
      }
      else
      {
         //If it cannot convert the string to an int, have the user input an integer
         System.out.print("Please enter an Integer: ");
         userInt = UserInputInt(userInt);
      }
      //Return the value of the new integer
      return userInt;
  }
   
}


Is This A Good Question/Topic? 0
  • +

Replies To: Counting sort to random number generator.

#2 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 780
  • View blog
  • Posts: 3,184
  • Joined: 13-June 14

Re: Counting sort to random number generator.

Posted 02 December 2017 - 12:00 AM

I'm not too sure what your question is. Do you know how the counting sort algorithm works? If you understand the steps, or have pseudocode for it, it shouldn't be too hard to implement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1