6 marks] Write a full Java program that: reads a number of 1 dimensional arrays
ID: 3712045 • Letter: 6
Question
6 marks] Write a full Java program that: reads a number of 1 dimensional arrays of real numbers from an input file, calculates the sum of each array, and outputs the original array and its sum to an output file. ? Your program should call the methods that are provided on the next page In the input file, each array of numbers is on a single line; The first number on each line indicates how many numbers are in that array. The number of lines in the file is not known in advance. A sample input file is: 5 2.2 1.76 3.5 7.23 -4.0 3 10.5 2.3 -16.0 4 5.432-1 45.23 6.1 Grades are assigned for - Correct functionality - Minimal redundancy Adherence to coding conventions java. util t joua. io. elass ttest static void main lStrvall ers) Scamer le nes Scanmer (nes ) txt") e got Vector lesew pst kev Filel el double Vedo Suma)Explanation / Answer
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner; public class VectorTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter input file name: "); String filename = in.nextLine(); try { Scanner fin = new Scanner(new File(filename)); double[] arr; double sum; PrintStream fOut = new PrintStream(new File("Output.txt")); while (fin.hasNextInt()) { arr = getVector(fin); sum = vectorSum(arr); vectorOut(arr, fOut); fOut.println(". And Sum is " + sum); } fOut.flush(); fOut.close(); fin.close(); } catch (FileNotFoundException e) { System.out.println(filename + " does not exists"); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.