This is a java assignment Please help me
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.
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") );
}
}
import java.io.*;
import java.util.Scanner;
public class Main {
public double fileAverage( String filename ){
Scanner sc = null;
try {
sc = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
float f;
int count = 0;
double sum = 0;
while(sc.hasNextDouble()){
f = sc.nextFloat();
sum += f;
count += 1;
}
return sum/count;
}
public static void main( String[] args ){
Main obj = new Main();
System.out.println( obj.fileAverage( "numbers.txt"));
}
}

1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

2.5

This is a java assignment Please help me Write the body of the fileAverage() method. Have...
Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...
Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...
Java Method Resolution : Consider the following example: What is printed on the console if Main is executed, and why? How the codes could be improved? public class Main { public static void main(String[] args) throws Exception { A obj = null; obj.foo(); } } public class A { public void foo() { System.out.println(“foo in A”); } }
Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. Code: import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; /** * * @author David */ public class ReadingFiles { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here...
Is there any way that I can call method in other class without constructor in java? public class Chest { private String contents; public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } I need to set content and print out in the main method, but it keep gives me a error message when I try Chest.setContents("Gold"); on main class. I suppose...
java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main { { return true; } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main { { String ans = name.toUpperCase();...
I have one method {search()} that search for a particular file in a directory that the filename start by "B" and it will return the files. I have 2 file that start by B [Bcc.txt, Ba.txt]. But in main method I want to count a word "light" how many times it appeares in the each files [Bcc.txt, Ba.txt]. However, I really don't know how to do it. I need help on that. public class CountWord { public static void main(String[]...
Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...
Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me. Need help with this to run the application. /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * * @author Java Foundations * @version 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with...
in
java
Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags when running programs. On command line, for example, we may do “java MyProgram -a -v". Depending on options, the program may do things differently. For example, "a" may do all things while "-v" display messages verbosely. You can provide options in Eclipse instead of command line: "Run ... Run Configurations ... Arguments". Create a Java class (Main.java)....