1. LinkedList: o Create an empty list, adding to the front or rear of the list.
ID: 3826289 • Letter: 1
Question
1. LinkedList:
o Create an empty list, adding to the front or rear of the list.
o Count the number of nodes in the list, delete element at the beginning or end of the list, search the list for a specific item
2. Layout managers:
o FlowLayout, GridLayout, GridBagLayout, BorderLayout, GridBagConstraints, Insets
3. Event listeners:
o WindowAdapter, ActionListener, MouseAdapter, MouseMotionListener, HyperlinkListener
4. Frames
o Using JFrame
o Creating frames and adding menus, and menuitems,
o The difference of Menu, MenuItem, MenuBar
5. The difference of JTField , JTextArea and JEditorPane
6. Be able to create a frame using JFrame
7. Be able to place menus onto the frame
8. Be able to place menu items onto the frame
9. Be able to create anonymous event objects
Example questions
1. What happens when you place a single button in the center of a container that uses BorderLayout manager?
2. What happens when you place multiple buttons in the south of a container that uses BorderLayout manager?
3. What event method is used to register window event onto a JFrame object?
4. Name two methods found in the WindowAdapter class.
1. Given that Flow represents a class that is a subclass of the class JFrame. Trace the code below and describe the output.
String arr[] = {“One”, “Two”, “Three”};
Flow f = new Flow(“Test”)
Container c = f.getContentPane();
c.setLayout(new FlowLayout(3));
for (int i = 0; I < arr.length; i++)
c.add(new JButton(arr[i]));
f.setSize(400, 300);
f.setVisible(true);
Explanation / Answer
Hi, Please find my implementation of Q1.
Please repost others in separate post.
public class LinkedList
{
private Node head; // head of list
/* Linked list Node*/
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
/* Inserts a new Node at front of the list. */
public void addFirst(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
public int countNode(){
Node t = head;
int count = 0;
while(t != null){
count++;
t = t.next;
}
return count;
}
public void deleteFront(){
if(head != null)
head = head.next;
}
public void removeLast(){
if(head.next == null){
head = null;
}else{
Node t = head;
while(t.next.next != null){
t = t.next;
}
t.next = null; // removing last
}
}
/* Appends a new node at the end. This method is
defined inside LinkedList class shown above */
public void addLast(int new_data)
{
/* 1. Allocate the Node &
2. Put in the data
3. Set next as null */
Node new_node = new Node(new_data);
/* 4. If the Linked List is empty, then make the
new node as head */
if (head == null)
{
head = new Node(new_data);
return;
}
/* 4. This new node is going to be the last node, so
make next of it as null */
new_node.next = null;
/* 5. Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
return;
}
/* This function prints contents of linked list starting from
the given node */
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
System.out.println();
}
/* Driver program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
/* Start with the empty list */
LinkedList llist = new LinkedList();
llist.addFirst(4);
llist.printList();
llist.addFirst(5);
llist.printList();
llist.addLast(6);
llist.printList();
llist.addLast(7);
llist.printList();
llist.deleteFront();
llist.printList();
llist.removeLast();
llist.printList();
}
}
/*
Sample run:
4
5 4
5 4 6
5 4 6 7
4 6 7
4 6
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.