Advertisement
leoalfreducci

jshell 01.10.2020

Oct 1st, 2020
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 38.84 KB | None | 0 0
  1. Last login: Thu Oct  1 14:34:39 on ttys000
  2. MacBook-Pro-di-Leonardo:~ leonardo$ jshell
  3. |  Welcome to JShell -- Version 13.0.2
  4. |  For an introduction type: /help intro
  5.  
  6. jshell> public interface IntMultiSet  {
  7.    ...> /**
  8.    ...> * Restitusice il numero delle occorrence dell’intero elem nel multi-insieme. * Apublic interface IntMultiSet  {nsieme restituisce 0
  9.    ...> /**
  10.    ...> * Restitusice il numero delle occorrence dell’intero elem nel multi-insi
  11. eme. * A elem non appartimene al multiinsieme restituisce 0
  12.    ...> **/ggiunge al multi-insieme una occorrenza di elem
  13.    ...>              int getCount(int elem);
  14.    ...> /**          void add(int elem);
  15.    ...> * Aggiunge al multi-insieme una occorrenza di elem
  16.    ...>              * */e di elem al multi-insieme **/
  17.    ...>              void add(int elem); int n);
  18.    ...> /**
  19.    ...> * Aggiunge n copie di elem al multi-insieme **/-insieme. * Restituisce false se              void  add(int elem, int n);
  20.    ...> /**
  21.    ...> * Rimuove tutte le occorrenze di elem dal multi-insieme. * Restituisce f
  22. alse se elem non e’ presente
  23.    ...> */Rimuove n copie of di elem dal multi-insieme * Restituisce false se elem non e             boolean remove(int elem);
  24.    ...> /**          * */
  25.    ...> * Rimuove n copie of di elem dal multi-insieme * Restituisce false se el
  26. em non e’ presente
  27.    ...>              * */mero degli elementi del multi-insieme. **/
  28.    ...>              boolean remove(int elem, int n);
  29.    ...> /**
  30.         * Restuisce il numero degli elementi del multi-insieme. **/
  31.         int size(); }
  32.  
  33. |  created interface IntMultiSet
  34.  
  35. jshell>
  36.  
  37. jshell>
  38.  
  39. jshell> public class Foo {
  40.    ...> private int a[];
  41.    ...>   class Foo (int[] arr_in) {
  42.    ...>     a = arr_in;
  43.    ...>   }
  44.    ...>  
  45.    ...> }
  46. |  Error:
  47. |  '{' expected
  48. |    class Foo (int[] arr_in) {
  49. |             ^
  50. |  Error:
  51. |  ';' expected
  52. |    class Foo (int[] arr_in) {
  53. |                           ^
  54. |  Error:
  55. |  reached end of file while parsing
  56. |  }
  57. |   ^
  58.  
  59. jshell> public class Foo {
  60.    ...> private int a[];
  61.    ...>   class Foo(int[] arr_in) {
  62.    ...>     a = arr_in;
  63.    ...>   }
  64.    ...>  
  65.    ...> }
  66. |  Error:
  67. |  '{' expected
  68. |    class Foo(int[] arr_in) {
  69. |             ^
  70. |  Error:
  71. |  ';' expected
  72. |    class Foo(int[] arr_in) {
  73. |                          ^
  74. |  Error:
  75. |  reached end of file while parsing
  76. |  }
  77. |   ^
  78.  
  79. jshell> public class Foo {
  80.    ...> private int a[];
  81.    ...>   class Foo(int arr_in[]) {
  82.    ...>     a = arr_in;
  83.    ...>   }
  84.    ...>  
  85.    ...> }
  86. |  Error:
  87. |  '{' expected
  88. |    class Foo(int arr_in[]) {
  89. |             ^
  90. |  Error:
  91. |  ';' expected
  92. |    class Foo(int arr_in[]) {
  93. |                          ^
  94. |  Error:
  95. |  reached end of file while parsing
  96. |  }
  97. |   ^
  98.  
  99. jshell>
  100.  
  101. jshell>
  102.  
  103. jshell>
  104.  
  105. jshell>
  106.  
  107. jshell> public class Foo {
  108.    ...> private int a[];
  109.    ...>   public Foo(int arr_in[]) {
  110.    ...>     a = arr_in;
  111.    ...>   }
  112.    ...>  
  113.    ...> }
  114. |  created class Foo
  115.  
  116. jshell> private int[] a;
  117. a ==> null
  118.  
  119. jshell> private int[] a = new int[5];
  120. a ==> int[5] { 0, 0, 0, 0, 0 }
  121.  
  122. jshell> System.out.println("a length" + a.length);
  123. a length5
  124.  
  125. jshell> int a[] = new int[5];
  126. a ==> int[5] { 0, 0, 0, 0, 0 }
  127.  
  128. jshell> a[1] = 3;
  129. $7 ==> 3
  130.  
  131. jshell> a[2] = 5;
  132. $8 ==> 5
  133.  
  134. jshell> a[3] = 7;
  135. $9 ==> 7
  136.  
  137. jshell> a[4] = 9;
  138. $10 ==> 9
  139.  
  140. jshell> a[5] = 9
  141. |  Exception java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
  142. |        at (#11:1)
  143.  
  144. jshell> a[0] = 1;
  145. $12 ==> 1
  146.  
  147. jshell> int[] b = new int[5];
  148. b ==> int[5] { 0, 0, 0, 0, 0 }
  149.  
  150. jshell> b[0]= 281;
  151. $14 ==> 281
  152.  
  153. jshell> b[1] = 293
  154. $15 ==> 293
  155.  
  156. jshell> b[2] = 1;
  157. $16 ==> 1
  158.  
  159. jshell> b[3]  = 203
  160. $17 ==> 203
  161.  
  162. jshell> b[4] = 92392;
  163. $18 ==> 92392
  164.  
  165. jshell> public int getCount(int elem) {
  166.    ...>     //cerca l'elemento
  167.    ...>     for(int i = 0; i < a.lenght; i++)
  168.    ...>       if(a[i] == elem)
  169.    ...>         return b[i];
  170.    ...>     return 0;
  171.    ...>   }
  172. |  Error:
  173. |  cannot find symbol
  174. |    symbol:   variable lenght
  175. |      for(int i = 0; i < a.lenght; i++)
  176. |                         ^------^
  177.  
  178. jshell> public int[] a = a;
  179. a ==> int[5] { 1, 3, 5, 7, 9 }
  180.  
  181. jshell> public int getCount(int elem) {
  182.    ...>     //cerca l'elemento
  183.    ...>     for(int i = 0; i < a.lenght; i++)
  184.    ...>       if(a[i] == elem)
  185.    ...>         return b[i];
  186.    ...>     return 0;
  187.    ...>   }
  188. |  Error:
  189. |  cannot find symbol
  190. |    symbol:   variable lenght
  191. |      for(int i = 0; i < a.lenght; i++)
  192. |                         ^------^
  193.  
  194. jshell> public class Foo implements IntMultiSet {
  195.    ...>   private int[] a;
  196.    ...>   private int[] b;
  197.    ...>   public Foo(int a_in[], int b_in) {
  198.    ...>     a = a_in;
  199.    ...>     b = b_in;
  200.    ...>   }
  201.    ...>
  202.    ...>   public int getCount(int elem) {
  203.    ...>     //cerca l'elemento
  204.    ...>     for(int i = 0; i < a.lenght; i++)
  205.    ...>       if(a[i] == elem)
  206.    ...>         return b[i];
  207.    ...>     return 0;
  208.    ...>   }
  209.    ...> }
  210.    ...>
  211. |  Error:
  212. |  Foo is not abstract and does not override abstract method size() in IntMultiSet
  213. |  public class Foo implements IntMultiSet {
  214. |  ^----------------------------------------...
  215. |  Error:
  216. |  incompatible types: int cannot be converted to int[]
  217. |      b = b_in;
  218. |          ^--^
  219. |  Error:
  220. |  cannot find symbol
  221. |    symbol:   variable lenght
  222. |      for(int i = 0; i < a.lenght; i++)
  223. |                         ^------^
  224.  
  225. jshell> public class Foo  {
  226.    ...>   private int[] a;
  227.    ...>   private int[] b;
  228.    ...>   public Foo(int a_in[], int b_in) {
  229.    ...>     a = a_in;
  230.    ...>     b = b_in;
  231.    ...>   }
  232.    ...>
  233.    ...>   public int getCount(int elem) {
  234.    ...>     //cerca l'elemento
  235.    ...>     for(int i = 0; i < a.lenght; i++)
  236.    ...>       if(a[i] == elem)
  237.    ...>         return b[i];
  238.    ...>     return 0;
  239.    ...>   }
  240.    ...> }
  241. |  Error:
  242. |  incompatible types: int cannot be converted to int[]
  243. |      b = b_in;
  244. |          ^--^
  245. |  Error:
  246. |  cannot find symbol
  247. |    symbol:   variable lenght
  248. |      for(int i = 0; i < a.lenght; i++)
  249. |                         ^------^
  250.  
  251. jshell> public class Foo {
  252.    ...>   private int[] a;
  253.    ...>   private int[] b;
  254.    ...>   public Foo(int a_in[], int b_in[]) {
  255.    ...>     a = a_in;
  256.    ...>     b = b_in;
  257.    ...>   }
  258.    ...>
  259.    ...>   public int getCount(int elem) {
  260.    ...>     //cerca l'elemento
  261.    ...>     for(int i = 0; i < a.lenght; i++)
  262.    ...>       if(a[i] == elem)
  263.    ...>         return b[i];
  264.    ...>     return 0;
  265.    ...>   }
  266.    ...> }
  267.    ...>
  268. |  Error:
  269. |  cannot find symbol
  270. |    symbol:   variable lenght
  271. |      for(int i = 0; i < a.lenght; i++)
  272. |                         ^------^
  273.  
  274. jshell> public class Foo {
  275.    ...>   int[] a;
  276.    ...>   private int[] b;
  277.    ...>   public Foo(int a_in[], int b_in[]) {
  278.    ...>     a = a_in;
  279.    ...>     b = b_in;
  280.    ...>   }
  281.    ...>
  282.    ...>   public int getCount(int elem) {
  283.    ...>     //cerca l'elemento
  284.    ...>     for(int i = 0; i < a.lenght; i++)
  285.    ...>       if(a[i] == elem)
  286.    ...>         return b[i];
  287.    ...>     return 0;
  288.    ...>   }
  289.    ...> }
  290.    ...>
  291. |  Error:
  292. |  cannot find symbol
  293. |    symbol:   variable lenght
  294. |      for(int i = 0; i < a.lenght; i++)
  295. |                         ^------^
  296.  
  297. jshell> public class Foo {
  298.    ...>   public int[] a;
  299.    ...>   private int[] b;
  300.    ...>   public Foo(int a_in[], int b_in[]) {
  301.    ...>     a = a_in;
  302.    ...>     b = b_in;
  303.    ...>   }
  304.    ...>
  305.    ...>   public int getCount(int elem) {
  306.    ...>     //cerca l'elemento
  307.    ...>     for(int i = 0; i < a.lenght; i++)
  308.    ...>       if(a[i] == elem)
  309.    ...>         return b[i];
  310.    ...>     return 0;
  311.    ...>   }
  312.    ...> }
  313.    ...>
  314. |  Error:
  315. |  cannot find symbol
  316. |    symbol:   variable lenght
  317. |      for(int i = 0; i < a.lenght; i++)
  318. |                         ^------^
  319.  
  320. jshell> public class Foo {
  321.    ...>   private int[] a;
  322.    ...>   private int[] b;
  323.    ...>   public Foo(int a_in[], int b_in[]) {
  324.    ...>     a = a_in;
  325.    ...>     b = b_in;
  326.    ...>   }
  327.    ...>
  328.    ...>   public int getCount(int elem) {
  329.    ...>     //cerca l'elemento
  330.    ...>     for(int i = 0; i < a.length; i++)
  331.    ...>       if(a[i] == elem)
  332.    ...>         return b[i];
  333.    ...>     return 0;
  334.    ...>   }
  335.    ...> }
  336.    ...>
  337. |  created class Foo
  338.  
  339. jshell> a;
  340. a ==> int[5] { 1, 3, 5, 7, 9 }
  341.  
  342. jshell> b;
  343. b ==> int[5] { 281, 293, 1, 203, 92392 }
  344.  
  345. jshell> getCount(7);
  346. |  Error:
  347. |  cannot find symbol
  348. |    symbol:   method getCount(int)
  349. |  getCount(7);
  350. |  ^------^
  351.  
  352. jshell> public int getCount(int elem) {
  353.    ...>     //cerca l'elemento
  354.    ...>     for(int i = 0; i < a.length; i++)
  355.    ...>       if(a[i] == elem)
  356.    ...>         return b[i];
  357.    ...>     return 0;
  358.    ...>   }
  359. |  created method getCount(int)
  360.  
  361. jshell> getCount(7);
  362. $24 ==> 203
  363.  
  364. jshell> public void add(int elem, int n) {
  365.    ...>     //aggiunge n occorrenze di elem
  366.    ...>     private int position = position(elem);
  367.    ...>     if(position == -1) {
  368.    ...>       //l'elemento non è presente nel multi-insieme, va aggiunta una posizione all'array
  369.    ...>       a = java.util.Arrays.copyOf(a, a.length + 1);
  370.    ...>       b = java.util.Arrays.copyOf(b, b.length + 1);
  371.    ...>       position = a.length - 1;
  372.    ...>       a[position] = elem;
  373.    ...>       b[position] = 0;
  374.    ...>
  375.    ...>     }
  376.    ...>
  377.    ...>     b[position]+=n;
  378.    ...>   }
  379. |  Error:
  380. |  illegal start of expression
  381. |      private int position = position(elem);
  382. |      ^
  383.  
  384. jshell> public void add(int elem, int n) {
  385.    ...>     //aggiunge n occorrenze di elem
  386.    ...>     int position = position(elem);
  387.    ...>     if(position == -1) {
  388.    ...>       //l'elemento non è presente nel multi-insieme, va aggiunta una posizione all'array
  389.    ...>       a = java.util.Arrays.copyOf(a, a.length + 1);
  390.    ...>       b = java.util.Arrays.copyOf(b, b.length + 1);
  391.    ...>       position = a.length - 1;
  392.    ...>       a[position] = elem;
  393.    ...>       b[position] = 0;
  394.    ...>
  395.    ...>     }
  396.    ...>
  397.    ...>     b[position]+=n;
  398.    ...>   }
  399. |  created method add(int,int), however, it cannot be invoked until method position(int) is declared
  400.  
  401. jshell> public void position(int elem) {
  402.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisce la posizione altrimenti -1
  403.    ...>     for(int i = 0; i < a.length; i++)
  404.    ...>       if(a[i] == elem)
  405.    ...>         return i;
  406.    ...>     return -1;
  407.    ...>   }
  408. |  Error:
  409. |  incompatible types: unexpected return value
  410. |          return i;
  411. |                 ^
  412. |  Error:
  413. |  incompatible types: unexpected return value
  414. |      return -1;
  415. |             ^^
  416.  
  417. jshell> public int position(int elem) {
  418.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisce la posizione altrimenti -1
  419.    ...>     for(int i = 0; i < a.length; i++)
  420.    ...>       if(a[i] == elem)
  421.    ...>         return i;
  422.    ...>     return -1;
  423.    ...>   }
  424. |  created method position(int)
  425.  
  426. jshell> public void add(int elem, int n) {
  427.    ...>     //aggiunge n occorrenze di elem
  428.    ...>     int position = position(elem);
  429.    ...>     if(position == -1) {
  430.    ...>       //l'elemento non è presente nel multi-insieme, va aggiunta una posizione all'array
  431.    ...>       a = java.util.Arrays.copyOf(a, a.length + 1);
  432.    ...>       b = java.util.Arrays.copyOf(b, b.length + 1);
  433.    ...>       position = a.length - 1;
  434.    ...>       a[position] = elem;
  435.    ...>       b[position] = 0;
  436.    ...>
  437.    ...>     }
  438.    ...>
  439.    ...>     b[position]+=n;
  440.    ...>   }
  441. |  modified method add(int,int)
  442.  
  443. jshell> a;
  444. a ==> int[5] { 1, 3, 5, 7, 9 }
  445.  
  446. jshell> b;
  447. b ==> int[5] { 281, 293, 1, 203, 92392 }
  448.  
  449. jshell> add(1, 3);
  450.  
  451. jshell> a;
  452. a ==> int[5] { 1, 3, 5, 7, 9 }
  453.  
  454. jshell> b;
  455. b ==> int[5] { 284, 293, 1, 203, 92392 }
  456.  
  457. jshell> add(8, 1023910);
  458.  
  459. jshell> a;
  460. a ==> int[6] { 1, 3, 5, 7, 9, 8 }
  461.  
  462. jshell> b;
  463. b ==> int[6] { 284, 293, 1, 203, 92392, 1023910 }
  464.  
  465. jshell> a;
  466. a ==> int[6] { 1, 3, 5, 7, 9, 8 }
  467.  
  468. jshell> a = java.util.Arrays.copyOf(a, a.length - 1);
  469. a ==> int[5] { 1, 3, 5, 7, 9 }
  470.  
  471. jshell> public boolean remove(int elem, int n) {
  472.    ...>     int position = position(elem);
  473.    ...>     if(position == -1)
  474.    ...>       return false;
  475.    ...>
  476.    ...>     b[position]-=n;
  477.    ...>     if(b[position] <= 0) {
  478.    ...>       //l'elemento va eliminato dall'array a e b
  479.    ...>       for(int i = position + 1; i < a.length; i++) {
  480.    ...>         a[i - 1] = a[i];
  481.    ...>         b[i - 1] = b[i];
  482.    ...>       }
  483.    ...>       a = java.util.Arrays.copyOf(a, a.length - 1);
  484.    ...>       b = java.util.Arrays.copyOf(b, b.length - 1);
  485.    ...>     }
  486.    ...>
  487.    ...>     return true;  
  488.    ...>   }
  489. |  created method remove(int,int)
  490.  
  491. jshell> a;
  492. a ==> int[5] { 1, 3, 5, 7, 9 }
  493.  
  494. jshell> remve(2932, 1);
  495. |  Error:
  496. |  cannot find symbol
  497. |    symbol:   method remve(int,int)
  498. |  remve(2932, 1);
  499. |  ^---^
  500.  
  501. jshell> remove(2932, 1);
  502. $40 ==> false
  503.  
  504. jshell> a; b;
  505. a ==> int[5] { 1, 3, 5, 7, 9 }
  506. b ==> int[6] { 284, 293, 1, 203, 92392, 1023910 }
  507.  
  508. jshell> remove(1, 280);
  509. $43 ==> true
  510.  
  511. jshell> a;b;
  512. a ==> int[5] { 1, 3, 5, 7, 9 }
  513. b ==> int[6] { 4, 293, 1, 203, 92392, 1023910 }
  514.  
  515. jshell> remove(3, 1);
  516. $46 ==> true
  517.  
  518. jshell> a;b;
  519. a ==> int[5] { 1, 3, 5, 7, 9 }
  520. b ==> int[6] { 4, 292, 1, 203, 92392, 1023910 }
  521.  
  522. jshell> b = java.util.Arrays.copyOf(b, b.length - 1);
  523. b ==> int[5] { 4, 292, 1, 203, 92392 }
  524.  
  525. jshell> a;b;
  526. a ==> int[5] { 1, 3, 5, 7, 9 }
  527. b ==> int[5] { 4, 292, 1, 203, 92392 }
  528.  
  529. jshell> remove(1, 4);
  530. $52 ==> true
  531.  
  532. jshell> a;b;
  533. a ==> int[4] { 3, 5, 7, 9 }
  534. b ==> int[4] { 292, 1, 203, 92392 }
  535.  
  536. jshell> remove(5, 3);
  537. $55 ==> true
  538.  
  539. jshell> a;b;
  540. a ==> int[3] { 3, 7, 9 }
  541. b ==> int[3] { 292, 203, 92392 }
  542.  
  543. jshell> remove(9, 9232983);
  544. $58 ==> true
  545.  
  546. jshell> a;b;
  547. a ==> int[2] { 3, 7 }
  548. b ==> int[2] { 292, 203 }
  549.  
  550. jshell> public int size() {
  551.    ...>     int sum = 0;
  552.    ...>     for(int i = 0; i < b.length; i++)
  553.    ...>       sum+=b[i];
  554.    ...>     return sum;
  555.    ...>   }
  556. |  created method size()
  557.  
  558. jshell> size();
  559. $62 ==> 495
  560.  
  561. jshell> public class Foo implements IntMultiSet{
  562.    ...>   private int[] a;
  563.    ...>   private int[] b;
  564.    ...>   public Foo(int a_in[], int b_in[]) {
  565.    ...>     a = a_in;
  566.    ...>     b = b_in;
  567.    ...>   }
  568.    ...>
  569.    ...>   public int getCount(int elem) {
  570.    ...>     //cerca l'elemento e ne restituisce quante copie ve ne sono
  571.    ...>     for(int i = 0; i < a.length; i++)
  572.    ...>       if(a[i] == elem)
  573.    ...>         return b[i];
  574.    ...>     return 0;
  575.    ...>   }
  576.    ...>
  577.    ...>   public int position(int elem) {
  578.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisce la posizione altrimenti -1
  579.    ...>     for(int i = 0; i < a.length; i++)
  580.    ...>       if(a[i] == elem)
  581.    ...>         return i;
  582.    ...>     return -1;
  583.    ...>   }
  584.    ...>
  585.    ...>   public void add(int elem, int n) {
  586.    ...>     //aggiunge n occorrenze di elem
  587.    ...>     int position = position(elem);
  588.    ...>     if(position == -1) {
  589.    ...>       //l'elemento non è presente nel multi-insieme, va aggiunta una posizione all'array
  590.    ...>       a = java.util.Arrays.copyOf(a, a.length + 1);
  591.    ...>       b = java.util.Arrays.copyOf(b, b.length + 1);
  592.    ...>       position = a.length - 1;
  593.    ...>       a[position] = elem;
  594.    ...>       b[position] = 0;
  595.    ...>     }
  596.    ...>
  597.    ...>     b[position]+=n;
  598.    ...>   }
  599.    ...>
  600.    ...>   public void add(int elem) {
  601.    ...>     //aggiunge una copia di elem nell'array
  602.    ...>     add(elem, 1);
  603.    ...>   }
  604.    ...>
  605.    ...>   public boolean remove(int elem, int n) {
  606.    ...>     int position = position(elem);
  607.    ...>     if(position == -1)
  608.    ...>       return false;
  609.    ...> public class Foo implements IntMultiSet{
  610.    ...>   private int[] a;;
  611.    ...>   private int[] b; <= 0) {
  612.    ...>   public Foo(int a_in[], int b_in[]) {rray a e b
  613.    ...>     a = a_in; i = position + 1; i < a.length; i++) {
  614.    ...>     b = b_in; 1] = a[i];
  615.    ...>   }     b[i - 1] = b[i];
  616.    ...>       }
  617.    ...>   public int getCount(int elem) {(a, a.length - 1);
  618.    ...>     //cerca l'elemento e ne restituisce quante copie ve ne sono
  619.    ...>     for(int i = 0; i < a.length; i++)
  620.    ...>       if(a[i] == elem)
  621.    ...>         return b[i];
  622.    ...>     return 0;
  623.    ...>   }
  624.    ...>   public int size() {
  625.    ...>   public int position(int elem) {
  626.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisc
  627. e la posizione altrimenti -1
  628.    ...>     for(int i = 0; i < a.length; i++)
  629.    ...>       if(a[i] == elem)
  630.    ...>         return i;
  631.    ...>     return -1;
  632.    ...>   }
  633.  
  634.           public void add(int elem, int n) {
  635.             //aggiunge n occorrenze di elem
  636.             int position = position(elem);
  637.             if(position == -1) {
  638.               //l'elemento non è presente nel multi-insieme, va aggiunta una pos
  639. izione all'array
  640.              a = java.util.Arrays.copyOf(a, a.length + 1);
  641.              b = java.util.Arrays.copyOf(b, b.length + 1);
  642.              position = a.length - 1;
  643.              a[position] = elem;
  644.              b[position] = 0;
  645.            }
  646.  
  647.            b[position]+=n;
  648.          }
  649.  
  650.          public void add(int elem) {
  651.            //aggiunge una copia di elem nell'array
  652.             add(elem, 1);
  653.           }
  654.  
  655.           public boolean remove(int elem, int n) {
  656.             int position = position(elem);
  657.             if(position == -1)
  658.               return false;
  659.  
  660.             b[position]-=n;
  661.             if(b[position] <= 0) {
  662.               //l'elemento va eliminato dall'array a e b
  663.               for(int i = position + 1; i < a.length; i++) {
  664.                 a[i - 1] = a[i];
  665.                 b[i - 1] = b[i];
  666.               }
  667.               a = java.util.Arrays.copyOf(a, a.length - 1);
  668.               b = java.util.Arrays.copyOf(b, b.length - 1);
  669.             }
  670.  
  671.             return true;
  672.           }
  673.  
  674.           public int size() {
  675.             int sum = 0;
  676.             for(int i = 0; i < b.length; i++)
  677.               sum+=b[i];
  678.             return sum;
  679.           }
  680.  
  681.         }
  682.  
  683. |  Error:
  684. |  Foo is not abstract and does not override abstract method remove(int) in IntMultiSet
  685. |  public class Foo implements IntMultiSet{
  686. |  ^---------------------------------------...
  687.  
  688. jshell> public class Foo implements IntMultiSet{
  689.    ...>   private int[] a;
  690.    ...>   private int[] b;
  691.    ...>   public Foo(int a_in[], int b_in[]) {
  692.    ...>     a = a_in;
  693.    ...>     b = b_in;
  694.    ...>   }
  695.    ...>
  696.    ...>   public int getCount(int elem) {
  697.    ...>     //cerca l'elemento e ne restituisce quante copie ve ne sono
  698.    ...>     for(int i = 0; i < a.length; i++)
  699.    ...>       if(a[i] == elem)
  700.    ...>         return b[i];
  701.    ...>     return 0;
  702.    ...>   }
  703.    ...>
  704.    ...>   public int position(int elem) {
  705.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisce la posizione altrimenti -1
  706.    ...>     for(int i = 0; i < a.length; i++)
  707.    ...>       if(a[i] == elem)
  708.    ...>         return i;
  709.    ...>     return -1;
  710.    ...>   }
  711.    ...>
  712.    ...>   public void add(int elem, int n) {
  713.    ...>     //aggiunge n occorrenze di elem
  714.    ...>     int position = position(elem);
  715.    ...>     if(position == -1) {
  716.    ...>       //l'elemento non è presente nel multi-insieme, va aggiunta una posizione all'array
  717.    ...>       a = java.util.Arrays.copyOf(a, a.length + 1);
  718.    ...>       b = java.util.Arrays.copyOf(b, b.length + 1);
  719.    ...>       position = a.length - 1;
  720.    ...>       a[position] = elem;
  721.    ...>       b[position] = 0;
  722.    ...>     }
  723.    ...>
  724.    ...>     b[position]+=n;
  725.    ...>   }
  726.    ...>
  727.    ...>   public void add(int elem) {
  728.    ...>     //aggiunge una copia di elem nell'array
  729.    ...>     add(elem, 1);
  730.    ...>   }
  731.    ...>
  732.    ...>   public boolean remove(int elem, int n) {
  733.    ...>     int position = position(elem);
  734.    ...>     if(position == -1)
  735.    ...>       return false;
  736.    ...>
  737.    ...>     b[position]-=n;
  738.    ...>     if(b[position] <= 0) {
  739.    ...>       //l'elemento va eliminato dall'array a e b
  740.    ...> public class Foo implements IntMultiSet{ngth; i++) {
  741.    ...>   private int[] a; a[i];
  742.    ...>   private int[] b; b[i];
  743.    ...>   public Foo(int a_in[], int b_in[]) {
  744.    ...>     a = a_in;a.util.Arrays.copyOf(a, a.length - 1);
  745.    ...>     b = b_in;a.util.Arrays.copyOf(b, b.length - 1);
  746.    ...>   } }
  747.    ...>
  748.    ...>   public int getCount(int elem) {
  749.    ...>     //cerca l'elemento e ne restituisce quante copie ve ne sono
  750.    ...>     for(int i = 0; i < a.length; i++)
  751.    ...>       if(a[i] == elem)e(int elem) {
  752.    ...>         return b[i];em, 1);
  753.    ...>     return 0;
  754.    ...>   }
  755.    ...>   public int size() {
  756.    ...>   public int position(int elem) {
  757.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisc
  758. e la posizione altrimenti -1
  759.    ...>     for(int i = 0; i < a.length; i++)
  760.    ...>       if(a[i] == elem)
  761.    ...>         return i;
  762.    ...>     return -1;
  763.    ...>   }
  764.  
  765.           public void add(int elem, int n) {
  766.             //aggiunge n occorrenze di elem
  767.             int position = position(elem);
  768.             if(position == -1) {
  769.               //l'elemento non è presente nel multi-insieme, va aggiunta una pos
  770. izione all'array
  771.              a = java.util.Arrays.copyOf(a, a.length + 1);
  772.              b = java.util.Arrays.copyOf(b, b.length + 1);
  773.              position = a.length - 1;
  774.              a[position] = elem;
  775.              b[position] = 0;
  776.            }
  777.  
  778.            b[position]+=n;
  779.          }
  780.  
  781.          public void add(int elem) {
  782.            //aggiunge una copia di elem nell'array
  783.             add(elem, 1);
  784.           }
  785.  
  786.           public boolean remove(int elem, int n) {
  787.             int position = position(elem);
  788.             if(position == -1)
  789.               return false;
  790.  
  791.             b[position]-=n;
  792.             if(b[position] <= 0) {
  793.               //l'elemento va eliminato dall'array a e b
  794.               for(int i = position + 1; i < a.length; i++) {
  795.                 a[i - 1] = a[i];
  796.                 b[i - 1] = b[i];
  797.               }
  798.               a = java.util.Arrays.copyOf(a, a.length - 1);
  799.               b = java.util.Arrays.copyOf(b, b.length - 1);
  800.             }
  801.  
  802.             return true;
  803.           }
  804.  
  805.           public boolean remove(int elem) {
  806.             return remove(elem, 1);
  807.           }
  808.  
  809.           public int size() {
  810.             int sum = 0;
  811.             for(int i = 0; i < b.length; i++)
  812.               sum+=b[i];
  813.             return sum;
  814.           }
  815.  
  816.         }
  817.  
  818. |  created class Foo
  819.  
  820. jshell> a;
  821. a ==> int[2] { 3, 7 }
  822.  
  823. jshell> int a[5] = { 1, 3, 5, 7, 9 }
  824. |  Error:
  825. |  ']' expected
  826. |  int a[5] = { 1, 3, 5, 7, 9 };
  827. |        ^
  828.  
  829. jshell> add (19, 382);
  830.  
  831. jshell> add(283, 4924);
  832.  
  833. jshell> add (1283, 293020392);
  834.  
  835. jshell> a;b;
  836. a ==> int[5] { 3, 7, 19, 283, 1283 }
  837. b ==> int[5] { 292, 203, 382, 4924, 293020392 }
  838.  
  839. jshell> Foo obj = new obj(a, b);
  840. |  Error:
  841. |  cannot find symbol
  842. |    symbol:   class obj
  843. |  Foo obj = new obj(a, b);
  844. |                ^-^
  845.  
  846. jshell> Foo obj = new Foo(a, b);
  847. obj ==> Foo@200a570f
  848.  
  849. jshell> obj.size;
  850. |  Error:
  851. |  cannot find symbol
  852. |    symbol:   variable size
  853. |  obj.size;
  854. |  ^------^
  855.  
  856. jshell> obj.size();
  857. $71 ==> 293026193
  858.  
  859. jshell> a;b;
  860. a ==> int[5] { 3, 7, 19, 283, 1283 }
  861. b ==> int[5] { 292, 203, 382, 4924, 293020392 }
  862.  
  863. jshell> remove(3, 2934392);
  864. $74 ==> true
  865.  
  866. jshell> int[] arr_fin = new int[0];
  867. arr_fin ==> int[0] {  }
  868.  
  869. jshell> obj.arr1;
  870. |  Error:
  871. |  cannot find symbol
  872. |    symbol:   variable arr1
  873. |  obj.arr1;
  874. |  ^------^
  875.  
  876. jshell> obj.a;
  877. |  Error:
  878. |  a has private access in Foo
  879. |  obj.a;
  880. |  ^---^
  881.  
  882. jshell> public class Foo implements IntMultiSet{
  883.    ...>   public int[] a;
  884.    ...>   public int[] b;
  885.    ...>   public Foo(int a_in[], int b_in[]) {
  886.    ...>     a = a_in;
  887.    ...>     b = b_in;
  888.    ...>   }
  889.    ...>
  890.    ...>   public int getCount(int elem) {
  891.    ...>     //cerca l'elemento e ne restituisce quante copie ve ne sono
  892.    ...>     for(int i = 0; i < a.length; i++)
  893.    ...>       if(a[i] == elem)
  894.    ...>         return b[i];
  895.    ...>     return 0;
  896.    ...>   }
  897.    ...>
  898.    ...>   public int position(int elem) {
  899.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisce la posizione altrimenti -1
  900.    ...>     for(int i = 0; i < a.length; i++)
  901.    ...>       if(a[i] == elem)
  902.    ...>         return i;
  903.    ...>     return -1;
  904.    ...>   }
  905.    ...>
  906.    ...>   public void add(int elem, int n) {
  907.    ...>     //aggiunge n occorrenze di elem
  908.    ...>     int position = position(elem);
  909.    ...>     if(position == -1) {
  910.    ...>       //l'elemento non è presente nel multi-insieme, va aggiunta una posizione all'array
  911.    ...>       a = java.util.Arrays.copyOf(a, a.length + 1);
  912.    ...>       b = java.util.Arrays.copyOf(b, b.length + 1);
  913.    ...>       position = a.length - 1;
  914.    ...>       a[position] = elem;
  915.    ...>       b[position] = 0;
  916.    ...>     }
  917.    ...>
  918.    ...>     b[position]+=n;
  919.    ...>   }
  920.    ...>
  921.    ...>   public void add(int elem) {
  922.    ...>     //aggiunge una copia di elem nell'array
  923.    ...>     add(elem, 1);
  924.    ...>   }
  925.    ...>
  926.    ...>   public boolean remove(int elem, int n) {
  927.    ...>     int position = position(elem);
  928.    ...>     if(position == -1)
  929.    ...>       return false;
  930.    ...>
  931.    ...>     b[position]-=n;
  932.    ...>     if(b[position] <= 0) {
  933.    ...> public class Foo implements IntMultiSet{ay a e b
  934.    ...>   public int[] a; position + 1; i < a.length; i++) {
  935.    ...>   public int[] b;= a[i];
  936.    ...>   public Foo(int a_in[], int b_in[]) {
  937.    ...>     a = a_in;
  938.    ...>     b = b_in;a.util.Arrays.copyOf(a, a.length - 1);
  939.    ...>   }   b = java.util.Arrays.copyOf(b, b.length - 1);
  940.    ...>     }
  941.    ...>   public int getCount(int elem) {
  942.    ...>     //cerca l'elemento e ne restituisce quante copie ve ne sono
  943.    ...>     for(int i = 0; i < a.length; i++)
  944.    ...>       if(a[i] == elem)
  945.    ...>         return b[i];ove(int elem) {
  946.    ...>     return 0;move(elem, 1);
  947.    ...>   }
  948.    ...>
  949.    ...>   public int position(int elem) {
  950.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisc
  951. e la posizione altrimenti -1 < b.length; i++)
  952.    ...>     for(int i = 0; i < a.length; i++)
  953.    ...>       if(a[i] == elem)
  954.    ...>         return i;
  955.    ...>     return -1;
  956.    ...>   }
  957.  
  958.           public void add(int elem, int n) {
  959.             //aggiunge n occorrenze di elem
  960.             int position = position(elem);
  961.             if(position == -1) {
  962.               //l'elemento non è presente nel multi-insieme, va aggiunta una pos
  963. izione all'array
  964.              a = java.util.Arrays.copyOf(a, a.length + 1);
  965.              b = java.util.Arrays.copyOf(b, b.length + 1);
  966.              position = a.length - 1;
  967.              a[position] = elem;
  968.              b[position] = 0;
  969.            }
  970.  
  971.            b[position]+=n;
  972.          }
  973.  
  974.          public void add(int elem) {
  975.            //aggiunge una copia di elem nell'array
  976.             add(elem, 1);
  977.           }
  978.  
  979.           public boolean remove(int elem, int n) {
  980.             int position = position(elem);
  981.             if(position == -1)
  982.               return false;
  983.  
  984.             b[position]-=n;
  985.             if(b[position] <= 0) {
  986.               //l'elemento va eliminato dall'array a e b
  987.               for(int i = position + 1; i < a.length; i++) {
  988.                 a[i - 1] = a[i];
  989.                 b[i - 1] = b[i];
  990.               }
  991.               a = java.util.Arrays.copyOf(a, a.length - 1);
  992.               b = java.util.Arrays.copyOf(b, b.length - 1);
  993.             }
  994.  
  995.             return true;
  996.           }
  997.  
  998.           public boolean remove(int elem) {
  999.             return remove(elem, 1);
  1000.           }
  1001.  
  1002.           public int size() {
  1003.             int sum = 0;
  1004.             for(int i = 0; i < b.length; i++)
  1005.               sum+=b[i];
  1006.             return sum;
  1007.           }
  1008.         }
  1009.  
  1010. |  replaced class Foo
  1011. |    update replaced variable obj, reset to null
  1012.  
  1013. jshell> public class Foo implements IntMultiSet{
  1014.    ...>   public int[] a;
  1015.    ...>   public int[] b;
  1016.    ...>   public Foo(int a_in[], int b_in[]) {
  1017.    ...>     a = a_in;
  1018.    ...>     b = b_in;
  1019.    ...>   }
  1020.    ...>
  1021.    ...>   public int getCount(int elem) {
  1022.    ...>     //cerca l'elemento e ne restituisce quante copie ve ne sono
  1023.    ...>     for(int i = 0; i < a.length; i++)
  1024.    ...>       if(a[i] == elem)
  1025.    ...>         return b[i];
  1026.    ...>     return 0;
  1027.    ...>   }
  1028.    ...>
  1029.    ...>   public int position(int elem) {
  1030.    ...>     //cerca la posizione dell'elemento elem in a. Se lo trova restituisce la posizione altrimenti -1
  1031.    ...>     for(int i = 0; i < a.length; i++)
  1032.    ...>       if(a[i] == elem)
  1033.    ...>         return i;
  1034.    ...>     return -1;
  1035.    ...>   }
  1036.    ...>
  1037.    ...>   public void add(int elem, int n) {
  1038.    ...>     //aggiunge n occorrenze di elem
  1039.    ...>     int position = position(elem);
  1040.    ...>     if(position == -1) {
  1041.    ...>       //l'elemento non è presente nel multi-insieme, va aggiunta una posizione all'array
  1042.    ...>       a = java.util.Arrays.copyOf(a, a.length + 1);
  1043.    ...>       b = java.util.Arrays.copyOf(b, b.length + 1);
  1044.    ...>       position = a.length - 1;
  1045.    ...>       a[position] = elem;
  1046.    ...>       b[position] = 0;
  1047.    ...>     }
  1048.    ...>
  1049.    ...>     b[position]+=n;
  1050.    ...>   }
  1051.    ...>
  1052.    ...>   public void add(int elem) {
  1053.    ...>     //aggiunge una copia di elem nell'array
  1054.    ...>     add(elem, 1);
  1055.    ...>   }
  1056.    ...>
  1057.    ...>   public boolean remove(int elem, int n) {
  1058.    ...>     int position = position(elem);
  1059.    ...>     if(position == -1)
  1060.    ...>       return false;
  1061.    ...>
  1062.    ...>     b[position]-=n;
  1063.    ...>
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.         obj.a;
  1116. |  Exception java.lang.NullPointerException
  1117. |        at (#77:1)
  1118.  
  1119. jshell> obj.a;
  1120. |  Exception java.lang.NullPointerException
  1121. |        at (#78:1)
  1122.  
  1123. jshell> obj;
  1124. obj ==> null
  1125.  
  1126. jshell> Foo obj = new Foo(a, b);
  1127. obj ==> Foo@299a06ac
  1128.  
  1129. jshell> obj;
  1130. obj ==> Foo@299a06ac
  1131.  
  1132. jshell> obj.a;
  1133. $82 ==> int[4] { 7, 19, 283, 1283 }
  1134.  
  1135. jshell> obj1.a.length
  1136. |  Error:
  1137. |  package obj1 does not exist
  1138. |  obj1.a.length
  1139. |  ^----^
  1140.  
  1141. jshell> obj.a.length;
  1142. $83 ==> 4
  1143.  
  1144. jshell> public void Union(Foo obj1, Foo obj2) {
  1145.    ...>   private int[] arr_a_tmp = new int[0];
  1146.    ...>   private int arr_b_tmp = new int[0];
  1147.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1148.    ...>   for(int i = 0; i < obj1.a.length; i++) {
  1149.    ...>     if(obj2.position(obj1.a[i]) != -1)
  1150.    ...>       obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.position(obj1.a[i])));
  1151.    ...>   }
  1152.    ...> }
  1153. |  Error:
  1154. |  illegal start of expression
  1155. |    private int[] arr_a_tmp = new int[0];
  1156. |    ^
  1157.  
  1158. jshell> public void Union(Foo obj1, Foo obj2) {
  1159.    ...>   int[] arr_a_tmp = new int[0];
  1160.    ...>   int arr_b_tmp = new int[0];
  1161.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1162.    ...>   for(int i = 0; i < obj1.a.length; i++) {
  1163.    ...>     if(obj2.position(obj1.a[i]) != -1)
  1164.    ...>       obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.position(obj1.a[i])));
  1165.    ...>   }
  1166.    ...> }
  1167. |  Error:
  1168. |  incompatible types: int[] cannot be converted to int
  1169. |    int arr_b_tmp = new int[0];
  1170. |                    ^--------^
  1171. |  Error:
  1172. |  incompatible types: int cannot be converted to int[]
  1173. |    Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1174. |                                     ^-------^
  1175. |  Error:
  1176. |  cannot find symbol
  1177. |    symbol:   method max(int,int)
  1178. |        obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.position(obj1.a[i])));
  1179. |                               ^-^
  1180.  
  1181. jshell> public int max (int el1, int el2) {
  1182.    ...>   if(el1 > el2)
  1183.    ...>     return el1;
  1184.    ...>   else
  1185.    ...>     return el2;
  1186.    ...> }
  1187. |  created method max(int,int)
  1188.  
  1189. jshell> public void Union(Foo obj1, Foo obj2) {
  1190.    ...>   int[] arr_a_tmp = new int[0];
  1191.    ...>   int arr_b_tmp = new int[0];
  1192.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1193.    ...>   for(int i = 0; i < obj1.a.length; i++) {
  1194.    ...>     if(obj2.position(obj1.a[i]) != -1)
  1195.    ...>       obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.position(obj1.a[i])));
  1196.    ...>   }
  1197.    ...> }
  1198.    ...>
  1199. |  Error:
  1200. |  incompatible types: int[] cannot be converted to int
  1201. |    int arr_b_tmp = new int[0];
  1202. |                    ^--------^
  1203. |  Error:
  1204. |  incompatible types: int cannot be converted to int[]
  1205. |    Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1206. |                                     ^-------^
  1207.  
  1208. jshell> public void Union(Foo obj1, Foo obj2) {
  1209.    ...>   int[] arr_a_tmp = new int[0];
  1210.    ...>   int arr_b_tmp = new int[0];
  1211.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1212.    ...>   for(int i = 0; i < obj1.a.length; i++) {
  1213.    ...>     if(obj2.position(obj1.a[i]) != -1)
  1214.    ...>       obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.b[obj2.position(obj1.a[i])]);
  1215.    ...>   }
  1216.    ...> }
  1217. |  Error:
  1218. |  ')' expected
  1219. |        obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.b[obj2.position(obj1.a[i])]);
  1220. |                                                                               ^
  1221.  
  1222. jshell> public void Union(Foo obj1, Foo obj2) {
  1223.    ...>   int[] arr_a_tmp = new int[0];
  1224.    ...>   int arr_b_tmp = new int[0];
  1225.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1226.    ...>   for(int i = 0; i < obj1.a.length; i++) {
  1227.    ...>     if(obj2.position(obj1.a[i]) != -1)
  1228.    ...>       obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.b[obj2.position(obj1.a[i])]));
  1229.    ...>   }
  1230.    ...> }
  1231. |  Error:
  1232. |  incompatible types: int[] cannot be converted to int
  1233. |    int arr_b_tmp = new int[0];
  1234. |                    ^--------^
  1235. |  Error:
  1236. |  incompatible types: int cannot be converted to int[]
  1237. |    Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1238. |                                     ^-------^
  1239.  
  1240. jshell> public void Union(Foo obj1, Foo obj2) {
  1241.    ...>   int[] arr_a_tmp = new int[0];
  1242.    ...>   int[] arr_b_tmp = new int[0];
  1243.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1244.    ...>   for(int i = 0; i < obj1.a.length; i++) {
  1245.    ...>     if(obj2.position(obj1.a[i]) != -1)
  1246.    ...>       obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.b[obj2.position(obj1.a[i])]));
  1247.    ...>   }
  1248.    ...> }
  1249.    ...>
  1250. |  created method Union(Foo,Foo)
  1251.  
  1252. jshell> a;b;
  1253. a ==> int[4] { 7, 19, 283, 1283 }
  1254. b ==> int[4] { 203, 382, 4924, 293020392 }
  1255.  
  1256. jshell> int[] c = new int {7, 19, 28, 39, 27, 47 };
  1257. |  Error:
  1258. |  '[' expected
  1259. |  int[] c = new int {7, 19, 28, 39, 27, 47 };
  1260. |                   ^
  1261. |  Error:
  1262. |  illegal start of expression
  1263. |  int[] c = new int {7, 19, 28, 39, 27, 47 };
  1264. |                    ^
  1265.  
  1266. jshell> int[] c =  {7, 19, 28, 39, 27, 47 };
  1267. c ==> int[6] { 7, 19, 28, 39, 27, 47 }
  1268.  
  1269. jshell> int[] d = {200, 10, 2391, 28283, 2993, 2939 }
  1270. d ==> int[6] { 200, 10, 2391, 28283, 2993, 2939 }
  1271.  
  1272. jshell> foo.a;
  1273. |  Error:
  1274. |  cannot find symbol
  1275. |    symbol:   variable foo
  1276. |  foo.a;
  1277. |  ^-^
  1278.  
  1279. jshell> Foo obj1 = new Foo(a, b);
  1280. obj1 ==> Foo@5577140b
  1281.  
  1282. jshell> Foo obj2 = new Foo(c, d);
  1283. obj2 ==> Foo@67f89fa3
  1284.  
  1285. jshell> public Foo Union(Foo obj1, Foo obj2) {
  1286.    ...>   int[] arr_a_tmp = new int[0];
  1287.    ...>   int[] arr_b_tmp = new int[0];
  1288.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1289.    ...>   for(int i = 0; i < obj1.a.length; i++) {
  1290.    ...>     if(obj2.position(obj1.a[i]) != -1)
  1291.    ...>       obj_fin.add(obj1.a[i], max(obj1.b[i], obj2.b[obj2.position(obj1.a[i])]));
  1292.    ...>   }
  1293.    ...>   return obj_fin;
  1294.    ...> }
  1295. |  replaced method Union(Foo,Foo)
  1296.  
  1297. jshell> Union(obj1, obj2);
  1298. $93 ==> Foo@6073f712
  1299.  
  1300. jshell> Foo objunion = Union(obj1, obj2);
  1301. objunion ==> Foo@3d04a311
  1302.  
  1303. jshell> obj1.a; obj1.b; obj2.a; obj2.b;
  1304. $95 ==> int[4] { 7, 19, 283, 1283 }
  1305. $96 ==> int[4] { 203, 382, 4924, 293020392 }
  1306. $97 ==> int[6] { 7, 19, 28, 39, 27, 47 }
  1307. $98 ==> int[6] { 200, 10, 2391, 28283, 2993, 2939 }
  1308.  
  1309. jshell> objunion.a; objunion.b;
  1310. $99 ==> int[2] { 7, 19 }
  1311. $100 ==> int[2] { 203, 382 }
  1312.  
  1313. jshell> public int min (int el1, int el2) {
  1314.    ...>   if(el1 > el2)
  1315.    ...>     return el2;
  1316.    ...>   else
  1317.    ...>     return el1;
  1318.    ...> }
  1319.    ...>
  1320.    ...> public Foo Intersect(Foo obj1, Foo obj2) {
  1321.    ...>   int[] arr_a_tmp = new int[0];
  1322.    ...>   int[] arr_b_tmp = new int[0];
  1323.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1324.    ...>   for(int i = 0; i < obj1.a.length; i++) {
  1325.    ...>     if(obj2.position(obj1.a[i]) != -1)
  1326.    ...>       obj_fin.add(obj1.a[i], min(obj1.b[i], obj2.b[obj2.position(obj1.a[i])]));
  1327.    ...>   }
  1328.    ...>   return obj_fin;
  1329.    ...> }
  1330. |  created method min(int,int)
  1331. |  created method Intersect(Foo,Foo)
  1332.  
  1333. jshell> Foo objintersect = Intersect(obj1,obj2);
  1334. objintersect ==> Foo@57fa26b7
  1335.  
  1336. jshell> objintersect.a; onjintersect.b;
  1337. $104 ==> int[2] { 7, 19 }
  1338. |  Error:
  1339. |  cannot find symbol
  1340. |    symbol:   variable onjintersect
  1341. |   onjintersect.b;
  1342. |   ^----------^
  1343.  
  1344. jshell> objintersect.a; objintersect.b;
  1345. $105 ==> int[2] { 7, 19 }
  1346. $106 ==> int[2] { 200, 10 }
  1347.  
  1348. jshell> public Foo Union(Foo obj1, Foo obj2) {
  1349.    ...>   int[] arr_a_tmp = new int[0];
  1350.    ...>   int[] arr_b_tmp = new int[0];
  1351.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1352.    ...>   for(int i = 0; i < obj1.a.length; i++)
  1353.    ...>     obj_fin.add(obj1.a[i], obj1.b[i]);
  1354.    ...>   for(int i = 0; i < obj1.a.length; i++)
  1355.    ...>     obj_fin.add(obj2.a[i], obj2.b[i]);
  1356.    ...>   return obj_fin;
  1357.    ...> }
  1358. |  modified method Union(Foo,Foo)
  1359.  
  1360. jshell> Foo objunion = Union(obj1, obj2);
  1361. objunion ==> Foo@4141d797
  1362.  
  1363. jshell> obj1.a; obj1.b; obj2.a; obj2.b; objintersect.a; onjintersect.b; objunion.a; objunion.b;
  1364. $109 ==> int[4] { 7, 19, 283, 1283 }
  1365. $110 ==> int[4] { 203, 382, 4924, 293020392 }
  1366. $111 ==> int[6] { 7, 19, 28, 39, 27, 47 }
  1367. $112 ==> int[6] { 200, 10, 2391, 28283, 2993, 2939 }
  1368. $113 ==> int[2] { 7, 19 }
  1369. |  Error:
  1370. |  cannot find symbol
  1371. |    symbol:   variable onjintersect
  1372. |   onjintersect.b;
  1373. |   ^----------^
  1374.  
  1375. jshell> obj1.a; obj1.b; obj2.a; obj2.b; objintersect.a; objintersect.b; objunion.a; objunion.b;
  1376. $114 ==> int[4] { 7, 19, 283, 1283 }
  1377. $115 ==> int[4] { 203, 382, 4924, 293020392 }
  1378. $116 ==> int[6] { 7, 19, 28, 39, 27, 47 }
  1379. $117 ==> int[6] { 200, 10, 2391, 28283, 2993, 2939 }
  1380. $118 ==> int[2] { 7, 19 }
  1381. $119 ==> int[2] { 200, 10 }
  1382. $120 ==> int[6] { 7, 19, 283, 1283, 28, 39 }
  1383. $121 ==> int[6] { 403, 392, 4924, 293020392, 2391, 28283 }
  1384.  
  1385. jshell> public Foo Union(Foo obj1, Foo obj2) {
  1386.    ...>   int[] arr_a_tmp = new int[0];
  1387.    ...>   int[] arr_b_tmp = new int[0];
  1388.    ...>   Foo obj_fin = new Foo(arr_a_tmp, arr_b_tmp);
  1389.    ...>   for(int i = 0; i < obj1.a.length; i++)
  1390.    ...>     obj_fin.add(obj1.a[i], obj1.b[i]);
  1391.    ...>   for(int i = 0; i < obj2.a.length; i++)
  1392.    ...>     obj_fin.add(obj2.a[i], obj2.b[i]);
  1393.    ...>   return obj_fin;
  1394.    ...> }
  1395. |  modified method Union(Foo,Foo)
  1396.  
  1397. jshell> Foo objunion = Union(obj1, obj2);
  1398. objunion ==> Foo@3339ad8e
  1399.  
  1400. jshell> obj1.a; obj1.b; obj2.a; obj2.b; objintersect.a; objintersect.b; objunion.a; objunion.b;
  1401.    ...>
  1402. $124 ==> int[4] { 7, 19, 283, 1283 }
  1403. $125 ==> int[4] { 203, 382, 4924, 293020392 }
  1404. $126 ==> int[6] { 7, 19, 28, 39, 27, 47 }
  1405. $127 ==> int[6] { 200, 10, 2391, 28283, 2993, 2939 }
  1406. $128 ==> int[2] { 7, 19 }
  1407. $129 ==> int[2] { 200, 10 }
  1408. $130 ==> int[8] { 7, 19, 283, 1283, 28, 39, 27, 47 }
  1409. $131 ==> int[8] { 403, 392, 4924, 293020392, 2391, 28283, 2993, 2939 }
  1410.  
  1411. jshell>
  1412.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement