aiuto ho bisogno di una mano per verificare la correttezza alcuni metodi ricorsivi.
Nel seguente codice i metodi getkilometer, getprice e gettime sono definiti sia iterativamente che ricorsivamente, chi mi sa dire si la versione ricorsiva è corretta???
Grazie
codice:import java.util.Iterator; class ItineraryStep extends Itinerary implements Iterator<ItineraryStep> { protected Connection step; protected Itinerary next; public ItineraryStep() { step = null; next = Itinerary.makeEmpty(); } public ItineraryStep(Connection step) { this(); this.step = step; } @Override public boolean hasNext() { return (next != null && !(next instanceof EmptyItinerary)); } @Override public ItineraryStep next() { ItineraryStep result = null; if (hasNext()) { result = (ItineraryStep) next; } return result; } @Override public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException("remove"); } public Itinerary add(Connection c) throws InvalidItinerary { ItineraryStep tratta = this; if (next instanceof EmptyItinerary && step == null) { step = c; } else { ItineraryStep nextStep = new ItineraryStep(c); while (hasNext()) { if (tratta.step != null && tratta.step.to.equalsIgnoreCase(c.to)) { throw new InvalidItinerary("Città già presente " + c.to); } tratta = tratta.next(); } if (tratta.step != null && !tratta.step.to.equals(c.from)) { throw new InvalidItinerary("Buco nell'itinerario da " + tratta.step.to + " a " + c.from); } tratta.next = nextStep; } return this; } public float getKilometer() { float km = 0; ItineraryStep tratta = this; km += tratta.step.km; while (tratta.hasNext()) { tratta = tratta.next(); km += tratta.step.km; } return km; } ------------------ public float getKilometer() { if(!this.hasNext()) return this.step.km; return this.step.km + getKilometer(this.next()); } public float getKilometer (ItineraryStep i1) { if(!i1.hasNext()) return i1.step.km; return i1.step.km + getKilometer(i1.next()); } ------------------ public float getPrice() { float price = 0; ItineraryStep tratta = this; if (tratta.step instanceof Motorway) { price += ((Motorway) tratta.step).price; } while (tratta.hasNext()) { tratta = tratta.next(); if (tratta.step instanceof Motorway) { price += ((Motorway) tratta.step).price; } } return price; } ------------------ public float getPrice() { if(!this.hasNext() && this instanceof Motorway) return this.step.price; return this.step.km + getPrice(this.next()); } public float getPrice(ItineraryStep i1) { if(!i1.hasNext() && i1 instanceof Motorway) return i1.step.price; return i1.step.price + getPrice(i1.next()); } ------------------ public int getTime() { int time = 0; ItineraryStep tratta = (ItineraryStep) this; time += tratta.step.time; while (tratta.hasNext()) { tratta = tratta.next(); time += tratta.step.time; } return time; } ------------------ public int getTime() { if(!this.hasNext()) return this.step.km; return this.step.km + getTime(this.next()); } public int getTime(ItineraryStep i1) { if(!i1.hasNext()) return i1.step.time; return i1.step.time + getTime(i1.next()); } ------------------ @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("ItineraryStep [step="); builder.append(step); builder.append(", next="); builder.append(next); builder.append("]"); return builder.toString(); } }

Rispondi quotando