Question

Write four overloaded methods called randomize. Each method will return a random number based on the...

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. Must have two double parameters.
  • randomize() - Returns a random double between 0 and max. Must have one double parameter.

Because these methods are overloaded, they should be declared in the same class, so there is only one code-runner to test all four methods.

In accordance to the assignment; How do I fix this code, meaning what is the fixed version of the current code I have now.

import java.util.Scanner;

class Lesson_35_Activity {
  
    //returns random int b/t min and max inclusive; has 2 int parameters
    public static int randomize(int min, int max) 
    {
      int x = (int)(Math.random() * ((max - min) + 1)) + min;
      return x;
    }
    
    //returns random int b/t 0 and max inclusive; has one int parameter
    public static int randomize(int max) 
    {
      int x = (int)(Math.random() * (max + 1));
      return x;
    }
    
    //returns random double b/t min and max inclusive; two double parameters
    public static double randomize(double min, double max) 
    {
      double x = (double)(Math.random() * ((max - min) + 1)) + min;
      return x;
    }
    
    //returns random double b/t 0 and max inclusive; has one double parameter. 
    public static double randomize(double max) 
    {
      double x = (double)(Math.random() * (max + 1));
      return x;
    }
     
    public static void main(String[] args)
     {
      Scanner scan = new Scanner(System.in);
      
      int mi = scan.nextInt();
      int ma = scan.nextInt();
      
      System.out.println(randomize(mi, ma));
      System.out.println(randomize(0, ma));  
      
      double mii = mi;
      double maa = ma; 
      
      System.out.println(randomize(mii, maa));
      System.out.println(randomize(0, maa));
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per requirement submitted above kindly find below solution.

Lesson_35_Activity.java :


//import package
import java.util.Scanner;

//Java class
class Lesson_35_Activity {

   // returns random int b/t min and max inclusive; has 2 int parameters
   public static int randomize(int min, int max) {
       int x = (int) (Math.random() * ((max - min) + 1)) + min;
       return x;
   }

   // returns random int b/t 0 and max inclusive; has one int parameter
   public static int randomize(int max) {
       int x = (int) (Math.random() * (max + 1));
       return x;
   }

   // returns random double b/t min and max inclusive; two double parameters
   public static double randomize(double min, double max) {
       double x = (double) (Math.random() * ((max - min) + 1)) + min;
       return x;
   }

   // returns random double b/t 0 and max inclusive; has one double parameter.
   public static double randomize(double max) {
       double x = (double) (Math.random() * (max + 1));
       return x;
   }

   // main method, which is entry point of the program
   public static void main(String[] args) {
       // object of Scanner class
       Scanner scan = new Scanner(System.in);
       // asking min and max number
       System.out.print("Enter min number : ");
       int mi = scan.nextInt();// reading min number
       System.out.print("Enter max number : ");
       int ma = scan.nextInt();// reading max number
       // checking number
       if (mi < ma) {
           System.out.println(randomize(mi, ma));// method call
           System.out.println(randomize(0, ma));// method call

           double mii = mi;
           double maa = ma;

           System.out.printf("%.2f", randomize(mii, maa));
           System.out.printf("\n%.2f", randomize(0, maa));
       } else {
           // when minimum number is greater than maximum number then display
           System.out.println("Enter min number " + mi + " should be less than max " + ma);
       }
   }
}

==================================

Output :Compile and Run Lesson_35_Activity.java to get the screen as shown below

Screen 1:Lesson_35_Activity.java , screen when second number is less than first number

Screen 2:Screen showing random numbers

NOTE :PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Write four overloaded methods called randomize. Each method will return a random number based on the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the following activity, you will submit your entire program to the code runner, from your...

    For the following activity, you will submit your entire program to the code runner, from your first import statement to your last curly bracket. Please download the Lesson 35 Activity template (Links to an external site.) and use it to write your code. Additionally, you can download the Lesson 35 Coding Activity (PDF) (Links to an external site.) to print or use as an offline reference. If you need a refresher on how to use this Code Runner, watch the...

  • (1) Identify the true statements about overloaded methods. Select ALL that apply. a. they can have...

    (1) Identify the true statements about overloaded methods. Select ALL that apply. a. they can have different parameter types b. they can have different return types c. they must have different modifiers d. they can have a different number of parameters e. they must have the same method name (2) 6. int length = 5, width = 8; 7. area(length, width); As used in line 7, length and width are called the __?__ of the area method. (3) public static...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Create a class named Billing that includes three overloaded computeBill() methods for a photo book store....

    Create a class named Billing that includes three overloaded computeBill() methods for a photo book store. • When computeBill() receives a single parameter, it represents the price of one photo book ordered. Add 8% tax, and return the total due. • When computeBill() receives two parameters, they represent the price of a photo book and the quantity ordered. Multiply the two values, add 8% tax, and return the total due. • When computeBill() receives three parameters, they represent the price...

  • 5. Write a static method "f(n)" in the space provide, that returns O if n is...

    5. Write a static method "f(n)" in the space provide, that returns O if n is even, and if n is odd, returns 1 or -1 according as n is greater than or less than 0. importjava.util.Scanner; public class Q_05 Your "f(n)" method: public static void main(String args[]) mana int n; Scanner input = new Scanner(System.in); System.out.print("Please enetrn: "); n=input.nextInt(); System.out.println(f(n)); "Method f(n)" 7. Write a static method "max" in the space provide, that returns the maximum value from 3...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

    Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...

  • In this problem you'll get to use one of Java's random number generators, java.util.Random (see the...

    In this problem you'll get to use one of Java's random number generators, java.util.Random (see the docs for Random). This class generates a random double precision number uniformly distributed in the range [0.0 ... 1.0). That is, the numbers go from 0 to 1, including 0 but excluding 1. It is a common need in Java programming to generate a random integer number that's uniformly distributed in the range of [0 ... n), such as we saw in the dice...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT