Question

In: Computer Science

This is a java assignment Write the body of the fileAverage() method. Have it open the...

This is a java assignment Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place). For the testing system to work, don't change the class name nor the method name. Furthermore, you cannot add "throws IOException" to the fileAverage() header. Additionally, the file will have different contents during testing.

public class Main { public double fileAverage( String filename ){ } public static void main( String[] args ){ Main obj = new Main(); System.out.println( obj.fileAverage( "numbers.txt") ); } }

Solutions

Expert Solution

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

public class Main {
   public double fileAverage(String filename) {
       double avg = 0;
       Scanner sc = null;
       try {
           //opening the file with given name
           sc = new Scanner(new File(filename));
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       double sum = 0, count = 0;
       //reading numbers from file
       while (sc.hasNext()) {
           //adding to sum
           sum += sc.nextDouble();
           //increasing the count
           count++;
       }
       //finding the average
       avg = sum / count;
       return avg;
   }

   public static void main(String[] args) {
       Main obj = new Main();
       System.out.println(obj.fileAverage("numbers.txt"));
   }
}

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

Please write in Java and have two methods: the main method and the reverse word Write...
Please write in Java and have two methods: the main method and the reverse word Write a method that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input: The quick brown fox jumps over the lazy dog you should get the following output: dog lazy the over jumps fox brown quick The Then create a main method to prompt the user to enter a line of words...
How do I write this method correctly? What Ihave is a Java project assignment where I'm...
How do I write this method correctly? What Ihave is a Java project assignment where I'm supposed to create a Roshambo game. One of the classes that the project is supposed to have is a class called "RoshamboApp that let the user play 1 of 2 AI opponents, a "Bart" class, and a "Lisa" class. The instructions say "Create a class names RoshamboApp that allows player1 to play Bart or Lisa as shown in the console output. Rock should beat...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
java method for dequeue write the “dequeue” method for a queue of type double. If the...
java method for dequeue write the “dequeue” method for a queue of type double. If the queue is empty return 0.0. Make sure to change the links properly ensure that the data structure remains a queue. use the code provided below public class Q04 { public class ListNode//Public for testing purposes { public double data; public ListNode link; public ListNode(double aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//Public for testing purposes public ListNode...
Question 6 20 pts Compose the body of the following Java method: /* * Accepts an...
Question 6 20 pts Compose the body of the following Java method: /* * Accepts an array of Character consisting of "{[()]}" characters and * returns true if all pairs of parentheses match. Returns false if * parenthesis expression is malformed. Braces are allowed to occur * inside brackets, and brackets are allowed to occur inside parentheses. */ public boolean parenthesesMatch(Character [] input) {
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT