Question

In: Computer Science

Modify the HighArray class to implement the method specified below, which reads integer data from a...

  1. Modify the HighArray class to implement the method specified below, which reads integer data from a file to fill the array. The first integer read from the file should indicate the number of integers contained in the file which can be read into the array.



/**
* Read integers into array from named file. First element in
* file is a count of number of integers contained in the file.
*
* @param fileName   name of file containing integer data
*/
void readIntFile(String fileName)

Note: Use the void insert(int value) method defined in the class to add each integer to the array as it is loaded from the file.

Here is a sample code to read data from file

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class FileReaderSample {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner in = new Scanner(System.in);

        System.out.println("Please enter file name");

      String s = in.nextLine();

        Scanner input = new Scanner(new File(s));

        int[] arr = new int[100];

        int i = 0;

        while (input.hasNextInt()) {

            int next = input.nextInt();

            arr[i] = next;

            i++;

        }// end while

        int nElems = i;

// Print the array contents

       System.out.println("Array Contents");

        for (i = 0; i < nElems; i++) {

            System.out.println(i + "\t" + arr[i]);

        }// end for

    }// end main

Solutions

Expert Solution

Short Summary:

Implemented the program as per requirement

Attached source code and sample output

**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**This class reads integer data from a file to fill the array**/
public class HighArray {

   /**
   * Read integers into array from named file. First element in
   * file is a count of number of integers contained in the file.
   *
   * @param fileName name of file containing integer data
   */
   void readIntFile(String fileName)
   {
       Scanner input =null;
       try {
           //Create file with given File name
           input = new Scanner(new File(fileName));
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }


       int i = 0;
       int[] arr = null ;
       int arraySize=0;

       while (input.hasNextInt()) {
           //First element in the array is size of the array, as it contains no of elements
           if(i==0)
           {
               arraySize=input.nextInt();
               arr = new int[arraySize];
           }
           else
           {
               //Load array from second element
               int next = input.nextInt();
               //Start from zero'th index
               arr[i-1] = next;
           }
           i++;

       }// end while

       // Print the array contents

       System.out.println("*******Array Contents******");

       for (i = 0; i < arraySize; i++) {

           System.out.println(i + "\t" + arr[i]);

       }// end for
       input.close();
   }// end Method

   //Main method to test the above method
   public static void main(String[] args)
   {
       //Create object for HighArray
       HighArray obj=new HighArray();
       Scanner in=new Scanner(System.in);
       //Get file name input from user
       System.out.print("Please enter the file name: ");
       obj.readIntFile(in.nextLine());
       in.close();
   }
}

Code Screenshot:

Output:

InputFile.txt

Output:


**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

In C ++, Design and implement a program that reads from the user four integer values...
In C ++, Design and implement a program that reads from the user four integer values between 0 and 100, representing grades. The program then, on separate lines, prints out the entered grades followed by the highest grade, lowest grade, and averages of all four grades. Format the outputs following the sample runs below. Sample run 1: You entered:    95, 80, 100, 70 Highest grade: 100 Lowest grade:   70 Average grade: 86.25
Complete the class code below and include a method named partition which takes an integer array...
Complete the class code below and include a method named partition which takes an integer array and a partition integer as parameters and then partitions the original array into two new arrays where elements of the first array are all less than or equal to the partition integer and the second array are all greater than the partition integer. The method should print the original array, followed by the lower partition and finally the upper partition. Nothing should be returned...
Design and implement an application that reads an integer value and prints the sum of all...
Design and implement an application that reads an integer value and prints the sum of all even integers between 2 and the input value, inclusive. Print an error message if the input value is less than 2 and prompt accordingly so that the user can enter the right number. Your program file will be called YourLastNameExamQ2
Problem: Implement a class named StringParser, along with specified methods, that can read words from a...
Problem: Implement a class named StringParser, along with specified methods, that can read words from a text file, parse those words (such as finding palindromes), and write those words to a new file More specifically: 1. Write the StringParser class so that it has the following methods: a) public static void main(String[] args) -- The main driver of the program. Prompts the user for an input file, an output file, and a choice for what kinds of words to output....
Implement the addSecond method in IntSinglyLinkedList. This method takes an Integer as an argument and adds...
Implement the addSecond method in IntSinglyLinkedList. This method takes an Integer as an argument and adds it as the second element in the list. Here is an example of adding the Integer 7 to a list with two elements. Abstract view: addSecond(7) on the list [12, 100] turns the list into [12, 7, 100] Implement the rotateLeft method in IntSinglyLinkedList. It moves all elements closer to the front of the list by one space, moving the front element to be...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. NOTE: You are not allowed to use...
Write a C/C++ program which reads in a list of process names and integer times from...
Write a C/C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read the list until end of transmission (^d). You should read the list and represent it in a linked list data structure. You should use the alarm system call to schedule a timer...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT