Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * La classe kappa implementa tutti i metodi che ti interessano.
- * La classe testing funziona in questo modo:
- * Visualizza un menu con varie scelte.
- * Se scegli di popolare il vettore, genera un nuovo oggetto Kappa con il vettore scelto
- * Se scegli di visualizzare l'array o di rimuovere i doppioni o di sapere se è o meno ordinato
- * non si generano nuovi oggetti e si agisce sull'ultimo generato, ovvero gli ultimi valori usati per
- * popolare l'array.
- * 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
- */
- ////////////////////////////////////////////////////////////////////////////////
- file kappa.java
- ////////////////////////////////////////////////////////////////////////////////
- import java.util.Scanner;
- public class Kappa {
- Integer[] array;
- public Kappa(Integer[] array) {
- this.array = array;
- }
- public static Integer[] insertArray() {
- int lenght_array;
- Scanner scanner = new Scanner(System.in);
- System.out.println("Inserisci numero elementi");
- lenght_array = scanner.nextInt();
- Integer array[] = new Integer[lenght_array];
- for (int i = 0; i < lenght_array; i++) {
- array[i] = scanner.nextInt();
- }
- return array;
- }
- public void printArray() {
- for (Integer i : array) {
- System.out.print("[" + i + "] ");
- }
- }
- public boolean isSortedAscending() {
- boolean b = true;
- int i = 1;
- while (i < array.length && b == true) {
- if (array[i] < array[i - 1]) {
- b = false;
- }
- i++;
- }
- return b;
- }
- public boolean isSortedDescending() {
- boolean b = true;
- int i = 1;
- while (i < array.length && b == true) {
- if (array[i] > array[i - 1]) {
- b = false;
- }
- i++;
- }
- return b;
- }
- private void removeDuplicatesInOrderedArray() {
- int i = 0, cont_elements = 0, j;
- while (i < array.length) {
- cont_elements++;
- j = i + 1;
- while (j < array.length && array[j].equals(array[i])) {
- j++;
- }
- i = j;
- }
- i = 0;
- Integer array_temp[] = new Integer[cont_elements];
- cont_elements = 0;
- while (i < array.length) {
- array_temp[cont_elements++] = array[i];
- j = i + 1;
- while (j < array.length && array[j].equals(array[i])) {
- j++;
- }
- i = j;
- }
- array = array_temp;
- }
- /**
- * Setta a null tutti i doppioni presenti nell'array, in questo modo tutti i
- * valori non null presenti nell'array saranno privi di doppioni; poi copia
- * i valori non null nell'array che così sarà privo di doppioni.
- */
- private void removeDuplicatesInUnorderedArray() {
- int cont_elements = 0;
- for (int i = 0; i < array.length; i++) {
- if (array[i] != null) {
- cont_elements++;
- for (int j = i + 1; j < array.length; j++) {
- if (array[j] != null && array[j].equals(array[i])) {
- array[j] = null;
- }
- }
- }
- }
- Integer array_temp[] = new Integer[cont_elements];
- cont_elements = 0;
- for (Integer i : array) {
- if (i != null) {
- array_temp[cont_elements++] = i;
- }
- }
- array = array_temp;
- }
- /**
- * Rimuove i duplicati presenti nell'array, eseguendo procedure diverse in
- * base all'ordinamento dell'array.
- */
- public void removeDuplicates() {
- if (this.isSortedDescending() || this.isSortedAscending()) {
- this.removeDuplicatesInOrderedArray();
- } else {
- this.removeDuplicatesInUnorderedArray();
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- File Testing.java
- //////////////////////////////////////////////////////////////////////////////
- import java.util.Scanner;
- public class Testing {
- private static final Integer[] A = {58,10,32,21,10,32,14,56};
- private static final Integer[] B = {13,27,27,32,56,56,56};
- private static void printMenu(){
- System.out.println("1 - Popola array con dati caso A");
- System.out.println("2 - Popola array con dati caso B");
- System.out.println("3 - Popola array con dati inseriti da tastiera");
- System.out.println("4 - Visualizza array");
- System.out.println("5 - Elimina doppioni");
- System.out.println("6 - L'array è ordinato in modo decrescente?");
- System.out.println("7 - Esci");
- }
- public static void main(String args[]){
- Scanner scanner = new Scanner(System.in);
- Kappa kappa = null;
- int choice;
- System.out.println("Scegli una delle possibili opzioni:");
- do{
- Testing.printMenu();
- choice = scanner.nextInt();
- switch(choice){
- case 1:{
- kappa = new Kappa(Testing.A);
- System.out.println("L'array è stato popolato. Scegli un'altra opzione\n");
- break;
- }
- case 2:{
- kappa = new Kappa(Testing.B);
- System.out.println("L'array è stato popolato. Scegli un'altra opzione\n");
- break;
- }
- case 3:{
- kappa = new Kappa(Kappa.insertArray());
- System.out.println("L'array è stato popolato. Scegli un'altra opzione\n");
- break;
- }
- case 4:{
- if(kappa == null){
- System.out.println("Devi prima popolare l'array");
- }
- else{
- kappa.printArray();
- System.out.println("\nScegli un'altra opzione\n");
- }
- break;
- }
- case 5:{
- if(kappa == null){
- System.out.println("Devi prima popolare l'array");
- }
- else{
- kappa.removeDuplicates();
- System.out.println("I doppioni sono stati rimossi. Scegli un'altra opzione\n");
- }
- break;
- }
- case 6:{
- if(kappa == null){
- System.out.println("Devi prima popolare l'array");
- }
- else{
- if(kappa.isSortedDescending()){
- System.out.println("L'array è ordinato in modo decrescente");
- }else{
- System.out.println("L'array non è ordinato in modo decrescente");
- }
- }
- System.out.println("Scegli un'altra opzione\n");
- break;
- }
- case 7:{
- System.out.println("ARRIVEDERCI!");
- break;
- }
- default:{
- System.out.println("Scelta non valida");
- }
- }
- }while(choice!=7);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement