Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Attempting to call in a method for random number generator to generate 20 random numbers in the range of 1 and 2 inclusively. Display needs

Attempting to call in a method for random number generator to generate 20 random numbers in the range of 1 and 2 inclusively. Display needs to show the cntr numbers and random numbers horizontally. Here is the java code so far.

package lab2Package;

import java.util.Scanner;

public class Lab2Class

{

public static void main(String[] args)

{

//declare variables

String name;

int userNum;

int i;

double userDoubleNum;

int randomNum;

//initialize variables

name = "none yet";

userNum = 0;

userDoubleNum = 0.0;

randomNum = 0;

//enter your name at the keyboard

name = inputString("Enter your name\t");

System.out.println("My name is "+ name);

//enter 5 integers at the keyboard and print it out

System.out.println("Integers from keyboard");

for(i = 0; i < 5; i++)

{

userNum = inputInteger("Enter an integer");

System.out.println("You have entered "+ userNum);

}

//enter 5 decimal numbers at the keyboard and print each one out

System.out.println("Decimal values from keyboard");

for(i = 0; i < 5; i++)

{

userDoubleNum = inputDouble("Enter a decimal value");

System.out.println("You have entered "+ userDoubleNum);

}

//generate 20 random numbers and print each one out

System.out.println("Random numbers between one and two inclusively");

for(i = 0; i < 20; i++)

{

randomNum = genRandom()

}

}//end main

//

// inputString

//

// the purpose of this method is to input a string from the keyboard

//

// Input: prompt the prompt message for the dialog box

//

// Return: inputStr the string that was entered at the keyboard

//

public static String inputString(String prompt)

{

Scanner input = new Scanner(System.in);

String inputStr;

//prompt user to enter in string

System.out.print(prompt);

//read in value as string from keyboard

inputStr = input.nextLine();

//return input string

return(inputStr);

}//end input string

//

// inputInteger

//

// the purpose of this method is to input an integer value form the keyboard

//

// Input: prompt the prompt message for the user

//

// Return: num the integer entered at the keyboard

//

public static int inputInteger(String prompt)

{

Scanner input = new Scanner(System.in);

String cleanUpStr;

int num;

num = 0;

cleanUpStr = "no string yet";

System.out.println(prompt);

num = input.nextInt();

cleanUpStr = input.nextLine();

return(num);

}//end inputInteger

//

// inputDouble

//

// the purpose of this method is to input an decimal value form the keyboard

//

// Input: prompt the prompt message for the user

//

// Return: num the integer entered at the keyboard

//

public static double inputDouble(String prompt)

{

Scanner input = new Scanner(System.in);

String cleanUpStr;

double num;

num = 0.0;

cleanUpStr = "no string yet";

System.out.println(prompt);

num = input.nextDouble();

cleanUpStr = input.nextLine();

return(num);

}//end inputDouble

//

// genRandom

//

// the purpose of this method is to generate a random number

// between the minimum and maximum values specified inclusively

//

// Input: min the minimum value of the range of numbers

// max the maximum value of the range of numbers

//

// Return: num the random number in the given range

//

public static int genRandom(int min, int max)

{

double rNum; //the original random number generated

double range; //the range of the random numbers allowed

int num;

rNum = 0.0;

range = 0.0;

num = 0;

//validate the input

if((min >= 0) && (min <= max))

{

//generate a random number 0.0 <= rNum < 1.0

rNum = Math.random();

//determine the range

range = (double)max - (double)min + 1.0;

//scale the number between min and max inclusively

rNum = rNum * range;

rNum = rNum + (double)min;

//convert back to integer value

num = (int)rNum;

}

else

{

System.out.println("Invalid input arguements of "+ min +" and "+ max);

System.out.println("Random numbers cannot generate");

}

//return the integer

return(num);

}//end genRandom

}//end lab2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions