Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapp02.histogram;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.math.BigDecimal;
- import java.math.BigInteger;
- import java.math.RoundingMode;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.Collections;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class JavaApp02Histogram {
- public static void main(String[] args) {
- // Read in the data from the text file
- File data = new File("test/data.txt");
- Scanner scanner;
- try {
- scanner = new Scanner(data);
- } catch (FileNotFoundException ex) {
- String workingDir = "Current working directory: " + System.getProperty("user.dir");
- Logger.getLogger("JavaApp01Average").log(Level.SEVERE, workingDir, ex);
- return;
- }
- List<BigDecimal> list = new ArrayList<>();
- BigDecimal min = null;
- BigDecimal max = null;
- // Read in the first number and store this number in min, max and list
- Boolean dataFound = Boolean.FALSE;
- int count = 0;
- while (scanner.hasNext()){
- if (scanner.hasNextBigDecimal()){
- list.add(scanner.nextBigDecimal());
- count++;
- min = list.get(0);
- max = list.get(0);
- dataFound = Boolean.TRUE;
- break;
- } else {
- scanner.next();
- }
- }
- if (!dataFound){
- Logger.getLogger("JavaApp01Average").log(Level.SEVERE, "No valid data in file");
- return;
- }
- // Read in the remaining numbers and store these numbers in list
- // and update min and max
- while (scanner.hasNext()){
- if (scanner.hasNextBigDecimal()){
- BigDecimal temp = scanner.nextBigDecimal();
- list.add(temp);
- count++;
- if (temp.compareTo(min) == -1){
- min = temp;
- } else if (temp.compareTo(max) == 1){
- max = temp;
- }
- } else {
- scanner.next();
- }
- }
- // Find the range and select the number of bins to use in the histogram
- BigInteger range = max.toBigInteger().subtract(min.toBigInteger());
- BigInteger maxWidth = new BigInteger("8");
- BigInteger width = new BigInteger("1");
- if (range.compareTo(maxWidth) == 1){
- width = range.divide(maxWidth);
- if (range.remainder(maxWidth).compareTo(BigInteger.ZERO)==1){
- width = width.add(BigInteger.ONE);
- }
- }
- int binCount = range.divide(width).intValue();
- if (range.remainder(width).compareTo(BigInteger.ZERO)==1){
- binCount++;
- }
- // binCount is the number of bins that will be used
- // Find the class interval for each bin
- List<String> classInterval = new ArrayList<>();
- BigInteger classStart = min.toBigInteger();
- for (int b = 0; b< binCount; b++){
- classInterval.add(String.format("%3s",classStart.toString())+" >= x < "+String.format("%3s",classStart.add(width).toString()));
- classStart = classStart.add(width);
- }
- // Fill each bin
- long[] bin = new long[binCount];
- BigDecimal bw = new BigDecimal(width);
- for (int i = 0; i< list.size(); i++){
- int binNumber = list.get(i).divide(bw,RoundingMode.HALF_UP).intValue()-1;
- bin[binNumber]++;
- }
- // Print out the details of each bin
- int total = 0;
- for (int b = 0; b< binCount; b++){
- System.out.print(classInterval.get(b)+" |");
- for (int d = 0; d<total; d++){
- System.out.print("#");
- }
- for (int v = 0; v< bin[b]; v++){
- System.out.print("*");
- total++;
- }
- for (long s = bin[b]; s<60;s++){
- System.out.print(" ");
- }
- System.out.println("("+bin[b]+")");
- }
- int median = count /2;
- Collections.sort(list);
- System.out.println("There are " + count + " numbers in the list");
- System.out.println("The median is " + list.get(median));
- BigDecimal mode = null;
- int counter = 1;
- for (int i = 0; i < list.size(); i++) {
- int occurence = Collections.frequency(list, list.get(i));
- if (occurence > counter) {
- mode = list.get(i);
- counter = occurence;
- }
- }
- if (mode == null) {
- System.out.println("No mode found");
- }else{
- System.out.println("The mode is " + mode);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement