Advertisement
kwasinski

University exercise done in 3min

May 29th, 2022
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. interface IterateOver
  4. {
  5.     public void iterate(int i);
  6. }
  7.  
  8. class ModuloEmatrizes
  9. {
  10.     int[] anyArray = new int[5];
  11.     int sum = 0;
  12.     int productory = 1;
  13.  
  14.     public ModuloEmatrizes()
  15.     {
  16.         this.readValues();
  17.     }
  18.  
  19.     public static void main(String[] args)
  20.     {
  21.         ModuloEmatrizes obj = new ModuloEmatrizes();
  22.         obj.calcSum();
  23.         obj.calcProd();
  24.  
  25.         obj.showUserResult();
  26.  
  27.         System.exit(1);
  28.     }
  29.  
  30.     public void readValues()
  31.     {
  32.         this.loooop(
  33.             (i) ->  {
  34.                 this.anyArray[i] = Integer.parseInt(
  35.                     JOptionPane.showInputDialog("Input a number " + i)
  36.                 );
  37.             }
  38.         );
  39.     }
  40.  
  41.     public void calcSum()
  42.     {
  43.         this.loooop(
  44.             (i) -> {
  45.                 this.sum += this.anyArray[i];
  46.             }
  47.         );
  48.     }
  49.  
  50.     public void calcProd()
  51.     {
  52.         this.loooop(
  53.             (i) -> {
  54.                 this.productory *= this.anyArray[i];
  55.             }
  56.         );
  57.     }
  58.  
  59.     public void loooop(IterateOver obj)
  60.     {
  61.         for (int i = 0; i <= this.anyArray.length - 1; i++)
  62.         {
  63.             obj.iterate(i);
  64.         }
  65.     }
  66.  
  67.     public void showUserResult()
  68.     {
  69.         JOptionPane.showMessageDialog(
  70.             null,
  71.             "Result:\n\nSum: " + this.sum +
  72.             "\nProductory: " + this.productory
  73.         );
  74.  
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement