Advertisement
nicoladc89

Kappa and Testing

Nov 15th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.87 KB | None | 0 0
  1. /**
  2. * La classe kappa implementa tutti i metodi che ti interessano.
  3. * La classe testing funziona in questo modo:
  4. * Visualizza un menu con varie scelte.
  5. * Se scegli di popolare il vettore, genera un nuovo oggetto Kappa con il vettore scelto
  6. * Se scegli di visualizzare l'array o di rimuovere i doppioni o di sapere se è o meno ordinato
  7. * non si generano nuovi oggetti e si agisce sull'ultimo generato, ovvero gli ultimi valori usati per
  8. * popolare l'array.
  9. * Per usare i dati previsti dai casi A e B, ho usato due costanti statiche, una per l'array A e una per l'array B
  10. */
  11. ////////////////////////////////////////////////////////////////////////////////
  12. file kappa.java
  13. ////////////////////////////////////////////////////////////////////////////////
  14.  
  15. import java.util.Scanner;
  16.  
  17. public class Kappa {
  18.  
  19.     Integer[] array;
  20.  
  21.     public Kappa(Integer[] array) {
  22.         this.array = array;
  23.     }
  24.  
  25.     public static Integer[] insertArray() {
  26.         int lenght_array;
  27.         Scanner scanner = new Scanner(System.in);
  28.         System.out.println("Inserisci numero elementi");
  29.         lenght_array = scanner.nextInt();
  30.         Integer array[] = new Integer[lenght_array];
  31.         for (int i = 0; i < lenght_array; i++) {
  32.             array[i] = scanner.nextInt();
  33.         }
  34.         return array;
  35.     }
  36.  
  37.     public void printArray() {
  38.         for (Integer i : array) {
  39.             System.out.print("[" + i + "] ");
  40.         }
  41.     }
  42.  
  43.     public boolean isSortedAscending() {
  44.         boolean b = true;
  45.         int i = 1;
  46.         while (i < array.length && b == true) {
  47.             if (array[i] < array[i - 1]) {
  48.                 b = false;
  49.             }
  50.             i++;
  51.         }
  52.         return b;
  53.     }
  54.  
  55.     public boolean isSortedDescending() {
  56.         boolean b = true;
  57.         int i = 1;
  58.         while (i < array.length && b == true) {
  59.             if (array[i] > array[i - 1]) {
  60.                 b = false;
  61.             }
  62.             i++;
  63.         }
  64.         return b;
  65.     }
  66.  
  67.     private void removeDuplicatesInOrderedArray() {
  68.         int i = 0, cont_elements = 0, j;
  69.         while (i < array.length) {
  70.             cont_elements++;
  71.             j = i + 1;
  72.             while (j < array.length && array[j].equals(array[i])) {
  73.                 j++;
  74.             }
  75.             i = j;
  76.         }
  77.         i = 0;
  78.         Integer array_temp[] = new Integer[cont_elements];
  79.         cont_elements = 0;
  80.         while (i < array.length) {
  81.             array_temp[cont_elements++] = array[i];
  82.             j = i + 1;
  83.             while (j < array.length && array[j].equals(array[i])) {
  84.                 j++;
  85.             }
  86.             i = j;
  87.         }
  88.         array = array_temp;
  89.     }
  90.  
  91.     /**
  92.      * Setta a null tutti i doppioni presenti nell'array, in questo modo tutti i
  93.      * valori non null presenti nell'array saranno privi di doppioni; poi copia
  94.      * i valori non null nell'array che così sarà privo di doppioni.
  95.      */
  96.     private void removeDuplicatesInUnorderedArray() {
  97.         int cont_elements = 0;
  98.         for (int i = 0; i < array.length; i++) {
  99.             if (array[i] != null) {
  100.                 cont_elements++;
  101.                 for (int j = i + 1; j < array.length; j++) {
  102.                     if (array[j] != null && array[j].equals(array[i])) {
  103.                         array[j] = null;
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.         Integer array_temp[] = new Integer[cont_elements];
  109.         cont_elements = 0;
  110.         for (Integer i : array) {
  111.             if (i != null) {
  112.                 array_temp[cont_elements++] = i;
  113.             }
  114.         }
  115.         array = array_temp;
  116.     }
  117.  
  118.     /**
  119.      * Rimuove i duplicati presenti nell'array, eseguendo procedure diverse in
  120.      * base all'ordinamento dell'array.
  121.      */
  122.     public void removeDuplicates() {
  123.         if (this.isSortedDescending() || this.isSortedAscending()) {
  124.             this.removeDuplicatesInOrderedArray();
  125.         } else {
  126.             this.removeDuplicatesInUnorderedArray();
  127.         }
  128.     }
  129. }
  130.      
  131.     //////////////////////////////////////////////////////////////////////////////
  132.     File Testing.java
  133.     //////////////////////////////////////////////////////////////////////////////
  134.      
  135.     import java.util.Scanner;
  136.      
  137.     public class Testing {
  138.         private static final Integer[] A = {58,10,32,21,10,32,14,56};
  139.         private static final Integer[] B = {13,27,27,32,56,56,56};
  140.        
  141.         private static void printMenu(){
  142.             System.out.println("1 - Popola array con dati caso A");
  143.             System.out.println("2 - Popola array con dati caso B");
  144.             System.out.println("3 - Popola array con dati inseriti da tastiera");
  145.             System.out.println("4 - Visualizza array");
  146.             System.out.println("5 - Elimina doppioni");
  147.             System.out.println("6 - L'array è ordinato in modo decrescente?");
  148.             System.out.println("7 - Esci");
  149.         }
  150.        
  151.         public static void main(String args[]){
  152.             Scanner scanner = new Scanner(System.in);
  153.             Kappa kappa = null;
  154.             int choice;
  155.             System.out.println("Scegli una delle possibili opzioni:");
  156.             do{
  157.                 Testing.printMenu();
  158.                 choice = scanner.nextInt();
  159.                 switch(choice){
  160.                     case 1:{
  161.                         kappa = new Kappa(Testing.A);
  162.                         System.out.println("L'array è stato popolato. Scegli un'altra opzione\n");
  163.                         break;
  164.                     }
  165.                     case 2:{
  166.                         kappa = new Kappa(Testing.B);
  167.                         System.out.println("L'array è stato popolato. Scegli un'altra opzione\n");
  168.                         break;
  169.                     }
  170.                     case 3:{
  171.                         kappa = new Kappa(Kappa.insertArray());
  172.                         System.out.println("L'array è stato popolato. Scegli un'altra opzione\n");
  173.                         break;
  174.                     }
  175.                     case 4:{
  176.                         if(kappa == null){
  177.                             System.out.println("Devi prima popolare l'array");
  178.                         }
  179.                         else{
  180.                             kappa.printArray();
  181.                             System.out.println("\nScegli un'altra opzione\n");
  182.                         }
  183.                         break;
  184.                     }
  185.                     case 5:{
  186.                         if(kappa == null){
  187.                             System.out.println("Devi prima popolare l'array");
  188.                         }
  189.                         else{
  190.                             kappa.removeDuplicates();
  191.                             System.out.println("I doppioni sono stati rimossi. Scegli un'altra opzione\n");
  192.                         }
  193.                         break;
  194.                     }
  195.                     case 6:{
  196.                         if(kappa == null){
  197.                             System.out.println("Devi prima popolare l'array");
  198.                         }
  199.                         else{
  200.                             if(kappa.isSortedDescending()){
  201.                                 System.out.println("L'array è ordinato in modo decrescente");
  202.                             }else{
  203.                                 System.out.println("L'array non è ordinato in modo decrescente");
  204.                             }
  205.                         }
  206.                         System.out.println("Scegli un'altra opzione\n");
  207.                         break;
  208.                     }
  209.                     case 7:{
  210.                         System.out.println("ARRIVEDERCI!");
  211.                         break;
  212.                     }
  213.                     default:{
  214.                         System.out.println("Scelta non valida");
  215.                     }
  216.                 }
  217.             }while(choice!=7);
  218.         }
  219.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement