Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************
- * Name: Reginald Thomas
- * Assignment: Project Gourd 1
- * Due: 9-24-18
- * Note: This works better.
- *******************************************/
- import java.util.*;
- class ProjectGourdOneV2
- {
- public static void main(String[] args)
- {
- createChart();
- System.out.println();
- }
- // function that creates the chart
- static void createChart()
- {
- // Creates a scanner
- Scanner s = new Scanner(System.in);
- int totalCount = 0;
- // array to store count of leading digits
- // array will be of size 10 and store a count of each integer in its given index
- // index value will be incremented according to how often the value appears in the file
- int[] digitCount = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- ArrayList<Integer> nums = new ArrayList<Integer>();
- while(s.hasNextInt())
- {
- nums.add(s.nextInt());
- }
- for(int num: nums)
- {
- int leadingDigit = getLeadingDigit(num);
- switch(leadingDigit)
- {
- case 0: digitCount[0]++;
- break;
- case 1: digitCount[1]++;
- break;
- case 2: digitCount[2]++;
- break;
- case 3: digitCount[3]++;
- break;
- case 4: digitCount[4]++;
- break;
- case 5: digitCount[5]++;
- break;
- case 6: digitCount[6]++;
- break;
- case 7: digitCount[7]++;
- break;
- case 8: digitCount[8]++;
- break;
- case 9: digitCount[9]++;
- break;
- }
- totalCount++;
- }
- // converts the total number of digits to a double
- double percentage = totalCount;
- for(int i = 0; i <= 37; i++)
- System.out.print("-");
- System.out.println("\nLeading Digit\tCount\t\t%");
- for(int i = 0; i <= 37; i++)
- System.out.print("-");
- // Data Values
- // String.format allows to use 2 decimal places
- System.out.println();
- System.out.println("0\t\t" + digitCount[0] + "\t\t" + String.format("%.2f", (digitCount[0] / percentage) * 100) + "%");
- System.out.println("1\t\t" + digitCount[1] + "\t\t" + String.format("%.2f", (digitCount[1] / percentage) * 100) + "%");
- System.out.println("2\t\t" + digitCount[2] + "\t\t" + String.format("%.2f", (digitCount[2] / percentage) * 100) + "%");
- System.out.println("3\t\t" + digitCount[3] + "\t\t" + String.format("%.2f", (digitCount[3] / percentage) * 100) + "%");
- System.out.println("4\t\t" + digitCount[4] + "\t\t" + String.format("%.2f", (digitCount[4] / percentage) * 100) + "%");
- System.out.println("5\t\t" + digitCount[5] + "\t\t" + String.format("%.2f", (digitCount[5] / percentage) * 100) + "%");
- System.out.println("6\t\t" + digitCount[6] + "\t\t" + String.format("%.2f", (digitCount[6] / percentage) * 100) + "%");
- System.out.println("7\t\t" + digitCount[7] + "\t\t" + String.format("%.2f", (digitCount[7] / percentage) * 100) + "%");
- System.out.println("8\t\t" + digitCount[8] + "\t\t" + String.format("%.2f", (digitCount[8] / percentage) * 100) + "%");
- System.out.println("9\t\t" + digitCount[9] + "\t\t" + String.format("%.2f", (digitCount[9] / percentage) * 100) + "%");
- for(int i = 0; i <= 37; i++)
- System.out.print("-");
- System.out.println();
- // END DATA GOES HERE
- // x and y are place holders
- System.out.println("TOTAL\t\t"+ totalCount +"\t\t100.00%");
- for(int i = 0; i <= 37; i++)
- System.out.print("=");
- }
- // Function will be used to find the leading digit of the number
- static int getLeadingDigit(int testNum)
- {
- while(testNum > 9)
- {
- testNum /= 10;
- }
- return testNum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement