Question

In: Computer Science

Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...

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

Solutions

Expert Solution

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");

          }

     }

    


Related Solutions

import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); SimpleDate d1 = new SimpleDate(); SimpleDate d2 = new SimpleDate(stdin.nextInt(), stdin.nextInt(), stdin.nextInt()); System.out.println(d1); System.out.println(d2); System.out.println(d1.before(d2)); System.out.println(d2.before(d1)); } } Implement SimpleDate class in chapter6.date package with the following attributes: day, (int type,  private) month, (int type,  private) year (int type,  private) The class should have the following methods: a constructor with three parameters: year, month, and day a constructor with no parameters which initialize the...
what i have: import java.util.Scanner; public class examples1 { public static void main(String[] args) { Square...
what i have: import java.util.Scanner; public class examples1 { public static void main(String[] args) { Square shade = new Square(getside()); System.out.println("Your chooses are:"); System.out.println("\nCircle"); System.out.println("Triangle"); System.out.println("Rectangle"); System.out.println("Square"); boolean moreShapes = true; boolean userChoice = true; while(moreShapes) { Scanner shapes = new Scanner (System.in); System.out.println("\nWhat shape do you want:"); String yourShape = shapes.nextLine(); if (userChoice = shade != null) { shade.getSide(); } System.out.println("\nWhat are two size paramters of the shape you choose:"); String yourParameter = shapes.nextLine(); System.out.println("\nYour shape is: " +...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT