Hello, I am using the Intro to Java programmming and data structures by Y. Daniel Liang.Our professor wants us to generate a random number in java between 1 and 100. Then prompt the user for a number between 1 and 4. Next, the program will determine if the number entered by the user is a factor of the random number. I feel like I have some parts of the code down pack but not all of it. Thus far I have: import java.util.Scanner;public class Module_Project3 {public static void main(String[] args) {Scanner input = new Scanner(System.in);int num1 = (int)(Math.random()*100+1);int num2 = (int)(Math.random()*100+1);int num3 = (int)(Math.random()*100+1);int num4 = (int)(Math.random()*100+1);

Question
Asked Sep 26, 2019
15 views

Hello, I am using the Intro to Java programmming and data structures by Y. Daniel Liang.

Our professor wants us to generate a random number in java between 1 and 100. Then prompt the user for a number between 1 and 4. Next, the program will determine if the number entered by the user is a factor of the random number.

 

I feel like I have some parts of the code down pack but not all of it. Thus far I have:

 

import java.util.Scanner;
public class Module_Project3
{

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

int num1 = (int)(Math.random()*100+1);
int num2 = (int)(Math.random()*100+1);
int num3 = (int)(Math.random()*100+1);
int num4 = (int)(Math.random()*100+1);

check_circle

Expert Answer

Step 1

A java program to check the user’s input is a factor of random number generated is given below;

Program:

//Import a required package

import java.util.Scanner;

//Create a class

public class RandFactor

{

     //Define a main() function

     public static void main(String[] args)

     {

/*Create an object for Scanner class to get the input from the user*/

          Scanner input = new Scanner(System.in);

/*Generate a random number between 1 and 100 using random() method*/

          int num1 = (int)(Math.random()*100+1);

          //Print the string

System.out.println("Enter the number between 1 and 4: ");

          //Get the input from the user using next() method

          int userinput=input.nextInt();

          //Create a "for" loop to iterate from 1 to 4

          for(int i=1;i<=4;i++)

          {

              //Check whether the user input is between 1 and 4

              if(i==userinput)

              {

/*Check whether the user input is a factor of generated random number*/

                   if(num1%userinput==0)

                   {

                        //Print the generated random number

                        System.out.println("The generated random number is: "+num1);

                        //Display the output string

System.out.println("The given input "+userinput+"is a factor of generated random number"+num1);

                   }

                   //Otherwise

                   else

                   {

                        //Print the generated random number

System.out.println("The generated random number is: "+num1);

                        //Display the output string

System.out.println("The given input "+userinput+" is not a factor of generated random number"+num1);

                   }

              }

          }

     }

}

Screenshot of program #1:

fullscreen
Step 2

Screenshot of program #2:

/Create a "for" loop to iterate from 1 to 4
i=1; 1< - 4; 1 ++
for (int
//Check whether the user input is between 1 and 4
if (i-userinput)
1/Check whether the user input is a factor of generated random number
if (numleuserinput=0)
//Print the generated random number
System.out.println ("The generated random number is: "+numl)
/Display the output string
System.out.println ("The given input "+userinput+
"is a factor of generated random number"+numl)
//otherwise
else
//Print the generated random number
System.out.print ln ("The generated random number is: "+num1);
//Display the output string
System.out.println ("The given input "+userinput
"is not a factor of generated random number"+numl);
help_outline

Image Transcriptionclose

/Create a "for" loop to iterate from 1 to 4 i=1; 1< - 4; 1 ++ for (int //Check whether the user input is between 1 and 4 if (i-userinput) 1/Check whether the user input is a factor of generated random number if (numleuserinput=0) //Print the generated random number System.out.println ("The generated random number is: "+numl) /Display the output string System.out.println ("The given input "+userinput+ "is a factor of generated random number"+numl) //otherwise else //Print the generated random number System.out.print ln ("The generated random number is: "+num1); //Display the output string System.out.println ("The given input "+userinput "is not a factor of generated random number"+numl);

fullscreen
Step 3

Screenshot of output #1...

Enter the number between 1 and 4:
2
The generated random number is: 32
The given input 2is a factor of generated random number32
help_outline

Image Transcriptionclose

Enter the number between 1 and 4: 2 The generated random number is: 32 The given input 2is a factor of generated random number32

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Java

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Select Invoice_number, invoice_due_date, payment_date and the number of days late for those invoices...

A: Note: The given query has been compiled and executed in "sqlfiddle" online compiler. Creating an “In...

question_answer

Q: I can't figure this one out for the life of me. I've been stuck on it for 4 hours now... HELP! The p...

A: import re inputs = 0numbers_sum = 0 # enter the stringstring = input('Enter a string: ') # condition...

question_answer

Q: What is the difference between Python and JavaScript coding?

A: JavaScriptIt is a web programing language.it is a cross- platform language.Its code is integrated wi...

question_answer

Q: Develop pseudocode for the problem of reading in an arbitrary number of DNA (deoxyribonucleic acid) ...

A: PseudocodeCreate a dictionary data structure to store the DNA base and the complementary base as ser...

question_answer

Q: Question 2 3 4

A: Question 2:Given binary value = 11012   The given binary value does not represents the decimal numbe...

question_answer

Q: (Latest Revision: Apr 01, 2019) MST Problems First MST Problem:Suppose G=(V,E) is a connected undire...

A: Given (Table):According to the kruskal’s algorithm:Arrange every edge in increasing order of their w...

question_answer

Q: Show a derivation tree for w = aaabbaaa using the grammar in Example 5.1. EXAMPLE 5.1 The grammar G ...

A: Solution:Given that,w = aaabbaaa

question_answer

Q: Normalization               Repair the ER Diagrams into at least 1nf, 2nf and 3nf Give explanations...

A: Hi due to lack of adequate information we are unable to provide any solution. Please provide us more...

question_answer

Q: I have a HW that is asking me to draw on ERD diagram for a simple purchasing system that has order w...

A: ERD diagram for Purchasing System:The ERD diagram for the simple purchasing system is as follows: