Min Heap of Candy — Add and Remove

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Min Heap of Candy — Add and Remove

Using the provided code (Question04.java and Candyjava), write the add and remove methods for a min heap of Candy using an array implementation. Make sure to include all provided files in your project and write your solution in the file "Question04.java". This method should preserve the properties of a min hap, and the candies are compared based on their weight. This value can be accessed through its accessor.


Solution Tests:


• Does the solution compile?

• Does the solution have your name in the comments?

• Does the solution have a high-level solution description (150-300 words) in the comments?

• Does the method preserve the properties of a Min Heap?

• Does the output match the following?


Testing the Add Method

Candy Weight: 10.0,

Candy Weight: 40.0,

Candy Weight: 20.0,

Candy Weight: 50.0,

Candy Weight: 60.0,

Candy Weight: 80.0,

Candy Weight: 30.0,

Candy Weight: 100.0,

Candy Weight: 70.0,

Candy Weight: 90.0,

Testing the Remove Method

Candy Weight: 10.0,

Candy Weight: 20.0,

Candy Weight: 30.0,

Candy Weight: 40.0,

Candy Weight: 50.0,

Candy Weight: 60.0,

Candy Weight: 70.0,

Candy Weight: 80.0,

Candy Weight: 90.0,

Candy Weight: 100.0,

//Do not alter-
public class Question04
private Candy [] heap = new Candy [10];
public int size;//First null element
public void add(Candy aData)
{
//Write your code here
}//Do not alter
//Do not alter-
public Candy remove()
{
//Write your code here
}//Do not alter
//Write any additional helper properties or methods here
//Do not alter-
public void printBreadthorder()
{
for(Candy c : this.heap)
{
if(c != nul)
System.out.print(c+", ");
else
System.out.print("null, ");
}
System.out.println();
}
Run | Debug
public static void main(String[] args)
{
System.out.println("Testing the Add Method");
Candy [] array = {
new Candy (100.0),
new Candy (80.0),
new Candy(60.0),
new Candy(40.0),
new Candy(20.0),
new Candy(10.0),
new Candy (30.0),
new Candy(50.0),
new Candy(70.0),
new Candy (90.0)};
Question04 heap = new Question04();
for(Candy c : array)
heap.add (c);
heap.printBreadthorder();
System.out.println("Testing the Remove Method");
for(int i=0;i<array.length;i++)
System.out.print(heap.remove() +", ");
}
Transcribed Image Text://Do not alter- public class Question04 private Candy [] heap = new Candy [10]; public int size;//First null element public void add(Candy aData) { //Write your code here }//Do not alter //Do not alter- public Candy remove() { //Write your code here }//Do not alter //Write any additional helper properties or methods here //Do not alter- public void printBreadthorder() { for(Candy c : this.heap) { if(c != nul) System.out.print(c+", "); else System.out.print("null, "); } System.out.println(); } Run | Debug public static void main(String[] args) { System.out.println("Testing the Add Method"); Candy [] array = { new Candy (100.0), new Candy (80.0), new Candy(60.0), new Candy(40.0), new Candy(20.0), new Candy(10.0), new Candy (30.0), new Candy(50.0), new Candy(70.0), new Candy (90.0)}; Question04 heap = new Question04(); for(Candy c : array) heap.add (c); heap.printBreadthorder(); System.out.println("Testing the Remove Method"); for(int i=0;i<array.length;i++) System.out.print(heap.remove() +", "); }
//Do not alter-
public class Candy implements Comparable<Candy>
{
private double weight;
public Candy (double aW)
{
this.setWeight(aw);
}
public double getWeight()
{
return weight;
}
public void setWeight(double weight)
{
if(weight > 0.0)
this.weight = weight;
else
this.weight = 1.0;
}
public String toString()
{
return "Candy Weight: "+this.weight;
}
public int compareTo(Candy aC)
{
if(aC
return 1;
else if(this.weight > aC.getWeight())
return 1;
else if(this.weight < aC.getWeight())
return -1;
else
= null)
return 0;
}
}
Transcribed Image Text://Do not alter- public class Candy implements Comparable<Candy> { private double weight; public Candy (double aW) { this.setWeight(aw); } public double getWeight() { return weight; } public void setWeight(double weight) { if(weight > 0.0) this.weight = weight; else this.weight = 1.0; } public String toString() { return "Candy Weight: "+this.weight; } public int compareTo(Candy aC) { if(aC return 1; else if(this.weight > aC.getWeight()) return 1; else if(this.weight < aC.getWeight()) return -1; else = null) return 0; } }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY