You are given the following Java Package class, which has the ability to create
ID: 3827983 • Letter: Y
Question
You are given the following Java Package class, which has the ability to create links for a linked list. Package represent a traditional box.
public class Package {
private double width, height, length; Package successor;
Package(Package p){
width=p.width; height=p.height; length=p.length;
}
Package(double w, double h, double l){
return width*height*length; }
}
You are given a Package linked list head as a parameter of a method. You are also given an independent Package object comparee as the second parameter. Construct a new linked list that contains Package objects with equal width, height, and length of the ones in the given linked list but whose volumes are greater than the volume of comparee. Return the head of the newly constructed linked list. You cannot change the given linked list in the process.
Example: Assume that we can represent a Package object as (height, width, height) for our dis- cussion in this example. Consider that the given linked list contains the following four package objects (4, 2, 3), (5, 1, 10), (3, 5, 2), and (4, 5, 1). Now consider that comparee is (4, 7, 1). The returned linked list must contain (5, 1, 10) and (3, 5, 2).
The header of the method is provided below.
public static Package biggers(Package head, Package comparee){
}
Explanation / Answer
Hi, Please find my implementation.
public class Package {
private double width, height, length; Package successor;
Package(Package p){
width=p.width; height=p.height; length=p.length;
}
Package(double w, double h, double l){
width=w;
height=h;
length=l;
}
double getVol(){
return width*height*length;
}
public static Package biggers(Package head, Package comparee){
Package dummy = new Package(0,0,0);
Package current = dummy;;
while(head != null){
if(head.getVol() > comparee.getVol()){
current.successor = new Package(head);
current = current.successor;
}
head = head.successor;
}
return dummy.successor;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.