DanieleCalisti

IO

May 5th, 2021
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package IO;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7.  
  8. /** Classe per le funzioni di input
  9.  *
  10.  */
  11. public class IO {
  12.  
  13.     InputStreamReader input = new InputStreamReader(System.in);
  14.     BufferedReader tastiera = new BufferedReader(input);
  15.  
  16.  
  17.     /**
  18.      * @return appo, intero passato da tastiera
  19.      * @throws IOException
  20.      */
  21.     public int getInteger() throws IOException {
  22.  
  23.         int appo;
  24.  
  25.         String numeroLetto = tastiera.readLine();
  26.         appo = Integer.valueOf(numeroLetto).intValue();
  27.  
  28.         return appo;
  29.  
  30.     }
  31.  
  32.     /**
  33.      * @return appo, stringa passata da tastiera
  34.      * @throws IOException
  35.      */
  36.     public String getString() throws IOException {
  37.  
  38.         String appo;
  39.  
  40.         appo = tastiera.readLine();
  41.  
  42.         return appo;
  43.  
  44.     }
  45.  
  46.     /**
  47.      * @return appo, reale passato da tastiera
  48.      * @throws IOException
  49.      */
  50.     public float getFloat() throws IOException {
  51.  
  52.         float appo;
  53.  
  54.         String numeroLetto = tastiera.readLine();
  55.         appo = Float.valueOf(numeroLetto).floatValue();
  56.  
  57.         return appo;
  58.  
  59.     }
  60.    
  61.     /**
  62.      * @param String s, stringa da stampare
  63.      * @param Boolean aCapo, true se la stringa deve andare a capo, false altrimenti
  64.      */
  65.     public void stampa(String s, boolean aCapo)
  66.     {
  67.         if(aCapo)
  68.             System.out.println(s);
  69.         else
  70.             System.out.print(s);
  71.     }
  72.    
  73.     /**
  74.      *  @param nodi, numero di nodi del grafo
  75.      *  @return matrice, matrice di adiacenza del grafo
  76.      */
  77.     public ArrayList<ArrayList<Integer>> creaMatrice(int nodi)
  78.     {
  79.         //Creo la matrice di adiacenza
  80.         ArrayList<ArrayList<Integer>> matrice = new ArrayList<>(nodi);
  81.        
  82.         //Mi carico la matrice di adiacenza
  83.         for (int i = 0; i < nodi; i++) {
  84.            
  85.             //Creo questo array di appoggio che
  86.             //praticamente conterrĂ  il valre di ogni singola riga della matrice
  87.             ArrayList<Integer> righe = new ArrayList<>(nodi);
  88.            
  89.             for (int j = 0; j < nodi; j++)
  90.             {
  91.                 //Prendo in input il peso dal nodo i al nodo j
  92.                 int appo = 0;
  93.                 stampa("Inserisci il peso nella cella "+(i+1)+" - " +(j+1)+": ", false);
  94.                 try
  95.                 {
  96.                     appo = getInteger();
  97.                 }catch(IOException e)
  98.                 {
  99.                     System.out.print("Errore! " + e.getMessage());
  100.                 }
  101.                
  102.                 righe.add(appo);       
  103.             }
  104.            
  105.             //Aggiungo alla matrice questa riga
  106.             matrice.add(righe);
  107.         }
  108.        
  109.         return matrice;
  110.     }
  111.    
  112.     /**
  113.      *
  114.      * @return nodi, numero dei nodi del grafo
  115.      */
  116.     public int getNodi()
  117.     {
  118.         int nodi = 0;
  119.        
  120.         stampa("Inserisci numero dei nodi: ", false);
  121.         try
  122.         {
  123.             nodi = getInteger();
  124.         }catch(IOException e)
  125.         {
  126.             System.out.print("Errore! " + e.getMessage());
  127.         }
  128.        
  129.         return nodi;
  130.     }
  131.    
  132.     /**
  133.      *
  134.      * @return src, nodo a cui arrivare
  135.      */
  136.     public int getSrc()
  137.     {
  138.         int src = 0;
  139.        
  140.         stampa("Inserisci il nodo da raggiungere: ", false);
  141.         try
  142.         {
  143.             src = getInteger();
  144.         }catch(IOException e)
  145.         {
  146.             System.out.print("Errore! " + e.getMessage());
  147.         }
  148.        
  149.         return src;
  150.     }
  151.  
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment