Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m in java one and need help with this code. please follow the directions give

ID: 3685717 • Letter: I

Question

I'm in java one and need help with this code. please follow the directions given and test your program to make sure it works as it is required. I will apreciate your help. thank you.please use the given code.

Programming Concepts

Random Number Generation

Searching Arrays

Exercise Location

For this exercise, you will copy a program from my directory into yours. It doesn't matter what directory this exercise is located in. You will need to copy this command and paste it into Loki:

cp ~lkoperski/1404_Lab/Exercises/Exercise11.java .

Exercise Description

You will create an array of size 5, and fill it with random numbers between 0 and 5. If the number 2 exists, output "2 exists!". Otherwise, output "2 doesn't exist!"

User input is underlined

user@loki:~$ java Exercise11

[4, 1, 3, 3, 3] 2 doesn't exist!

user@loki:~$ java Exercise11

[4, 2, 2, 2, 0]

2 exists!

1 import java.util.Random; 2 public class Exercise11 4 public static void main (String [] args Random r = new Random(); int[] nums = new int [ ]; // For each element in the nums array, fill it with a random 7 10 / number between 0 and5 12 13 14 15 16 17 18 19 20 21 // Print out the array (don't change this code) for ( int i = ; i

Explanation / Answer

Exercise 11.java

import java.util.Random;

public class Exercise11 {

   public static void main(String[] args)
   {
       Random r=new Random();
      
       int[] nums=new int[5];
      
       //filling array with random numbers from 1 to 5
       //int n = rand.nextInt(5) + 1;
       //5 is the maximum and the 1 is our minimum
      
       for(int i = 0; i < nums.length; i++) {
nums[i] = r.nextInt(5)+1;
//System.out.print(nums[i] + " ");
}
      
       for(int i=0;i<nums.length;i++)
       {
           if(i==0)
               System.out.print("[");
           System.out.print(nums[i]+",");
          
           if(i==nums.length-1)
               System.out.print("]");          
       }
       int flag=0;
       for(int i=0;i<nums.length;i++)
       {
           if(nums[i]==2)
           {
               flag=1;
           break;
           }
          
       }
      
       if(flag==0)
       {
           System.out.println("2 doesn't exists");
       }
       else if(flag==1)
       {
           System.out.println("2 exists");
       }
   }
}

Output

[4,4,1,5,5]2 doesn't exists

[1,5,2,5,5]2 exists

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote