Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4.  *
  5.  * @author JakeFromStateCS
  6.  */
  7. public class DoubleDifference {
  8.    
  9.     public static void main(String[] args) {
  10.     double testArray1[] = new double[10];
  11.     for( int i = 0; i < 10; i++ ) {
  12.         testArray1[i] = getRandomDouble( -10.0, 10.0 );
  13.     }
  14.     double difference = getDifference( testArray1 );
  15.     System.out.println( "The difference is: " + difference );
  16.     }
  17.    
  18.     /*
  19.     getRandomDouble:
  20.     Input:
  21.         lowerBound: The lowest possible number
  22.         upperBound: The highest possible number
  23.     Purpose:
  24.         Generates a random double between the lower and upper bounds
  25.     Returns:
  26.         double result: The result of the random generation
  27.     */
  28.     public static double getRandomDouble( double lowerBound, double upperBound ) {
  29.     //Returns a random double between 0 and 1
  30.     double randomDouble = new Random().nextDouble();
  31.     //If lowerBound is 2, upperBound is 10 and randomDouble is 0.3
  32.     // 2 + ( 0.3 * ( 10 - 2 ) )
  33.     // 2 + ( 0.3 * 8 )
  34.     // This shifts the number scale to the right
  35.     double result = lowerBound + ( randomDouble * ( upperBound - lowerBound ) );
  36.     return result;
  37.     }
  38.    
  39.     /*
  40.     getDifference:
  41.     Input:
  42.         input: An array of doubles
  43.     Purpose:
  44.         Return the difference between the highest and lowest value in an array of doubles
  45.     Returns:
  46.         double: The difference between the largest and smallest value in an array of doubles
  47.     */
  48.     public static double getDifference( double input[] ) {
  49.     //We want to set the smallest value to the largest value possible
  50.     //This way, every value will be lower than or equal to this one
  51.     double smallestValue = Double.MAX_VALUE;
  52.     //We want to set the largest value to negative Double.MAX_VALUE
  53.     //This will ensure that it is not possible for there to be a smaller value
  54.     double largestValue = -Double.MAX_VALUE ;
  55.    
  56.     //Loop through each element of the array
  57.     for( int key = 0; key < input.length; key ++ ) {
  58.         double value = input[key];
  59.         if( value > largestValue ) {
  60.         largestValue = value;
  61.         }
  62.         if( value < smallestValue ) {
  63.         smallestValue = value;
  64.         }
  65.         System.out.println( "input[" + key + "] = " + value] );
  66.     }
  67.     System.out.println( "Largest: " + largestValue );
  68.     System.out.println( "Smallest: " + smallestValue );
  69.     return largestValue - smallestValue;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement