In: Computer Science
Write program in Java
import java.util.Scanner;
public class Lab7Program {
public static void main(String[] args) {
//1. Create a double array that can hold 10 values
//2. Invoke the outputArray method, the double array is the actual
argument.
//4. Initialize all array elements using random floating point
numbers between 1.0 and 5.0, inclusive
//5. Invoke the outputArray method to display the contents of the
array
//6. Set last element of the array with the value 5.5, use length
to access last index.
//7. Invoke the outputArray method to display the contents of the
array
//8. Allow user to input elements 2 through 6 through
keyboard
//9. Invoke the outputArray method to display the contents of the
array
//10. Compute the sum of all elements in the array
//11. Display the sum you found in step #9
//12. Find the index of the smallest element in the array.
//13. Display the index of the smallest element and the value of
the
// smallest element.
//15. Invoke bubble sort method, the array is the actual
argument.
//16. Invoke the outputArray method, the array is the actual
argument.
//18. Invoke linearSearch method, search for value 5.5. Output a
statement informing if the
// search value was found or not.
}
// 3. Implement outputArray method that will display the contents
of the array
// on a single line, elements separated by spaces.
// There should be an empty line in the output after the array
elements.
// 14. Implement bubble sort method here. The double type array is
the formal parameter.
// 17. Implement linearSearch method here. The double type array
and search value are the formal parameters.
}
Screenshot of the code:
Output:
Code to copy:
//Import for random number.
import java.util.Random;
//Import for input.
import java.util.Scanner;
//Define the class.
public class Lab7Program
{
//Define the main method().
public static void main(String[] args)
{
//Create the scanner object.
Scanner scobj = new Scanner(System.in);
//Define the variables.
double array[]= new double[10],value=0;
double sum=0, min=0;int index=0;
//Use loop to fill array elements.
for(int i=0;i<array.length;i++)
{
//Generate the random number.
Random r = new Random();
//Store random number in array.
array[i] = 1.0 + (5.0 - 1.0)
* r.nextDouble();
}
//Call the outputArray() method to print
//the array elements.
outputArray(array);
System.out.println();
System.out.println("After Replacing the last"
+ " element with 5.5");
//Replace the last elements with 5.5
array[array.length-1]=5.5;
//Call the outputArray() method to print
//the array elements.
outputArray(array);
System.out.println();
System.out.println("Enter 5 array element:");
for(int i=1;i<6;i++)
{
array[i]=scobj.nextDouble();
}
//Call the outputArray() method to print
//the array elements.
outputArray(array);
//Compute the sum of array elements.
for(int i=0;i<array.length;i++)
{
sum=sum+array[i];
}
System.out.println();
//Print the sum.
System.out.println("The sum of array "
+ "elements is "+sum);
//store first element in the variable min.
min =array[0];
//Iterate through the array.
for(int i=0;i<array.length;i++)
{
//Compare the array elements.
if(array[i]<min)
{
//store the minimum element
//and its index.
min =array[i];
index=i;
}
}
System.out.println();
//Print the minimum element of array with
//its index.
System.out.printf("The index of smallest ");
System.out.printf("elements is "+ index+ " the smallest ");
System.out.printf("value in the array is %.2f\n ",min);
//Call the method to sort the array.
bubbleSort(array);
System.out.println();
//Call the outputArray() method to print
//the array elements.
outputArray(array);
System.out.println();
//Prompt the user to enter the value to be
//found.
System.out.println("Enter value to be found:");
value=scobj.nextDouble();
//Call the method to find the value.
linearSearch( array, value);
}
//Define the method to print the array elemnts.
public static void outputArray( double array[])
{
System.out.println("Array elements are as"
+ " follows:");
//Iterate through the array.
for(int i=0;i<array.length;i++)
{
//Print the array elements.
System.out.printf("%.2f ",array[i]);
}
System.out.println();
}
//Define the method to sort the array.
public static void bubbleSort( double array[])
{
//Define the variables.
int i,j;
double t;
//Use loop to iterate through the array.
for (i = 0; i < array.length; i++)
{
for (j = 0; j < array.length-i-1; j++)
{
//Compare the array elements.
if (array[j] > array[j+1])
{
//Swap the elements.
t=array[j];
array[j]=array[j+1];
array[j+1]=t;
}
}
}
}
//Define the method to search the element.
public static void linearSearch(
double array[], double value)
{
//Define the variables.
boolean flag=false;
int i;
//Iterate through the array.
for (i = 0; i < array.length; i++)
{
//Compare the array elements with the
//value to be found.
if(array[i]==value)
//Set flag true.
flag=true;
}
//Display the message as per the flag.
System.out.println(flag?"Value found":
"Value not found");
}
}