Advertisement
Guest User

Histogram Help Indentation

a guest
Nov 24th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. package javaapp02.histogram;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.math.BigDecimal;
  6. import java.math.BigInteger;
  7. import java.math.RoundingMode;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11. import java.util.Collections;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. public class JavaApp02Histogram {
  16.  
  17. public static void main(String[] args) {
  18. // Read in the data from the text file
  19. File data = new File("test/data.txt");
  20. Scanner scanner;
  21. try {
  22. scanner = new Scanner(data);
  23. } catch (FileNotFoundException ex) {
  24. String workingDir = "Current working directory: " + System.getProperty("user.dir");
  25. Logger.getLogger("JavaApp01Average").log(Level.SEVERE, workingDir, ex);
  26. return;
  27. }
  28. List<BigDecimal> list = new ArrayList<>();
  29. BigDecimal min = null;
  30. BigDecimal max = null;
  31. // Read in the first number and store this number in min, max and list
  32. Boolean dataFound = Boolean.FALSE;
  33. int count = 0;
  34. while (scanner.hasNext()){
  35. if (scanner.hasNextBigDecimal()){
  36. list.add(scanner.nextBigDecimal());
  37. count++;
  38. min = list.get(0);
  39. max = list.get(0);
  40. dataFound = Boolean.TRUE;
  41. break;
  42. } else {
  43. scanner.next();
  44. }
  45. }
  46. if (!dataFound){
  47. Logger.getLogger("JavaApp01Average").log(Level.SEVERE, "No valid data in file");
  48. return;
  49. }
  50. // Read in the remaining numbers and store these numbers in list
  51. // and update min and max
  52. while (scanner.hasNext()){
  53. if (scanner.hasNextBigDecimal()){
  54. BigDecimal temp = scanner.nextBigDecimal();
  55. list.add(temp);
  56. count++;
  57. if (temp.compareTo(min) == -1){
  58. min = temp;
  59. } else if (temp.compareTo(max) == 1){
  60. max = temp;
  61. }
  62. } else {
  63. scanner.next();
  64. }
  65. }
  66. // Find the range and select the number of bins to use in the histogram
  67. BigInteger range = max.toBigInteger().subtract(min.toBigInteger());
  68. BigInteger maxWidth = new BigInteger("8");
  69. BigInteger width = new BigInteger("1");
  70. if (range.compareTo(maxWidth) == 1){
  71. width = range.divide(maxWidth);
  72. if (range.remainder(maxWidth).compareTo(BigInteger.ZERO)==1){
  73. width = width.add(BigInteger.ONE);
  74. }
  75. }
  76. int binCount = range.divide(width).intValue();
  77. if (range.remainder(width).compareTo(BigInteger.ZERO)==1){
  78. binCount++;
  79. }
  80. // binCount is the number of bins that will be used
  81. // Find the class interval for each bin
  82. List<String> classInterval = new ArrayList<>();
  83. BigInteger classStart = min.toBigInteger();
  84. for (int b = 0; b< binCount; b++){
  85. classInterval.add(String.format("%3s",classStart.toString())+" >= x < "+String.format("%3s",classStart.add(width).toString()));
  86. classStart = classStart.add(width);
  87. }
  88. // Fill each bin
  89. long[] bin = new long[binCount];
  90. BigDecimal bw = new BigDecimal(width);
  91. for (int i = 0; i< list.size(); i++){
  92. int binNumber = list.get(i).divide(bw,RoundingMode.HALF_UP).intValue()-1;
  93. bin[binNumber]++;
  94. }
  95. // Print out the details of each bin
  96. int total = 0;
  97. for (int b = 0; b< binCount; b++){
  98. System.out.print(classInterval.get(b)+" |");
  99. for (int d = 0; d<total; d++){
  100. System.out.print("#");
  101. }
  102. for (int v = 0; v< bin[b]; v++){
  103. System.out.print("*");
  104. total++;
  105. }
  106. for (long s = bin[b]; s<60;s++){
  107. System.out.print(" ");
  108. }
  109. System.out.println("("+bin[b]+")");
  110. }
  111. int median = count /2;
  112. Collections.sort(list);
  113. System.out.println("There are " + count + " numbers in the list");
  114. System.out.println("The median is " + list.get(median));
  115. BigDecimal mode = null;
  116. int counter = 1;
  117. for (int i = 0; i < list.size(); i++) {
  118. int occurence = Collections.frequency(list, list.get(i));
  119. if (occurence > counter) {
  120. mode = list.get(i);
  121. counter = occurence;
  122. }
  123. }
  124. if (mode == null) {
  125. System.out.println("No mode found");
  126. }else{
  127. System.out.println("The mode is " + mode);
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement