0

I have the following java class with constructor and getters and setters.

public class Train implements Comparable< Train >{
    private String power;      // "steam", "diesel" or "electric"
    private int numPassengers; // total number of passengers 
    private Wagon freight;     // linked list of freight wagons; does not include passenger carriages; 

    public String getPower() {
        return power;
    }
    public void setPower(String power) {
        this.power = power;
    }
    public int getNumPassengers() {
        return numPassengers;
    }
    public void setNumPassengers(int numPassengers) {
        this.numPassengers = numPassengers;
    }
    public Wagon getFreight() {
        return freight;
    }
    public void setFreight(Wagon freight) {
        this.freight = freight;
    }
    @Override
    public int compareTo(Train o) {
        return new Integer(this.getNumPassengers()).compareTo(new Integer(o.getNumPassengers()));
    }

    public Train(String power){
        setPower(power);
        setNumPassengers(0);
        setFreight(null);
    }

    public Train(String power, int pass){
        setPower(power);
        setNumPassengers(pass);
        setFreight(null);       
    }
}

And also another class, wagon which is used by the train class.

class Wagon {
    private String cargo;   // e.g. "wheat"
    private int weight;     // weight of the cargo in tonnes
    private Wagon next;     // the wagon that follows this one in the train; or null if there isn't one

    public String getCargo() {
        return cargo;
    }

    public void setCargo(String cargo) {
        this.cargo = cargo;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public Wagon getNext() {
        return next;
    }

    public void setNext(Wagon next) {
        this.next = next;
    }

    public Wagon(String cargo, int weight) {
        setCargo(cargo);
        setWeight(weight);
        setNext(null);
    }

    public Wagon(String cargo, int weight, Wagon next) {
        setCargo(cargo);
        setWeight(weight);
        setNext(next);
    }
}

I initialise a new train like so:

Train trainC2 = new Train("steam", 0);
        trainC2.setFreight(new Wagon("wheat", 400, new Wagon("wheat", 300, new Wagon("maize", 280))));

I want to write a method that takes a train as an input, iterates through the wagons, if the cargo attribute matches a supplied string, the wagon is deleted. I have written this method which creates a new linkedlist and adds the trains wagons to it. It then iterates through this list and removes any matches. I am trying to iterate again through the modified list and use the setFreight method to update the actual train object.

    public static void removeWagons(Train train, String cargo) {
        LinkedList<Wagon> wag8 = new LinkedList<Wagon>();
        wag8.add(train.getFreight());
        ListIterator<Wagon> iter = wag8.listIterator();
        Wagon currentWagon = null;

        while (iter.hasNext()) {
            currentWagon = iter.next();
            System.out.println();

            if (currentWagon.getCargo() == cargo) {
                System.out.println("wag8b4" + wag8);
                iter.remove();
                System.out.println("wag8" + wag8);
                }

            }


        while (iter.hasNext()) {
            train.setFreight(iter.next());
} 

        }

I think I'm close, but don't know how to write a method that will produce an output like:

trainC2.setFreight(new Wagon("wheat", 400, new Wagon("wheat", 300, new Wagon("maize", 280))));

Or maybe there's a better way altogether?

4
  • Confusing -- do you need to sort or make unique wagons by freight, or can your train have 3 food wagons? One way to sort is to insert sorted. Another is to bubble sort, moving wagons to the front if minimum, the resorting the remainder of the train. Commented Jun 8, 2020 at 14:02
  • One irregularity JAVA has, that C/C++ can overcome( **node ), is that the first wagon hangs from the list class, and the others hang from the wagon class, so removing/updating the first is irregular. One way to deal with this is to have a dummy wagon there (coal tender? :D ). While walking the list, you have 2 references, the prior wagon and the current wagon, so you can hang the current's next wagon on the prior wagon next. You can even return the removed wagon, a bit like a pop(). Commented Jun 8, 2020 at 14:02
  • I don't need to sort or anything, I just need to remove the wagon that has a cargo that matches a specified string. Commented Jun 8, 2020 at 14:39
  • I think it would be easier and clearer if instead of each wagon holding another wagon and the train holding only the first wagon if the train instead held an ArrayList/LinkedList of all the wagons it is holding. Then the next methods and fields in the wagon class would be removed and setFreight would turn into addFreight and removeFreight. Commented Jun 8, 2020 at 15:31

1 Answer 1

1

You are not iterating over Wagons because when you call wag8.add(train.getFreight()), now your list has only one Wagon. My suggestion is to write a method named 'hasNext()' in your Wagon class so that you can know if there is another Wagon connected to the current wagon. Something like this:

public boolean hasNext(){
    return next!=null;
}

You seem to want to delete some wagons from the middle of the train. Let's say you want to delete the third wagon. For that, you can change the 'next' reference of the second wagon to point to the fourth wagon. Then you have no third wagon!

public static void removeWagons(Train train, String cargo) {
    // First we check the freight is not the matching wagon.
    // If it is we will ignore it
    Wagon freight = train.getFreight();
    Wagon currentWagon = train.getFreight();
    while(currentWagon.getCargo().equals(cargo)){
        freight= currentWagon.next;
        currentWagon=currentWagon.next;
    }
    //We make another wagon to hold a reference to the previous wagon
    Wagon previous=currentWagon;
    //Now the first wagon is alright. So "currentWagon" don't need to be the first
    currentWagon=currentWagon.next;
    
    do{
        if (currentWagon.getCargo().equals(cargo)){
            previous.next=currentWagon.next;
            currentWagon= currentWagon.next;
            continue;
        }
        previous= currentWagon;
        currentWagon= currentWagon.next;
            
    } while(currentWagon.hasNext())
    train.setFreight(freight);
}

Some parts of code are ambiguous. So don't hesitate to ask how does it work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.