Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- public class Beginner {
- // CLASS 1 - MAIN
- public static void main(String args[]){
- // 26 - Random
- System.out.println("**** Random (26) ****");
- System.out.println("Random dice");
- Random dice = new Random();
- int number;
- for(int i=0; i<10; i++){
- number = 1 + dice.nextInt(6);
- System.out.println(number);
- }
- System.out.println();
- // 27, 28, 29 - Arrays
- System.out.println("**** Arrays (27, 28) ****");
- int array[] = {32, 16, 8, 4, 2, 1};
- System.out.println("Index\tValue");
- for(int i=0; i<array.length; i++){
- System.out.println(i + "\t\t" + array[i]);
- }
- int sum = Arrays.stream(array).sum();
- System.out.println("SUM = " + sum);
- System.out.println();
- // 30 - Frequency table
- System.out.println("**** Frequency tables (30) ****");
- int ROLL = 1200;
- System.out.println("I will throw a dice " + ROLL + " times and count the number of incidences of each number");
- int freq[] = new int[7];
- int zaria;
- // The above array consists of 7 elements equal to 0 right now, but I want to update it
- // Every time, I throw '4' for example, I want to add +1 in index '4' of array
- for(int roll=1; roll<=ROLL; roll++){
- zaria = 1 + dice.nextInt(6);
- ++freq[zaria];
- // The above command increases by 1, the value of 'freq' in index 'zaria'
- }
- float average = ROLL / 6;
- System.out.println("Face\tTimes");
- for(int face=1; face< freq.length; face++){
- System.out.println(face + "\t\t" + freq[face] + "\t\t(" + (freq[face] - average) + ")");
- }
- System.out.println();
- // 31, 32 - Using methods
- System.out.println("**** Methods (31, 32) ****");
- int a[] = {1, 2, 3, 4, 5, 6};
- print(a);
- add_number(a, 100);
- print(a);
- System.out.println();
- // 33, 34 - Multidimensional arrays
- System.out.println("**** Multidimensional arrays (33, 34)");
- int multi[][] = {a, {11}, {21, 22}, {31, 32, 33}};
- print(multi);
- // 35 - Unknown number of arguments
- System.out.println("**** Unknown number of arguments in a method (35) ****");
- int e = 10;
- int b = 20;
- int c = 30;
- int d = 100;
- print_average(e, b);
- print_average(e, b, c);
- print_average(e, b, c, d);
- System.out.println();
- // 36 -
- } // END OF MAIN
- // FUNCTION 2 - PRINT ELEMENTS
- public static void print(int array[]){
- String str = "[";
- for(int i=0; i<array.length; i++) {
- if(i == array.length - 1){
- str += array[i];
- }
- else {
- str += array[i] + ", ";
- }
- }
- str += "]";
- System.out.println(str);
- } // END OF STATUS
- // FUNCTION 3 - ADD NUMBER
- public static void add_number(int array[], int n){
- // This CANNOT BE DONE WITH ENHANCE FOR-LOOP
- for(int element = 0; element<array.length; element++){
- array[element] += n;
- }
- } // END OF ADD NUMBER
- // FUNCTION 4 - DISPLAY MULTIS
- public static void print(int multi[][]){
- for(int row=0; row<multi.length; row++){
- for(int col=0; col<multi[row].length; col++){
- System.out.print(multi[row][col] + "\t");
- }
- System.out.println();
- }
- } // END OF ADD NUMBER
- // FUNCITON 5 - AVERAGE
- public static void print_average(int... numbers){
- // 'numbers' IS NOT A LIST, BUT ITS CLOSE TO THIS LOGIC
- // '...' means that we do not know the exact number of arguments
- int sum = 0;
- String str = "[";
- for(int n: numbers) {
- sum += n;
- str += n + ", ";
- }
- str += "]";
- float average = sum / numbers.length;
- System.out.println("Average of " + str + " is = " + average);
- } // END OF AVERAGE
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement