Question
insertHead, which puts a new Dog at the head of the list, insertTail, which puts a new Dog at the tail of the list weightDiff,
-
insertHead, which puts a new Dog at the head of the list,
-
insertTail, which puts a new Dog at the tail of the list
-
weightDiff, which returns the difference between the weights of the heaviest and the lightest Dog in the list. A sled dog team is more effective if all the dogs in it have close to the same weight.
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;
}
}
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started