java programming by just using import javautilScanner Write

 java programming by just using import java.util.Scanner   Write a program to implement array with method. In this lab, you will implement array with methods. In this lab, you will read the array and count the numbers of data  you read for the array in main method, then you perform a sequential search in a separate method and display the result as shown in sample output using  a separate method.  The methods that you will be writing are:  a. method that performs a sequential search. The method will take     an array, dataSize and key (value to be searched) as argument and returns int.  b. method to print the array. This method takes an array and number of data read    as argument and returns nothing.     c. and a main method.  Make sure that your read the values from input file as String  not double although the input data looks like int/double.  To convert a String value to an integer value, we need to use parse() method.  e.g. String line; double value = Double.parseDouble(line); // converts String line to double value  This is just a guideline that will help you to do the lab. You can approch this lab on your own way, if you want.  Make sure to implement defensive programming. Also, follow good programming practice.   1) Create an input file Lab9_in.txt with following values.    Here the sentinel value is #### so that when you read the values from file    make sure that you read them as line and then convert them to double value.    The last value is the search key which needs to be passed to perform sequential search.    Make sure not to have any blank lines in the in file.      If you do it will change the results.  3.4 4.5 6.7 8.9 9.0 #### 4.5  2) Complete the following program   // (Name) // (Section)    public class Lab9 {    public static int seqSearch(double[] dArr, int dataSize, double key)     {      // Search nameArr[] for the given key value.      // If there\'s a match in the array, return the index of the matching      // value, otherwise return -1.              // [Add code here]    }     public static void main(String [] args)    {       // Declare and instantiate a final named int constant SIZE_ARR       //    to 10.        // [Add code here]        // Declare and instantiate an array of type double       //    containing SIZE_ARR elements.        // [Add code here]        final String SENT = \"####\";       String line;       int count;       double dValue;       Scanner scan = new Scanner(System.in);       count = 0;        if(!scan.hasNextLine())       {          System.out.println(\"ERROR: No original value to read in.\");          System.out.println(\"Quitting Program.\");          System.exit(0);       }       line = scan.nextLine().trim();         // Write a SENTINEL loop to continue until the SENTINEL value       // is read in        // [Add code here]        {            // Convert the line (a String) to a double value.          dValue = Double.parseDouble(line);            // Store the converted value read in into the correct location           // of the array.          // Verify that there is enough room for the value in           // the array.  If not enough room print out an appropriate          // error message and quit the program.           // [Add code here.]           count++;           // Read in the next value          if(!scan.hasNextLine())          {             System.out.println(\"ERROR: No original value to read in.\");             System.out.println(\"Quitting Program.\");             System.exit(0);          }                  //trim() method is used to trim whitespace from the begining and end of a string          line = scan.nextLine().trim();       }        // Call the printData method with the array.        // [Add code here]             // Read in a last number to search through the array for.       // Remember to use all defensive programming.         // [Add code here]         // Call the seqSearch function in this class and store in iPos            // and print the message as shown in sample output if key value is found.       // Also print the message as shown in sample output even the key is not found.        // [Add code here]               } // End Main Method     public static void printData(double[] arr, int n)    {       // Loop through the used portion of the array and        // print out the information as shown in the sample        // output.        // [Add code here]     } // End of printData method  } // End public class Lab9   3) Run the program with the Lab9_in.txt file.     4) The output for the Lab9_found_output.txt should be:  Sample output:  dArray[0] : 3.4 dArray[1] : 4.5 dArray[2] : 6.7 dArray[3] : 8.9 dArray[4] : 9.0  Number 4.5 is found in the Array at index location 1.  5) Now change the key value in your input file to 3.6 in your input file and run the program. Save your output to file Lab9_notfound_output.txt. Your output file should look like:  dArray[0] : 3.4 dArray[1] : 4.5 dArray[2] : 6.7 dArray[3] : 8.9 dArray[4] : 9.0  Number 3.6 is not found in the Array.   5)Turn in a copy of your Lab9.java, Lab9.txt, and both output files.  

Solution

import java.util.*;
import java.io.*;
public class Lab9
{
   public static int seqSearch(double[] dArr, int dataSize, double key)
   {
     // Search nameArr[] for the given key value.
     // If there\'s a match in the array, return the index of the matching value, otherwise return -1.
     for(int i = 0; i < dataSize; i++)
     {
        if(dArr[i] == key)
         return i;
     }
     return -1;
   }

   public static void main(String [] args) throws FileNotFoundException
   {
      // Declare and instantiate a final named int constant SIZE_ARR to 10.
      final int SIZE_ARR = 10;
      // Declare and instantiate an array of type double containing SIZE_ARR elements.
      double[] dArr = new double[SIZE_ARR];
      final String SENT = \"####\";
      String line;
      int count;
      double dValue;
      File file = new File(\"Lab9_in.txt\");
      Scanner scan = new Scanner(file);
      count = 0;
    
      if(!scan.hasNextLine())
      {
         System.out.println(\"ERROR: No original value to read in.\");
         System.out.println(\"Quitting Program.\");
         System.exit(0);
      }
      line = scan.nextLine().trim();
      // Write a SENTINEL loop to continue until the SENTINEL value is read in
      while(!line.equals(SENT))
      {
         // Convert the line (a String) to a double value.
         dValue = Double.parseDouble(line);
         // Store the converted value read in into the correct location of the array.
         // Verify that there is enough room for the value in the array. If not enough room print out an appropriate error message and quit the program.
         if(count != SIZE_ARR)
            dArr[count] = dValue;
         count++;
         // Read in the next value
         if(!scan.hasNextLine())
         {
            System.out.println(\"ERROR: No original value to read in.\");
            System.out.println(\"Quitting Program.\");
            System.exit(0);
         }
         //trim() method is used to trim whitespace from the begining and end of a string
         line = scan.nextLine().trim();
      }
      // Call the printData method with the array.
      printData(dArr,SIZE_ARR);
      // Read in a last number to search through the array for.Remember to use all defensive programming.
      double key = Double.parseDouble(scan.nextLine().trim());
      // Call the seqSearch function in this class and store in iPos and print the message as shown in sample output if key value is found.
      // Also print the message as shown in sample output even the key is not found.
      int iPos = seqSearch(dArr,SIZE_ARR,key);
      if(iPos != -1)
         System.out.println(\"Number \" + key + \" is found in the Array at index location \" + iPos + \".\");
      else
         System.out.println(\"Number \" + key + \" is not found in the Array.\");
   } // End Main Method
   public static void printData(double[] arr, int n)
   {
      // Loop through the used portion of the array and print out the information as shown in the sample output.
      for(int i = 0; i < n; i++)
         System.out.println(\"dArray[\" + i + \"] : \" + arr[i]);

   } // End of printData method

} // End public class Lab9

 java programming by just using import java.util.Scanner Write a program to implement array with method. In this lab, you will implement array with methods. In
 java programming by just using import java.util.Scanner Write a program to implement array with method. In this lab, you will implement array with methods. In

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site