Lab #9 CS1113 Fall 2016 Write a program to implement array with method. In this
ID: 3776917 • Letter: L
Question
Lab #9 CS1113 Fall 2016 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.
Explanation / Answer
package com.prt.test;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab9 {
public static int seqSearch(double[] dArr, int dataSize, double key) {
// return 1;
// 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]
for (int i = 0; i < dArr.length; i++) {
if (key == dArr[i]) {
// System.out.println("the index matching value is "+ i);
return 0;
}
}
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 = 4;
double[] darray = new double[SIZE_ARR];
// [Add code here]
// Declare and instantiate an array of type double
// containing SIZE_ARR elements.
// [Add code here]
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); }
*/
/*
* File file=new File("D:\lab9.txt"); BufferedReader br = new
* BufferedReader(new FileReader(file));
*/
// Write a SENTINEL loop to continue until the SENTINEL value
// is read in
// [Add code here]
while (count < SIZE_ARR)
{
System.out.println("enter the double in the string form");
line = scan.nextLine().trim();
// 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.
darray[count] = dValue;
// 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.]
// if not enough room the loop fails it won't read again we don't
// need to do check
count++;
}
printData(darray, SIZE_ARR);
// 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.
System.out.println("enter the value for searching");
double key = scan.nextDouble();
int dataSize = SIZE_ARR;
int index = seqSearch(darray, dataSize, key);
int location = 0;
if (index == 0) {
for (int i = 0; i < darray.length; i++) {
if (key == darray[i]) {
location = i;
}
}
System.out.println("the number" + key
+ "is found at the index value is" + location);
} else
System.out.println("the 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.
// [Add code here]
for (int i = 0; i < n; i++) {
System.out.println("darray[" + i + "] value is" + arr[i]);
}
} // End of printData method
} // End public class Lab9
output
enter the double in the string form
3.2
enter the double in the string form
6.5
enter the double in the string form
2.3
enter the double in the string form
4.1
darray[0] value is3.2
darray[1] value is6.5
darray[2] value is2.3
darray[3] value is4.1
enter the value for searching
85.3
the number85.3is not found in the array
enter the double in the string form
2.5
enter the double in the string form
6.3
enter the double in the string form
4.2
enter the double in the string form
3.6
darray[0] value is2.5
darray[1] value is6.3
darray[2] value is4.2
darray[3] value is3.6
enter the value for searching
3.6
the number3.6is found at the index value is3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.