Question

In: Computer Science

Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...

Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the array to a function that finds and displays (on the screen) how many of the array’s elements are zero, how many are negative, and how many are positive. Display the number of zeroes first. Then display the other two counts in descending order. In other words, if there are more positives than negatives, display the number of positives before you display the number of negatives—and vice versa.  Pass the array to another function that finds and displays (on the screen) the average of the squares of the positive numbers and the average of the squares of the negative numbers. Display these averages in descending order. Your outputs should look exactly as shown on the next page (spelling, punctuation, spacing, number format, and so on).

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// numbers.txt

34.5
-56.6
78.7
0
-11.2
34.6
0
33.3

__________________________

// FinalExamProgram2.java

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

public class FinalExamProgram2 {

   public static void main(String[] args) throws FileNotFoundException {
       double nos[] = new double[8];
       int i = 0;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(new File("numbers.txt"));
       while (sc.hasNext()) {
           nos[i] = sc.nextDouble();
           i++;
       }
       sc.close();

       calculate(nos);
       calcAvgSquares(nos);

   }

   private static void calcAvgSquares(double[] nos) {
       int pos=0,neg=0;
       double posSum=0,negSum=0,posAvg=0,negAvg=0;

       for (int i = 0; i < nos.length; i++) {
       if (nos[i] < 0) {
               neg++;
               posSum+=nos[i]*nos[i];
           } else {
               pos++;
               negSum+=nos[i]*nos[i];
           }
       }
      
       posAvg=posSum/pos;
               negAvg=negSum/neg;
               if(posAvg>negAvg)
               {
                   System.out.println("Positive Average :"+posAvg);
                   System.out.println("Negative Average :"+negAvg);
               }
               else
               {
                   System.out.println("Negative Average :"+negAvg);
                   System.out.println("Positive Average :"+posAvg);
               }
              
      
      
   }

   private static void calculate(double[] nos) {
       int zeros = 0, positive = 0, negative = 0;

       for (int i = 0; i < nos.length; i++) {
           if (nos[i] == 0) {
               zeros++;
           } else if (nos[i] < 0) {
               negative++;
           } else {
               positive++;
           }
       }

       // Getting the input entered by the user
       System.out.println("No of Zeros :" + zeros);
       if (positive > negative) {
           System.out.println("No of Positives :" + positive);
           System.out.println("No of Negatives :" + negative);
       } else {
           System.out.println("No of Negatives :" + negative);
           System.out.println("No of Positives :" + positive);
       }

   }

}
__________________________

Output:

No of Zeros :2
No of Positives :4
No of Negatives :2
Negative Average :4844.995
Positive Average :554.8333333333334

_______________Could you plz rate me well.Thank You


Related Solutions

Overview: You will write a program that reads up to 100 numbers from a file. As...
Overview: You will write a program that reads up to 100 numbers from a file. As you read the numbers, insert them into an array in ascending order. Specifics: 1A. Write a function called insertIntoSortedArray . i. It should take three arguments - a. myArray[ ] : sorted array that should be able to hold at most 100 integers. b. numEntries : the number of elements inserted so far. c. newValue : the incoming value to be inserted into the...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
4. Write a program that reads all numbers from a file and determines the highest and...
4. Write a program that reads all numbers from a file and determines the highest and lowest numbers. You must NOT use arrays to solve this problem! Write functions where appropriate. Programming language should be C
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT