
UtilitaStrade.java
By:
heavenriver on
Jun 22nd, 2012 | syntax:
Java | size: 1.24 KB | hits: 19 | expires: Never
/**
* Supports extra operations with all types of roads in the previously defined classes.
*/
import java.util.*;
public class UtilitaStrade
{
/**
* Given a list of roads, returns the total length in number of kilometres.
* @param l The list of roads.
* @return The total number of kilometres of all roads in the list.
*/
public static double lunghezzaTotale(List<Strada> l)
{
int max = l.size();
double tot = 0.0;
for(int i = 0; i < max; i++)
tot += l.get(i).getLunghezzaKm();
return tot;
}
/**
* Given a list of roads, returns a list containing all the cities they cross.
* @param l The list of roads.
* @return The list of cities crossed by all roads in the input list.
*/
public static List<String> elencoCitta(List<Strada> l)
{
List<String> out = new LinkedList<String>();
int max = l.size();
for(int i = 0; i < max; i++)
{
if(l.get(i) instanceof StradaUrbana)
{
StradaUrbana s = (StradaUrbana)l.get(i);
out.add(s.citta());
}
else if(l.get(i) instanceof StradaExtraurbana)
{
StradaExtraurbana s = (StradaExtraurbana)l.get(i);
out.addAll(s.cittaAttraversate());
}
else;
}
return out;
}
}