Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: Michael Zhan
- //Date: 20180110
- //Lab: Occurences
- import java.util.Scanner;
- import static java.lang.System.*;
- public class Occurrences
- {
- private int[] list;
- public Occurrences(String line)
- {
- Scanner counter = new Scanner(line);
- int count = 0;
- int trash = 0;
- while (counter.hasNextInt())
- {
- trash=counter.nextInt();
- count++;
- }
- list = new int[count];
- Scanner chopper = new Scanner(line);
- int i =0;
- while (chopper.hasNextInt())
- {
- list[i]=chopper.nextInt();
- i++;
- }
- }
- public void setList(String line)
- {
- Scanner counter = new Scanner(line);
- int count = 0;
- int trash = 0;
- while (counter.hasNextInt())
- {
- trash=counter.nextInt();
- count++;
- }
- list = new int[count];
- Scanner chopper = new Scanner(line);
- int i =0;
- while (chopper.hasNextInt())
- {
- list[i]=chopper.nextInt();
- i++;
- }
- }
- public String toString()
- {
- String output="";
- int zero=0;
- int one=0;
- int two=0;
- int three=0;
- int four=0;
- int five=0;
- int six=0;
- int seven=0;
- int eight=0;
- int nine=0;
- for (int x: list)
- {
- if (x==0)
- zero++;
- if (x==1)
- one++;
- if (x==2)
- two++;
- if (x==3)
- three++;
- if (x==4)
- four++;
- if (x==5)
- five++;
- if (x==6)
- six++;
- if (x==7)
- seven++;
- if (x==8)
- eight++;
- if (x==9)
- nine++;
- }
- output += "Number\t\t\tOccurrences";
- output+= "\n\n0\t\t-\t\t"+zero+" ";
- for (int i=0; i<zero; i++)
- output += ("*");
- output+= "\n\n1\t\t-\t\t"+one+" ";
- for (int i=0; i<one; i++)
- output += ("*");
- output+= "\n\n2\t\t-\t\t"+two+" ";
- for (int i=0; i<two; i++)
- output += ("*");
- output+= "\n\n3\t\t-\t\t"+three+" ";
- for (int i=0; i<three; i++)
- output += ("*");
- output+= "\n\n4\t\t-\t\t"+four+" ";
- for (int i=0; i<four; i++)
- output += ("*");
- output+= "\n\n5\t\t-\t\t"+five+" ";
- for (int i=0; i<five; i++)
- output += ("*");
- output+= "\n\n6\t\t-\t\t"+six+" ";
- for (int i=0; i<six; i++)
- output += ("*");
- output+= "\n\n7\t\t-\t\t"+seven+" ";
- for (int i=0; i<seven; i++)
- output += ("*");
- output+= "\n\n8\t\t-\t\t"+eight+" ";
- for (int i=0; i<eight; i++)
- output += ("*");
- output+= "\n\n9\t\t-\t\t"+nine+" ";
- for (int i=0; i<nine; i++)
- output += ("*");
- output += "\n\n";
- return output;
- }
- }
- =======================================================================================================================================
- //Name: Michael Zhan
- //Date: 20180110
- //Lab: Occurences Runner
- public class OccurrencesRunner
- {
- public static void main( String [] args)
- {
- Occurrences test = new Occurrences("1 5 3 4 5 5 5 4 3 2 5 5 5 3 0 4 5");
- System.out.println(test);
- test.setList("2 3 4 5 6 7 8 9 0 2 3 5 6 8 8 8 9 4 5");
- System.out.println(test);
- test.setList("2 3 4 5 6 7 8 2 0 2 3 5 6 8 8 8 9 4 5");
- System.out.println(test);
- System.out.println ("program completed by: Michael Zhan");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement