Don't like ads? PRO users don't see any ads ;-)

UtilitaStrade.java

By: heavenriver on Jun 22nd, 2012  |  syntax: Java  |  size: 1.24 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.   * Supports extra operations with all types of roads in the previously defined classes.
  3.   */
  4.  
  5. import java.util.*;
  6.  
  7. public class UtilitaStrade
  8.        
  9.         {
  10.        
  11.         /**
  12.           * Given a list of roads, returns the total length in number of kilometres.
  13.           * @param l The list of roads.
  14.           * @return The total number of kilometres of all roads in the list.
  15.           */
  16.        
  17.         public static double lunghezzaTotale(List<Strada> l)
  18.                 {
  19.                 int max = l.size();
  20.                 double tot = 0.0;
  21.                 for(int i = 0; i < max; i++)
  22.                         tot += l.get(i).getLunghezzaKm();
  23.                 return tot;
  24.                 }
  25.        
  26.         /**
  27.           * Given a list of roads, returns a list containing all the cities they cross.
  28.           * @param l The list of roads.
  29.           * @return The list of cities crossed by all roads in the input list.
  30.           */
  31.        
  32.         public static List<String> elencoCitta(List<Strada> l)
  33.                 {
  34.                 List<String> out = new LinkedList<String>();
  35.                 int max = l.size();
  36.                 for(int i = 0; i < max; i++)
  37.                         {
  38.                         if(l.get(i) instanceof StradaUrbana)
  39.                                 {
  40.                                 StradaUrbana s = (StradaUrbana)l.get(i);
  41.                                 out.add(s.citta());
  42.                                 }
  43.                         else if(l.get(i) instanceof StradaExtraurbana)
  44.                                 {
  45.                                 StradaExtraurbana s = (StradaExtraurbana)l.get(i);
  46.                                 out.addAll(s.cittaAttraversate());
  47.                                 }
  48.                         else;
  49.                         }
  50.                 return out;
  51.                 }
  52.        
  53.         }