Starting from:

$25

CS212- Object-Oriented Programming in Java: Lab 6 Solved

Aim: File input and command line arguments.

Copy your Lab5Program1.java file to Lab6Progam1.java. Since the file name is different, change the name of the class to Lab6Program1.
        This program will read integer values from a file, and compute the sum and the average. Change the String array to an integer array in Lab6Program1. Rewrite the inputFromFile method so that it reads from the file given in args[0], converts the string value on each line to an integer and stores it in the next cell of the array.

                            If the name of the input file is lab6input.txt, you would run the program using:

  c:>java Lab5Program1 lab6input.txt
                    The file lab6input.txt is available on Blackboard.

Write a method with the following signature:
            public static int sum (int[] myArray, int myArraySize);
        This method should return the sum of all the integers in the partially-filled array that was created by inputFromFile. Use a JOptionPane.showMessageDialog to display the sum.

Write a method with the following signature:
             public static int average (int[] myArray, int myArraySize);
        This method should return the average of all the integers in the partially-filled array that was created by inputFromFile. Note that this method can call the method sum. Use a JOptionPane.showMessageDialog to display the sum.

        The program from Lab 4 initialized the array of words by an assignment statement. Modify the program so that it will read from a file, and the name of the file is given as a command line argument. For example, if the name of the input file is lab5input.txt, you would run the program using:

  c:>java Lab5Program1 lab6input.txt             
Run your program, and show your lab instructor.

More products