Question

In: Computer Science

First create the text file given below. Then complete the main that is given. There are...

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

Solutions

Expert Solution


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


Related Solutions

Allow the main process to generate a text file containing the text of this assignment. The...
Allow the main process to generate a text file containing the text of this assignment. The main process will then create two other processes and pass to them the name of the file and two codes (code1 and code2) using a pipe() system call. The codes are two different alphabetic letters such as “a” and “k”. The child processes should search the file for the character in the interval received, compute their frequencies and return them through a separate pipe...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name &quot;Test&quot; and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named &quot;361&quot; 5-Copy file &quot;Test&quot; to...
Given some data in a text file, the task is to scramble the text and output...
Given some data in a text file, the task is to scramble the text and output in a separate text file. So, we need to write a Python program that reads a text file, scrambles the words in the file and writes the output to a new text file. Rules to be followed: Words less than or equal to 3 characters need not be scrambled. Don’t scramble first and last char, so Scrambling can become Srbmnacilg or Srbmnailcg or Snmbracilg,...
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means...
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means you need to scramble the words of a file to become secure. Decryption means you need to apply some rules to an encrypted file in order to find the original file with its original content. An example of encryption is to replace each letter in a file with its consecutive letter in the alphabet. Therefore, ‘a’ is replaced by ‘b’, ‘b’ is replaced by...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
The ages of 10 brides at their first marriage are given below. Complete parts? (a) and?...
The ages of 10 brides at their first marriage are given below. Complete parts? (a) and? (b) below. 31.5 31.5?? 41.7 41.7?? 30.9 30.9?? 33.1 33.1?? 43.3 43.3?? 26.9 26.9?? 26.3 26.3?? 23.6 23.6?? 34.3 34.3?? 31.6 31.6?? ?(a) Find the range of the data set. Range equals = nothing ?(Round to the nearest tenth as? needed.) ?(b) Change 43.3 43.3 to 50.5 50.5 and find the range of the new data set. Range equals = nothing ?(Round to the...
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt)...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT