In: Computer Science
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given
Create this text file: data1.txt
-59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42
` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there are 3 methods that you need to uncomment and implement. The descriptions of the methods are where the methods should be written. I suggest you keep the comments in the code, so you can read the directions as you comment.
main method - you are only adding 1 statement to the main method and then uncommenting each method call as you implement it. DO NOT DO ALL METHODS AT ONCE - get one method at a time to work and test it to make sure it is working correctly.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Main
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner (System.in);
// declare an array called array that has a size of 50
// this is the ONLY statement you need to add to main;
int numElements;
String fileName;
int lowerBound;
int higherBound;
System.out.print("Enter the file name: ");
fileName = keyboard.next();
// numElements = fillArray(fileName, array);
System.out.println("The file has " + numElements + "
values");
// printArray(array, numElements);
System.out.print("Enter the lower bound number: ");
lowerBound = keyboard.nextInt();
System.out.print("Enter the higher bound number: ");
higherBound = keyboard.nextInt();
// printElementsInBoundary(array, numElements, lowerBound, higherBound);
}
// Method: fillArray
// This method has a filename and an array of integers passed into
it.
// It then opens the file with the name passed in and tests it to
make sure
// it opened correct. (See output to see what happens if the file
isn't there)
// The method then puts all of the integers in the file into the
array, It
// then closes the file and returns the number of integers that
were read into
// the array. You can assume that there will be room in the
array.
// Method: printElementsInBoundary
// This method has an array and the number of elements in the array
passed into it.
// It also has a lower bound and an upper bound passed into
it,
// The method then prints all of the numbers in the array that are
between the
// lower bound and upper bound - including both the lower and upper
bound. The numbers
// are printed with one space between each number.
// After printing them, it prints a new line.
// Method: printArray
// This method prints the numbers out in columns of width 5
and
// prints 10 per line. After printing all of the numbers, it
// prints a new line.
}
Sample Output
Enter the file name: data1.txt The file has 18 values -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 Enter the lower bound number: -20 Enter the higher bound number: 60 34 0 24 58 5 45 -19 -5 42
Sample Output - if file doesn't exist
Enter the file name: datafile.txt datafile.txt did not open correctly
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Main
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner (System.in);
// declare an array called array that has a size of 50
// this is the ONLY statement you need to add to main;
int array[]=new int[50];
int numElements;
String fileName;
int lowerBound;
int higherBound;
System.out.print("Enter the file name: ");
fileName = keyboard.next();
numElements = fillArray(fileName, array);
System.out.println("The file has " + numElements + " values");
printArray(array, numElements);
System.out.print("Enter the lower bound number: ");
lowerBound = keyboard.nextInt();
System.out.print("Enter the higher bound number: ");
higherBound = keyboard.nextInt();
printElementsInBoundary(array, numElements, lowerBound, higherBound);
}
// Method: fillArray
// This method has a filename and an array of integers passed into it.
// It then opens the file with the name passed in and tests it to make sure
// it opened correct. (See output to see what happens if the file isn't there)
// The method then puts all of the integers in the file into the array, It
// then closes the file and returns the number of integers that were read into
// the array. You can assume that there will be room in the array.
public static int fillArray(String fileName,int [] array)
{
int numElems = 0;
Scanner sc=null;
File f = new File(fileName);
//opening the file
try {
sc = new Scanner(f);
} catch (FileNotFoundException e) {
System.out.println(fileName+" did not open correctly");
return -1;
}
//reading numbers from file to array
while(sc.hasNextInt()) {
array[numElems++]=sc.nextInt();
}
// Close the file
sc.close();
return numElems;
// return the number of elements in the array
}
// Method: printElementsInBoundary
// This method has an array and the number of elements in the array passed into it.
// It also has a lower bound and an upper bound passed into it,
// The method then prints all of the numbers in the array that are between the
// lower bound and upper bound - including both the lower and upper bound. The numbers
// are printed with one space between each number.
// After printing them, it prints a new line.
private static void printElementsInBoundary(int[] aArray, int aNumElements, int aLowerBound, int aHigherBound) {
for(int i=0;i<aNumElements;i++) {
if(aArray[i]>=aLowerBound && aArray[i]<=aHigherBound)
System.out.print(aArray[i]+" ");
}
}
// Method: printArray
// This method prints the numbers out in columns of width 5 and
// prints 10 per line. After printing all of the numbers, it
// prints a new line.
private static void printArray(int[] aArray, int aNumElements) {
for(int i=0;i<aNumElements;i++) {
if(i%10==0 && i!=0)
System.out.println();
System.out.printf("%5d",aArray[i]);
}
System.out.println();
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot