Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. /*******************************************
  2. * Name: Reginald Thomas
  3. * Assignment: Project Gourd 1
  4. * Due: 9-24-18
  5. * Note: This works better.
  6. *******************************************/
  7.  
  8. import java.util.*;
  9.  
  10. class ProjectGourdOneV2
  11. {
  12.     public static void main(String[] args)
  13.     {  
  14.         createChart();
  15.        
  16.         System.out.println();
  17.     }
  18.    
  19.     // function that creates the chart
  20.     static void createChart()
  21.     {
  22.         // Creates a scanner
  23.         Scanner s = new Scanner(System.in);
  24.         int totalCount = 0;
  25.                
  26.         // array to store count of leading digits
  27.         // array will be of size 10 and store a count of each integer in its given index
  28.         // index value will be incremented according to how often the value appears in the file
  29.         int[] digitCount = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
  30.        
  31.         ArrayList<Integer> nums = new ArrayList<Integer>();
  32.        
  33.         while(s.hasNextInt())
  34.         {
  35.             nums.add(s.nextInt());
  36.         }
  37.        
  38.         for(int num: nums)
  39.         {
  40.             int leadingDigit = getLeadingDigit(num);
  41.                        
  42.             switch(leadingDigit)
  43.             {
  44.                 case 0: digitCount[0]++;
  45.                     break;
  46.                
  47.                 case 1: digitCount[1]++;
  48.                     break;
  49.                
  50.                 case 2: digitCount[2]++;
  51.                     break;
  52.                    
  53.                 case 3: digitCount[3]++;
  54.                     break;
  55.                    
  56.                 case 4: digitCount[4]++;
  57.                     break;
  58.                    
  59.                 case 5: digitCount[5]++;
  60.                     break;
  61.                    
  62.                 case 6: digitCount[6]++;
  63.                     break;
  64.                    
  65.                 case 7: digitCount[7]++;
  66.                     break;
  67.                    
  68.                 case 8: digitCount[8]++;
  69.                     break;
  70.                    
  71.                 case 9: digitCount[9]++;
  72.                     break;                             
  73.             }
  74.             totalCount++;
  75.         }
  76.        
  77.         // converts the total number of digits to a double
  78.         double percentage = totalCount;
  79.        
  80.         for(int i = 0; i <= 37; i++)
  81.             System.out.print("-");
  82.        
  83.         System.out.println("\nLeading Digit\tCount\t\t%");
  84.        
  85.         for(int i = 0; i <= 37; i++)
  86.             System.out.print("-");
  87.        
  88.         // Data Values
  89.         // String.format allows to use 2 decimal places
  90.         System.out.println();
  91.         System.out.println("0\t\t" + digitCount[0] + "\t\t" + String.format("%.2f", (digitCount[0] / percentage) * 100) + "%");
  92.         System.out.println("1\t\t" + digitCount[1] + "\t\t" + String.format("%.2f", (digitCount[1] / percentage) * 100) + "%");
  93.         System.out.println("2\t\t" + digitCount[2] + "\t\t" + String.format("%.2f", (digitCount[2] / percentage) * 100) + "%");
  94.         System.out.println("3\t\t" + digitCount[3] + "\t\t" + String.format("%.2f", (digitCount[3] / percentage) * 100) + "%");
  95.         System.out.println("4\t\t" + digitCount[4] + "\t\t" + String.format("%.2f", (digitCount[4] / percentage) * 100) + "%");
  96.         System.out.println("5\t\t" + digitCount[5] + "\t\t" + String.format("%.2f", (digitCount[5] / percentage) * 100) + "%");
  97.         System.out.println("6\t\t" + digitCount[6] + "\t\t" + String.format("%.2f", (digitCount[6] / percentage) * 100) + "%");
  98.         System.out.println("7\t\t" + digitCount[7] + "\t\t" + String.format("%.2f", (digitCount[7] / percentage) * 100) + "%");
  99.         System.out.println("8\t\t" + digitCount[8] + "\t\t" + String.format("%.2f", (digitCount[8] / percentage) * 100) + "%");
  100.         System.out.println("9\t\t" + digitCount[9] + "\t\t" + String.format("%.2f", (digitCount[9] / percentage) * 100) + "%");
  101.    
  102.        
  103.         for(int i = 0; i <= 37; i++)
  104.             System.out.print("-");
  105.        
  106.         System.out.println();
  107.        
  108.         // END DATA GOES HERE
  109.         // x and y are place holders
  110.         System.out.println("TOTAL\t\t"+ totalCount +"\t\t100.00%");
  111.        
  112.         for(int i = 0; i <= 37; i++)
  113.             System.out.print("=");
  114.     }
  115.    
  116.     // Function will be used to find the leading digit of the number
  117.     static int getLeadingDigit(int testNum)
  118.     {
  119.         while(testNum > 9)
  120.         {
  121.             testNum /= 10;
  122.         }
  123.        
  124.         return testNum;
  125.     }
  126.    
  127.    
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement