Advertisement
StefanTobler

Lesson 35 Activity

Dec 8th, 2016
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. /*
  2.  * Lesson 35 Coding Activity
  3.  *
  4.  * Write four overloaded methods called randomize. Each method will
  5.  * return a random number based on the parameters that it receives:
  6.  *
  7.  * Write four overloaded methods called randomize.
  8.  * Each method will return a random number based on the parameters that it receives:
  9.  * randomize() - Returns a random int between min and max inclusive. Must have two int parameters.
  10.  * randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter.
  11.  * randomize() - Returns a random double between min and max inclusive. Must have two double parameters.
  12.  * randomize() - Returns a random double between 0 and max inclusive. Must have one double parameter.
  13.  *
  14.  * Because these methods are overloaded, they should be declared in the same class.
  15.  * To simulate this, copy all four methods into the single Code Runner box.
  16.  */
  17.  
  18.  
  19. import java.util.Scanner;
  20. import java.lang.Math;
  21.  
  22. class Lesson_35_Activity {
  23.  
  24.     public static int randomize(int a, int b)
  25.     {
  26.     int dif = Math.abs(a-b)+1;
  27.     int min = 0;
  28.     if (a >= b)
  29.       min = b;
  30.     else
  31.       min = a;
  32.     int rand = (int)((Math.random()*dif)+min);
  33.     return rand;
  34.     }
  35.      
  36.     public static int randomize(int a)
  37.     {
  38.     int rand = (int)((Math.random()*(a+1)));
  39.     return rand;
  40.     }
  41.  
  42.     public static double randomize(double a, double b)
  43.     {
  44.     double dif = Math.abs(a-b);
  45.     System.out.println(dif);
  46.     double min = 0;
  47.     if (a >= b)
  48.       min = b;
  49.     else
  50.       min = a;
  51.     double rand = ((Math.random()*dif)+min);
  52.     return rand;
  53.     }    
  54.  
  55.     public static double randomize(double a)
  56.     {
  57.     double rand = ((Math.random()*(a)));
  58.     return rand;
  59.     }    
  60.      
  61.     public static void main(String[] args)
  62.      {
  63.       Scanner scan = new Scanner(System.in);
  64.       double a = scan.nextDouble();
  65.       double b = scan.nextDouble();
  66.       System.out.print(randomize(a,b));
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement