Question

In: Computer Science

JAVA Lab 0.2 — Calculating Averages (Averages) The file numbers.text consists of sequences of numbers, each...

JAVA

Lab 0.2 — Calculating Averages (Averages)

The file numbers.text consists of sequences of numbers, each sequence preceded by a header value and then followed by that many integers/ (For example the first line in the example below contains a header value of 3 followed by the three values 1, 2, and 3. Read in the sequences and print their averages. When all sequences have been read in, print out the number of sequences processed.

The header and subsequent values are placed on the same line for readability, but could just have easily been spread across two or more line, i.e., you don't have to process them using nextLine; simply read them in using nextInt.)

The name of your class should be Averages.

Sample Test Run

For example if the file numbers.text contains:

3 1 2 3
5 12 14 6 4 0
10 1 2 3 4 5 6 7 8 9 10
1 17
2 90 80

the program should produce the following output:

The average of the 3 integers 1 2 3 is 2.0
The average of the 5 integers 12 14 6 4 0 is 7.2
The average of the 10 integers 1 2 3 4 5 6 7 8 9 10 is 5.5
The average of the 1 integers 17 is 17.0
The average of the 2 integers 90 80 is 85.0
5 sets of numbers processed

Solutions

Expert Solution

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

public class Averages {

    public static void main(String[] args) {
        File file = new File("numbers.text");
        try {
            Scanner fin = new Scanner(file);
            int numSets = 0;
            while (fin.hasNextInt()) {
                int n = fin.nextInt(), num, count = 0;
                double total = 0;
                String s = "";
                for (int i = 0; i < n; i++) {
                    num = fin.nextInt();
                    total += num;
                    ++count;
                    s += num + " ";
                }
                System.out.println("The average of the " + count + " integers " + s + "is " + (total/count));
                ++numSets;
            }
            System.out.println(numSets + " sets of numbers processed");
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " is not found!");
        }
    }
}

Related Solutions

Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience using classes and arrays of classes. Use the following as the beginning of a student abstract data type. public class Student { private String fName ; private String lName ; private double[] grades; } This class should be in the package com.csc241. Write a program that prompts a user for a total number of students and dynamically allocates memory for just that number of...
IN JAVA Lab 10 This program reads times of runners in a race from a file...
IN JAVA Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you created and copy the Main class below into the file. There are 7 parts...
In Java Please!!! 5.24 LAB: Leap year A year in the modern Gregorian Calendar consists of...
In Java Please!!! 5.24 LAB: Leap year A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4 2)...
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 Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Java Question: COSC 2436 Lab 4 (Submit your Word file with answers) If you push the...
Java Question: COSC 2436 Lab 4 (Submit your Word file with answers) If you push the objects x, y, and z onto an initially empty stack, in what order will three pop operations remove them from the stack? What pseudocode statements create a stack of the three strings "Jill", "Jane", and "Joe", in that order with "Jill" at the top? Suppose that s and t are empty stacks and a, b, c, and d are objects. What do the stacks...
"You will need to compile each Java source file (provided below) separately, in its own file...
"You will need to compile each Java source file (provided below) separately, in its own file since they are public classes, to create a .class file for each, ideally keeping them all in the same directory." My class is asking me to do this and i am not understanding how to do it. I use JGRASP for all of my coding and i can't find a tutorial on how to do it. I just need a step by step tutorial...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT