I need assistance with a program I wrote for class. I got it to compile and do what I want, But I was wondering how I can format my methods to to display a decimal with 1-2 decimal places. Would I create a method in my NumberAnalysis class to do it for me? Or would I declare an instance of the DecimalFormat class in my main method? I am new to programming in General and would like some feed back.
package lesson4.skowronek; import java.util.Scanner; //Needed for Scanner Class import java.io.*; //Need for File and IOException import java.text.DecimalFormat; public class Ex8_11 { public static void main(String[] args) throws IOException{ { DecimalFormat decformatter = new DecimalFormat("#0.00"); NumberAnalysis na = new NumberAnalysis("C:\\Users\\*****\\Java Work\\Lesson 4\\src\\lesson4\\skowronek\\Numbers.txt"); System.out.println("Lowest Number: " + na.getLowest()); System.out.println("Highest Number: " + na.getHighest()); System.out.println("Total Number: " + na.getTotal()); System.out.println("Total Average Number: " + na.getAverage()); } } } package lesson4.skowronek; import java.text.DecimalFormat; import java.util.Scanner; import java.io.*; public class NumberAnalysis { double[] arrayNum; int cnt; final int ARRAY_SIZE = 12; NumberAnalysis(String filename) throws IOException { int cnt = 0; arrayNum = new double[ARRAY_SIZE]; File numbers = new File("C:\\Users\\******\\Java Work\\Lesson 4\\src\\lesson4\\skowronek\\Numbers.txt"); Scanner inputFile = new Scanner(numbers); while (inputFile.hasNext() && cnt < arrayNum.length) { arrayNum[cnt] = inputFile.nextDouble(); cnt++; } inputFile.close(); } public double getLowest() { double lowest = arrayNum[0]; for (int cnt = 1; cnt < arrayNum.length; cnt++) { if(arrayNum[cnt] < lowest) lowest = arrayNum[cnt]; } return lowest; } public double getHighest() { double highest = arrayNum[0]; for (int cnt = 1; cnt < arrayNum.length; cnt++) { if (arrayNum[cnt] > highest) highest = arrayNum[cnt]; } return highest; } public double getTotal() { double total = 0.0; //accumulator for (int cnt = 0; cnt < arrayNum.length; cnt++) { total += arrayNum[cnt]; } return total; } public double getAverage() { double average = getTotal() / arrayNum.length; return average; } }
--- Update ---
This is my output:
Lowest Number: 1.09
Highest Number: 82.76
Total Number: 367.89000000000004
Total Average Number: 30.657500000000002
--- Update ---
Think I just solved the answer to my own question, I did it by declaring double variables in my main method and called the methods and instantiated the variables into the methods... Is there a better way to do this?
package lesson4.skowronek; import java.util.Scanner; //Needed for Scanner Class import java.io.*; //Need for File and IOException import java.text.DecimalFormat; public class Ex8_11 { public static void main(String[] args) throws IOException{ { double lowest; double highest; double average; double total; NumberAnalysis na = new NumberAnalysis("C:\\Users\\*****\\Java Work\\Lesson 4\\src\\lesson4\\skowronek\\Numbers.txt"); lowest = na.getLowest(); highest = na.getHighest(); average = na.getAverage(); total = na.getTotal(); DecimalFormat df = new DecimalFormat("#0.00"); System.out.println("Lowest Number: " + df.format(lowest)); System.out.println("Highest Number: " + df.format(highest)); System.out.println("Total Number: " + df.format(total)); System.out.println("Total Average Number: " + df.format(average)); } } }