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

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){

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!