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

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....
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?
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...
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT