How to write an Applet to display a heap

  • Thread starter Thread starter DODGEVIPER13
  • Start date Start date
AI Thread Summary
The discussion centers on creating a Java applet to visualize a heap data structure. The user has successfully implemented the heap functionality but struggles with the applet aspect. They have created a simple applet that displays a blue box for testing but seek guidance on how to visually represent the heap. Key points include the necessity of repainting the applet window when elements are added or removed from the heap. For those new to applet programming, it is recommended to consult resources like Herbert Schildt's book on Java, emphasizing that hands-on experience is crucial for mastering applet development.
DODGEVIPER13
Messages
668
Reaction score
0
Ok so I have the heap part written but I don't understand how to do the Applet at all. I have figured out there is a special way an applet must be written and I wrote a simple applet that displayed a blue box just to test but I am wondering how I would get it to display a heap in the way it wants me too?


(Heap visualization) Write a Java applet that displays a heap graphically, as
shown in Figure 24.7. The applet let's you insert and delete an element from the
heap.

public class MinHeap<E extends Comparable>{
private java.util.ArrayList<E> list = new java.util.ArrayList<E>();

public MinHeap(){

}
public MinHeap(E[] objects){
for (int i = 0; i < objects.length; i++)
add(objects);
}
public void add(E newObject){
list.add(newObject);
int currentindex = list.size() - 1;

while (currentindex > 0){
int parentIndex = (currentindex-1)/2;
E p = list.get(parentIndex);

if (newObject.compareTo(p) >= 0){
break;
}
list.set(currentindex, p);
currentindex = parentIndex;

}
list.set(currentindex, newObject);
}
public E remove(){
if(list.size() == 0) return null;

E removedobject = list.get(0);
list.set(0, list.get(list.size() - 1));
list.remove(list.size() - 1);

int currentindex = 0;
while(currentindex < list.size()){
int left = 2*currentindex+1;
int right = 2*currentindex+2;

if(left >= list.size()) break;
int max = left;
if(right < list.size()){
if(list.get(max).compareTo(list.get(right)) < 0){
max=right;
}
}
if (list.get(currentindex).compareTo(list.get(max)) < 0){
E temp = list.get(max);
list.set(max, list.get(currentindex));
list.set(currentindex, temp);
currentindex = max;
}
else
break;
}
return removedobject;
}
public int getSize(){
return list.size();
}
}
 
Technology news on Phys.org
DODGEVIPER13 said:
Write a Java applet that displays a heap graphically, as
shown in Figure 24.7.
You haven't posted Figure 24.7.
DODGEVIPER13 said:
I don't understand how to do the Applet at all
Java applet designing is interesting, but often not easy. Basically when you add or remove a node from the stack, you will have to repaint/rebuild the window. Now, you have to find out how to do that. If you are new to Applets, try starting from Herbert Schildt's book on Java. The best way to learn applets, or anything in computer programming, is to try it out explicitly. Experience will be your best teacher.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
1
Views
2K
Replies
4
Views
5K
Replies
6
Views
3K
Replies
1
Views
9K
Replies
6
Views
3K
Replies
1
Views
4K
Back
Top