Advertisement
makispaiktis

1. Beginners Project

May 15th, 2022 (edited)
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. public class Beginner {
  5.  
  6.     // CLASS 1 - MAIN
  7.     public static void main(String args[]){
  8.  
  9.         // 26 - Random
  10.         System.out.println("**** Random (26) ****");
  11.         System.out.println("Random dice");
  12.         Random dice = new Random();
  13.         int number;
  14.         for(int i=0; i<10; i++){
  15.             number = 1 + dice.nextInt(6);
  16.             System.out.println(number);
  17.         }
  18.         System.out.println();
  19.  
  20.         // 27, 28, 29 - Arrays
  21.         System.out.println("**** Arrays (27, 28) ****");
  22.         int array[] = {32, 16, 8, 4, 2, 1};
  23.         System.out.println("Index\tValue");
  24.         for(int i=0; i<array.length; i++){
  25.             System.out.println(i + "\t\t" + array[i]);
  26.         }
  27.         int sum = Arrays.stream(array).sum();
  28.         System.out.println("SUM = " + sum);
  29.         System.out.println();
  30.  
  31.         // 30 - Frequency table
  32.         System.out.println("**** Frequency tables (30) ****");
  33.         int ROLL = 1200;
  34.         System.out.println("I will throw a dice " + ROLL + " times and count the number of incidences of each number");
  35.         int freq[] = new int[7];
  36.         int zaria;
  37.         // The above array consists of 7 elements equal to 0 right now, but I want to update it
  38.         // Every time, I throw '4' for example, I want to add +1 in index '4' of array
  39.         for(int roll=1; roll<=ROLL; roll++){
  40.             zaria = 1 + dice.nextInt(6);
  41.             ++freq[zaria];
  42.             // The above command increases by 1, the value of 'freq' in index 'zaria'
  43.         }
  44.         float average = ROLL / 6;
  45.         System.out.println("Face\tTimes");
  46.         for(int face=1; face< freq.length; face++){
  47.             System.out.println(face + "\t\t" + freq[face] + "\t\t(" + (freq[face] - average) + ")");
  48.         }
  49.         System.out.println();
  50.  
  51.         // 31, 32 - Using methods
  52.         System.out.println("**** Methods (31, 32) ****");
  53.         int a[] = {1, 2, 3, 4, 5, 6};
  54.         print(a);
  55.         add_number(a, 100);
  56.         print(a);
  57.         System.out.println();
  58.  
  59.         // 33, 34 - Multidimensional arrays
  60.         System.out.println("**** Multidimensional arrays (33, 34)");
  61.         int multi[][] = {a, {11}, {21, 22},  {31, 32, 33}};
  62.         print(multi);
  63.  
  64.         // 35 - Unknown number of arguments
  65.         System.out.println("**** Unknown number of arguments in a method (35) ****");
  66.         int e = 10;
  67.         int b = 20;
  68.         int c = 30;
  69.         int d = 100;
  70.         print_average(e, b);
  71.         print_average(e, b, c);
  72.         print_average(e, b, c, d);
  73.         System.out.println();
  74.  
  75.         // 36 -
  76.  
  77.  
  78.  
  79.     } // END OF MAIN
  80.  
  81.  
  82.     // FUNCTION 2 - PRINT ELEMENTS
  83.     public static void print(int array[]){
  84.         String str = "[";
  85.         for(int i=0; i<array.length; i++) {
  86.             if(i == array.length - 1){
  87.                 str += array[i];
  88.             }
  89.             else {
  90.                 str += array[i] + ", ";
  91.             }
  92.         }
  93.         str += "]";
  94.         System.out.println(str);
  95.     } // END OF STATUS
  96.  
  97.  
  98.     // FUNCTION 3 - ADD NUMBER
  99.     public static void add_number(int array[], int n){
  100.         // This CANNOT BE DONE WITH ENHANCE FOR-LOOP
  101.         for(int element = 0; element<array.length; element++){
  102.             array[element] += n;
  103.         }
  104.     } // END OF ADD NUMBER
  105.  
  106.  
  107.     // FUNCTION 4 - DISPLAY MULTIS
  108.     public static void print(int multi[][]){
  109.         for(int row=0; row<multi.length; row++){
  110.             for(int col=0; col<multi[row].length; col++){
  111.                 System.out.print(multi[row][col] + "\t");
  112.             }
  113.             System.out.println();
  114.         }
  115.     } // END OF ADD NUMBER
  116.  
  117.  
  118.     // FUNCITON 5 - AVERAGE
  119.     public static void print_average(int... numbers){
  120.         // 'numbers' IS NOT A LIST, BUT ITS CLOSE TO THIS LOGIC
  121.         // '...' means that we do not know the exact number of arguments
  122.         int sum = 0;
  123.         String str = "[";
  124.         for(int n: numbers) {
  125.             sum += n;
  126.             str += n + ", ";
  127.         }
  128.         str += "]";
  129.         float average = sum / numbers.length;
  130.         System.out.println("Average of " + str + " is = " + average);
  131.     } // END OF AVERAGE
  132.  
  133.  
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement