Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.Scanner; import java.util.Arrays; public class Lab 7 Template { public static void main ( String [ ] args ) { / / = =
import java.util.Scanner;
import java.util.Arrays;
public class LabTemplate
public static void mainString args
Declare some variables. You might need
an integer for the array size,
an integer for the exchange element, and
a scanner object for requesting input from System.in
Scanner object for user input
Scanner scanner new ScannerSystemin;
int size;
int exchangeElement;
Request array size from the user
System.out.printHow many elements in the array: ;
int size scanner.nextInt;
Declare an int array by the size you received above
int array new intsize;
Request an integer from the user and save it
Fill in the Array
for int i ; i size; i
Request the next int from the user,
System.out.printPlease enter the next value: ;
save it to the ith element of the array
arrayi scanner.nextInt;
Sort the array's elements in increasing order
Here we will use Selection Sort like algorithm.
Arrays.sortarray;
The first for loop iterates all elements as elementi
for int i ; i size ; i
arrayi arrayi ;
arraysize ;
The second for loop finds the right position of elementi
for int j i ; j size; j
Compare ith value and jth value,
If arrayi arrayj swap these two values
if arrayi arrayj
For example, let arrayi arrayj to swap
arrayi and arrayj means arrayi will become
and arrayj will have
To swap the values in two positions, you would need an
extra variable to temporarily hold the value. For example,
temp arrayi;
arrayi arrayj;
arrayj temp;
int temp arrayi;
arrayi arrayj;
arrayj temp;
System.out.println;
Display the sorted array
System.out.printThe array after sorting: ;
for int i ; i size; i
System.out.printarrayi;
System.out.println;
Remove the minimum in the sorted array
As our array is sorted in the increasing order, to remove the
minimum, we just remove the first element in the array. It is like
shifting the array to the left by one element.
Because we are doing shifting, in each iteration, we use two
values, one at index i the other at index i
To prevent us from going over the boundary of array, the following
offset variable should be set as the correct value.
int offset ;
In this for loop, we move the element at i to the position i
for int i ; i size ; i
Move arrayi to arrayi
arrayi arrayi ;
The last element will be set as zero. Remember the greatest index
should be array.length
arraysize ;
Display the Array after removing the first element
System.out.printThe array with the minimum removed: ;
for int i ; i size; i
System.out.printarrayi;
System.out.println;
System.out.println;
Search for an element and remove it
Ask the user which element to remove
Use your Scanner and the int variable valueToRemove to get a value for search
System.out.printEnter the value to search and remove: ;
int valueToRemove scanner.nextInt;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started