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

Write program in Java import java.util.Scanner; public class Lab7Program { publi

ID: 3603087 • Letter: W

Question

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.
}

Explanation / Answer

here's the required code.

I have commented the code for your understanding :

------------------------------------------------------------------------------------------------------------------------

public static void main(String args[]) {
        Scanner s = new Scanner(System.in); // scanner to read data
        double arr[] = new double[10]; // array declaration
        outputArray(arr,10); // printing array
        for ( int i =0;i<10;i++)   // initializing with random numbers
            arr[i] = Math.random()*5.0 + 1.0;
        outputArray(arr,10); // prinitng the array
        arr[(arr.length - 1)] = 5.5;   // changing the value using length() funtion
        outputArray(arr,10); // printing the array
        for( int i =1 ;i<6;i++)    // reading 2-6 values from user
            arr[i] = s.nextDouble();
        outputArray(arr,10); // printing the array
        double sum = 0.0;
        for( int i =0 ;i<10;i++) // loop to calculate sum
            sum = sum + arr[i];
          
        System.out.println("sum : " +sum); // print sum
        double min = 0;
        int i;
        for ( i =0;i<10;i++){ // looping to find min value
            if(min > arr[i]){
                min = arr[i];
                break;
            }
        }
        System.out.println("Index : "+ i + " Min value : " + min ); // printing min and index too
        bubbleSort(arr,10); // bubble sort
        linearSearch(arr,10,5.5); // linear search
        }
        public static void outputArray( double a[], int n){
            // looping over the array to output the values of array
            for ( int i =0 ;i<n; i++ )
                System.out.print(a[i] + " ");
            System.out.println();
        }
        public static void bubbleSort(double a[], int n){
            double temp = 0.0;
            // bubble sort algorithm
            for(int i =0; i< n;i++){
                for(int j = 0;j<i;j++){
                    if(a[i]>a[j]){
                        temp = a[i];
                        a[i]=a[j];
                        a[j] = temp;
                    }
                }
            }
            outputArray(a,n);
        }
        public static void linearSearch(double a[], int n, double val){
             int flag = 0;
            for( int i =0;i<n;i++){
                if( a[i] == val ) // if found flag == 1
                flag = 1;
            }
            if(flag == 1) // print found
            System.out.println("Found!");
            else           // print not found
            System.out.println("Not found!");
        }

------------------------------------------------------------------------------------------------------------------------------------

/* hope this helps */

/* if any queries please comment */

/* thank you */

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