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

Write a class called SortedArray.java. You can do all of this work in the main()

ID: 3584173 • Letter: W

Question

Write a class called SortedArray.java. You can do all of this work in the main() method of your class but if there are places to use methods effectively, feel free to do so. Prompt the user to enter a number between 1 to 10 and create an array of integers that size. Re-prompt for out-out-bounds entries. Print the array of integers (should be all zeros) Prompt the user to enter numbers to be placed in the array until the array is "full". Here's the "tricky" part: Add the numbers to the array so that they remain sorted at all times. If the number is the biggest in the array it should go to the end. If the number is the smallest it should go to the beginning and all previously entered numbers should be moved to make room. If the number belongs in the middle somewhere put it where it belongs and move everything else down, It would be very helpful to keep a variable that tracks how many elements in the array are valid. The zeros at the beginning are just place holders in the array so your numElements variable should be 0 to start. After adding one number to the array it should go up to 1 and so on. This will help you to figure out where the new numbers should be placed and where the old numbers need to be shifted to and also allow you to ignore any zeros that are left over from creating the array (they are not valid data). After each user-entered number, display the updated array. The program ends when the array is full (numElements == array. length) and sorted.

Explanation / Answer

import java.util.*;
public class array
{
public static void main(String[]args)
{
int num[]={5,4,3,2,1,0};
int l=num.length;
int i,j;
System.out.println("given number");
for(i=0;iSystem.out.println(""+num[i]);
}
System.out.println(" ");
System.out.println("ascending order");
Array.sort(num);
for(i=0;i{
System.out.println(" "+num[i]);
}
}

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