Question

In: Computer Science

You are expected to write a program from scratch. In the program, an array will be...

You are expected to write a program from scratch. In the program, an array will be initialized with 23 random integers between 1000 and 1999 (inclusive). The output of the program is 4 lines on the screen, specifically,

  • All elements in the array (line 1)
  • All elements in reverse order (line 2)
  • Every element that is less than 1500 and also at an odd index (line 3)
  • Every odd element that is larger than 1500 (line 4)

Note that you have to use the method Math.random() we covered previously to generate the random numbers. Other ways like using Random class we haven't covered or just reading in 23 numbers from keyboards are NOT accepted.

Please explain/comment on what each line means and what the process is. Thank you, I appreciate it.

Solutions

Expert Solution

Please find the code below::

RandomArray.java

package classes10;

public class RandomArray {
   public static void main(String[] args) {
       //declare array of size 23
       int myArray[] = new int[23];

       //populate array with random data
       for(int i=0;i<23;i++){
           myArray[i] = (int) (Math.random()*1000+999);
       }

       //printing array in right order
       for(int i=0;i<23;i++){
           System.out.print(myArray[i]+" ");
       }

       System.out.println();
       //printing array in reverse order
       for(int i=22;i>=0;i--){
           System.out.print(myArray[i]+" ");
       }


       System.out.println();
       //printing odd index number less than 1500
       for(int i=0;i<23;i++){
           if(i%2==0 && myArray[i]<1500)
               System.out.print(myArray[i]+" ");
       }

       System.out.println();
       //printing odd index number less than 1500
       for(int i=0;i<23;i++){
           if(myArray[i]>1500 && myArray[i]%2!=0)
               System.out.print(myArray[i]+" ");
       }

   }
}

output:


Related Solutions

1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
// JavaLanguage . You need to write a program that asks the user for an array...
// JavaLanguage . You need to write a program that asks the user for an array size, and then asks the user to enter that many integers. Your program will then print out the sum , average, largest and smallest of the values in the array.
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Write a program that uses an array of doubles initialized to whatever values you wish. Write...
Write a program that uses an array of doubles initialized to whatever values you wish. Write methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same...
Write a program that asks the user to enter an array of 8 characters then you...
Write a program that asks the user to enter an array of 8 characters then you have to check if characters ‘b’ or ‘a’ appears within the characters and replace them with ‘B’ or ‘A’. For example you enter “a m a l a a b d” The output should be “A m A l A A B d”
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT