Question

Please complete the lab following the guidelines using JAVA
Activity 1 Write a program called AnalyzeNumbers. This program will have array that holds 10 random integers from 1 to 5. Output the 10 random numbers to the screen using an enhanced for loop. This program will also have a method called frequency that takes an integer array as the parameter and it will return an array that holds the frequency of numbers. Index 0 will hold the frequency of 1, index 1 will hold the frequency of 2, and so on. Use this method in your program to create an array that holds the frequency of the random numbers in your array. Use an enhanced for loop to output the frequency of each number. Then display the number that appears the most in your array-you will need to account for numbers appearing the same amount for the mode.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Math.random() with ranges

(int)((Math.random()*(max-min+1))+min)

Example

Range 5-10 max is 10 min is 5

Max-min+1=10-5+1=6

(int)((Math.random()*6)+5)

In the above question range is 1-5

Max-min+1=5-1+1=5

(int)((Math.random()*5)+1)

Program

public class AnalyzeNumber{
static int freqarray[] = new int[5];

public static void main(String[] args) {
int i=0;
  
int array[]=new int[10];// to store 10 integers
for( i=0;i<10;i++)
{ array[i]= (int)((Math.random()*5)+1); //store 1-5 random numbers

}
System.out.println("Random number are");
  

for(i=0;i<10;i++)
{ System.out.println(array[i]); //store 1-5 random numbers
}

// AnalyzeNumber ob = new AnalyzeNumber();
frequency(array);
System.out.println("Frequency of number are");
  
for( i=0;i<5;i++)
{
System.out.println("frequency of "+ (i+1)+" is "+freqarray​[i]);
}
}
  
static int[] frequency (int arr[])
{
  
  
  
for( int i=0;i<5;i++)
{
freqarray[i]=0;// for counting initial value to be zero
}
  
for( int i=0;i<10;i++)
{
for(int j=0;j<5;j++)
{ if(arr[i]==(j+1))// checking integer value from1-5
freqarray[j]++;}
  
}
return freqarray;
}
}

Output for the given program

Random number are
4
4
5
5
4
3
4
4
3
1
Frequency of number are
frequency of 1 is 1
frequency of 2 is 0
frequency of 3 is 2
frequency of 4 is 5
frequency of 5 is 2

Explanation

run program and see the comments

if(arr[i]==(j+1)) // if array value is checked from 1to 5

// j is the position which so position 0 checks for 1 (j+1) //frequency
freqarray[j]++; // to count frequency of j+1 and store and //increment at position j

​​​

Add a comment
Know the answer?
Add Answer to:
Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers....
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
  • Please follow the instructions carefully. Thank you! For the second activity, I outputted the superhero class...

    Please follow the instructions carefully. Thank you! For the second activity, I outputted the superhero class below. SUPERHERO CLASS public class Superhero{   private String alias;   private String superpower;   private int health;   public Superhero(){       alias= "unkown";       superpower= "unknown";       health= 50; //Realized I did not use default constructor while going through instructions of lab   }   public Superhero(String alias1, String superpower1,int health1 ){       alias=alias1;       superpower=superpower1;       if(health1>=0 && health1<=50)           health= health1;       else if(health1<0||health1>50)           health=25;   }   public void setalias(String alias1){       alias=alias1;   }   public void setsuperpower(String...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • Using Java write a program that performs the following: 1. Define a class that contains an...

    Using Java write a program that performs the following: 1. Define a class that contains an array of type int as a data member. 2. Define a constructor that creates an array of 1024 elements and assigns it to the class data member. The constructor shall populate the elements with random numbers ranging in value from 1 to 1024. 3. Define and implement a search() method that take a parameter of type int and returns the index location if the...

  • write a java program that does the following Part one Use a For loop to compute...

    write a java program that does the following Part one Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic (Nested loops: Indent text) so that the...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

  • 5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create...

    5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create a method called generateSuperLottoNumbers() that returns an array of 6 random SuperLotto lottery numbers. The first 5 numbers must be from the range 1 to 47 The 6th number (the MEGA) must be from 1 to 27. Create a method called printTicket() that takes an integer array as an parameter it will loop through the integer array and print out the data Display the...

  • 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...

  • Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array...

    Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array of 100 integers. Initialize the array to have all zeros. 2. Puts even numbers starting from 6 (excluding multiples of 12) into the first 50 slots of the array above. So numbers 6,8,10,14,16,18,20,22,26, etc. go into the first 50 positions in the array. Print the array. 3. Puts random numbers between 45 and 55 into the second 50 slots of the array above (second...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

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