How to write an Applet to display a heap

  • Thread starter DODGEVIPER13
  • Start date
In summary, the conversation discusses creating a Java applet that displays a heap graphically, with the ability to insert and delete elements. The speaker is struggling with understanding how to design an applet and is encouraged to practice and learn from resources such as a Java book.
  • #1
DODGEVIPER13
672
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
  • #2
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.
 

1. What is an applet?

An applet is a type of program that runs within a web browser. It is written in the Java programming language and is typically used to add interactive features to web pages.

2. What is a heap?

A heap is a specialized data structure commonly used in computer programming. It is a type of tree-based data structure that is used to efficiently store and retrieve data in a specific order.

3. How do I write an applet to display a heap?

To write an applet to display a heap, you will need to use the Java programming language and a development environment such as Eclipse or NetBeans. You will also need to have a basic understanding of data structures and algorithms, as well as the Java AWT (Abstract Window Toolkit) or Swing library for creating graphical user interfaces.

4. Are there any specific steps I need to follow?

Yes, there are specific steps you can follow to write an applet to display a heap. These steps include creating a new Java project, importing the necessary libraries, defining the applet class, creating a graphical user interface, and implementing the heap data structure to store and display data.

5. Are there any resources or tutorials available to help me write an applet to display a heap?

Yes, there are many online resources and tutorials available to help you write an applet to display a heap. You can find step-by-step guides, sample code, and video tutorials on websites such as Oracle, Java Tutorials, and YouTube. Additionally, there are many online forums and communities where you can ask for help and guidance from other developers.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top