Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A linked list question. I am working on a linked list project which is about dog

ID: 3748717 • Letter: A

Question

A linked list question.

I am working on a linked list project which is about dog objects.

I need to finish some methods which are in DogTeam.java:

1. insertHead, which puts a new Dog at the beginning of the list.

2. insertTail, which puts a new Dog at the end of the list.

3. weightDiff, which returns the difference between the weights of the heaviest and the lightest Dog in the list.

4. insertAfter, which puts a new Dog in the list after the Dog with name dogName. (Don't worry about dog objects in the list having same names.)

Dog.java where declares dog object:

public class Dog{

private String name;
private double weight;

public Dog (String name, double weight){
this.name = name;
this.weight = weight;
}

public String getName(){
return this.name;
}
  
public double getWeight(){
return this.weight;
}
  
public void setName (String name){
this.name = name;
}
  
public void setWeight (double weight){
this.weight = weight;
}
}

LLDogNode.java where declare the linked list:

public class LLDogNode{

private Dog contents;
private LLDogNode link;

public LLDogNode (Dog dog, LLDogNode link){
this.contents = dog;
this.link = link;
}

public Dog getContents(){
return contents;
}

public LLDogNode getLink(){
return link;
}

public void setContents(Dog dog){
contents = dog;
}

public void setLink (LLDogNode link){
this.link = link;
}
}

Finally, the DogTeam.java where implements methods:

public class DogTeam{

private LLDogNode head;

public DogTeam(Dog dog){

head = new LLDogNode(dog, null);

}

public void printTeam(){

LLDogNode cur = head;

int dogNumber = 1;

System.out.println("----------------");

while (cur != null){

System.out.println(dogNumber+". "+cur.getContents().getName()+", "+cur.getContents().getWeight());

cur = cur.getLink();

dogNumber += 1;

}

}

public static void main(String[] args){

DogTeam team = new DogTeam(new Dog("dog1", 60));

team.printTeam();

System.out.println("weightDiff: " + team.weightDiff());

team.insertTail(new Dog("dog0", 5));

team.insertHead(new Dog("dog2", 90));

team.printTeam();

System.out.println("weightDiff: " + team.weightDiff());

team.insertHead(new Dog("dog3", 7));

team.insertAfter(new Dog("dog4", 100), "dog2");

team.printTeam();

team.insertTail(new Dog("dog10", 205));

team.insertAfter(new Dog("dog9", 75), "dog10");

team.printTeam();

System.out.println("weightDiff: " + team.weightDiff());

}

public void insertHead(Dog dog){

// TODO(0)

// puts new node containing dog at the head of the list

  

}

public void insertTail(Dog dog){

// TODO(1)

// puts new node containing dog at the tail of the list

  

}

public double weightDiff(){

// TODO(2)

// returns difference between max and min weights of dogs in list

// pre: this list contains at least one node

return 0.0;

}

  

public void insertAfter(Dog dog, String dogName){

}

}

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Attaching only DogTeam.java as other classes are unmodified.

// DogTeam.java

public class DogTeam {

      private LLDogNode head;

      public DogTeam(Dog dog) {

            head = new LLDogNode(dog, null);

      }

      public void printTeam() {

            LLDogNode cur = head;

            int dogNumber = 1;

            System.out.println("----------------");

            while (cur != null) {

                  System.out.println(dogNumber + ". " + cur.getContents().getName()

                              + ", " + cur.getContents().getWeight());

                  cur = cur.getLink();

                  dogNumber += 1;

            }

      }

      public static void main(String[] args) {

            DogTeam team = new DogTeam(new Dog("dog1", 60));

            team.printTeam();

            System.out.println("weightDiff: " + team.weightDiff());

            team.insertTail(new Dog("dog0", 5));

            team.insertHead(new Dog("dog2", 90));

            team.printTeam();

            System.out.println("weightDiff: " + team.weightDiff());

            team.insertHead(new Dog("dog3", 7));

            team.insertAfter(new Dog("dog4", 100), "dog2");

            team.printTeam();

            team.insertTail(new Dog("dog10", 205));

            team.insertAfter(new Dog("dog9", 75), "dog10");

            team.printTeam();

            System.out.println("weightDiff: " + team.weightDiff());

      }

      public void insertHead(Dog dog) {

            // TODO(0)

            // puts new node containing dog at the head of the list

            LLDogNode newNode = new LLDogNode(dog, head);

            head = newNode;

      }

      public void insertTail(Dog dog) {

            // TODO(1)

            // puts new node containing dog at the tail of the list

            LLDogNode newNode = new LLDogNode(dog, null);

            if (head == null) {

                  // adding as head

                  head = newNode;

            } else {

                  LLDogNode temp = head;

                  // looping through list

                  while (temp.getLink() != null) {

                        temp = temp.getLink();

                  }

                  // adding to end of list

                  temp.setLink(newNode);

            }

      }

      public double weightDiff() {

            // TODO(2)

            // returns difference between max and min weights of dogs in list

            // pre: this list contains at least one node

            // variables to store highest and lowest weights

            double weightHighest = 0, weightLowest = 0;

            int count = 0; // count of dogs

            LLDogNode node = head;

            // looping through the list

            while (node != null) {

                  // finding weight of current dog

                  double weight = node.getContents().getWeight();

                  if (count == 0) {

                        // first dog, adding this weight as the highest and lowest

                        weightHighest = weight;

                        weightLowest = weight;

                  } else {

                        // checking if weight is higher than current highest weight

                        if (weight > weightHighest) {

                              // setting this as the new highest weight

                              weightHighest = weight;

                        }

                        // checking if weight is lower than current lowest weight

                        if (weight < weightLowest) {

                              // setting this as the new lowest weight

                              weightLowest = weight;

                        }

                  }

                  count++;

                  node = node.getLink();

            }

            // returning the weight diff

            return weightHighest - weightLowest;

      }

      public void insertAfter(Dog dog, String dogName) {

            LLDogNode node = head;

            // creating a new node

            LLDogNode newNode = new LLDogNode(dog, null);

            // looping until the dog with given name is found

            while (node != null) {

                  if (node.getContents().getName().equalsIgnoreCase(dogName)) {

                        // current dog has the required dogName, so adding after it

                        newNode.setLink(node.getLink());

                        node.setLink(newNode);

                        break;

                  }

                  node = node.getLink();

            }

            //if the dog is not found, new dog is not added

      }

}

//OUTPUT

----------------

1. dog1, 60.0

weightDiff: 0.0

----------------

1. dog2, 90.0

2. dog1, 60.0

3. dog0, 5.0

weightDiff: 85.0

----------------

1. dog3, 7.0

2. dog2, 90.0

3. dog4, 100.0

4. dog1, 60.0

5. dog0, 5.0

----------------

1. dog3, 7.0

2. dog2, 90.0

3. dog4, 100.0

4. dog1, 60.0

5. dog0, 5.0

6. dog10, 205.0

7. dog9, 75.0

weightDiff: 200.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote