Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Lesson 35 Coding Activity
- *
- * Write four overloaded methods called randomize. Each method will
- * return a random number based on the parameters that it receives:
- *
- * Write four overloaded methods called randomize.
- * Each method will return a random number based on the parameters that it receives:
- * randomize() - Returns a random int between min and max inclusive. Must have two int parameters.
- * randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter.
- * randomize() - Returns a random double between min and max inclusive. Must have two double parameters.
- * randomize() - Returns a random double between 0 and max inclusive. Must have one double parameter.
- *
- * Because these methods are overloaded, they should be declared in the same class.
- * To simulate this, copy all four methods into the single Code Runner box.
- */
- import java.util.Scanner;
- import java.lang.Math;
- class Lesson_35_Activity {
- public static int randomize(int a, int b)
- {
- int dif = Math.abs(a-b)+1;
- int min = 0;
- if (a >= b)
- min = b;
- else
- min = a;
- int rand = (int)((Math.random()*dif)+min);
- return rand;
- }
- public static int randomize(int a)
- {
- int rand = (int)((Math.random()*(a+1)));
- return rand;
- }
- public static double randomize(double a, double b)
- {
- double dif = Math.abs(a-b);
- System.out.println(dif);
- double min = 0;
- if (a >= b)
- min = b;
- else
- min = a;
- double rand = ((Math.random()*dif)+min);
- return rand;
- }
- public static double randomize(double a)
- {
- double rand = ((Math.random()*(a)));
- return rand;
- }
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in);
- double a = scan.nextDouble();
- double b = scan.nextDouble();
- System.out.print(randomize(a,b));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement