In: Computer Science
Plz, use NETBEANS 8.1 or 8.2 to solve it. Mention every answer to it's question
Thank u
Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)
a) Create a Clock class
1. Add hour, minute, and second as attributes
2. Add a constructor and a toString() method
3. Implement the Comparable interface, and add a CompareTo() method
4. Add methods to get and set all attributes.
b) Add to MyLinkedList class the following methods:
1. Insert a node at the front of the list
2. Insert a Node in place, assuming that the list is ordered in ascending order.
3. Delete node in front
4. Delete last node
5. Method to return the size of the list
6. Method to find the node with the minimum value in the list – getMinimum()
c) Create a Test3 class to do the following in the main method:
1. Create a new List (call it clockList) of type MyLinkedList
2. Insert five Clock objects numbers to the list (not ordered).
3. Display all the clocks in the List
4. Sort the List and display it in both ascending and descending
5. Display the largest element in the list
6. Display the smallest element in the list
7. Display the size of the list
8. Search for a particular clock in the List by printing true if found or false.
9. Delete a clock from the front of the list
10. Delete a clock from the back of the list
11. Order the list in ascending order
12. Insert a new clock elements to its appropriate position in the List and display it
package listclasses;
public class Clock {
int hour;
int minute;
int second;
Clock(){
}
Clock(int hour,int minute,int second){
this.hour=hour;
this.minute=minute;
this.second=second;
}
public String tostring(){
return "hour is:"+hour+ "minute
is:"+minute +"second is:"+second;
}
}
class doublelinkedlist2 {
doublelinkedlist2 privious;
doublelinkedlist2 next;
Clock c;
public doublelinkedlist2(Clock c){
this.c=c;
}
public doublelinkedlist2(doublelinkedlist2
privious,Clock c,doublelinkedlist2 next){
this.privious=privious;
this.c=c;
this.next=next;
}
public String tostring(){
return c+"";
}
}
clockmain.java
package listclasses;
public class clockmain {
static doublelinkedlist2 head;
static int size=0;
static {
head=null;
size=0;
}
public static void main(String[] args) {
Clock c=new Clock(2,1,22);
Clock c2=new Clock(3,2,23);
Clock c3=new Clock(4,1,12);
Clock c4=new Clock(5,55,32);
Clock c5=new Clock(6,42,22);
insertfront(c);
insertfront(c2);
insertfront(c3);
insertfront(c4);
insertrear(c5);
display();
removeanywhere(1);
display();
}
public static void insertfront(Clock c){
if(head==null){
head=new
doublelinkedlist2(null,c,null);
}
else{
doublelinkedlist2 newlist=new
doublelinkedlist2(null,c,head);
head.privious=newlist;
head=newlist;
}
size++;
}
public static void insertrear(Clock c){
if(head==null)
head=new
doublelinkedlist2(null,c,null);
else{
doublelinkedlist2 current;
current=head;
while(current.next!=null){
current=current.next;
}
doublelinkedlist2 newlist=new
doublelinkedlist2(current,c,null);
current.next=newlist;
}
size++;
}
public static void removefront(){
if(head==null)
return;
head=head.next;
head.privious=null;
size--;
}
public static void removerrear(){
if(head==null)
return;
if(head.next==null){
head=null;
size--;
return;
}
doublelinkedlist2 curent=head;
while(curent.next.next!=null){
curent=curent.next;
}
curent.next=null;
size--;
}
public static void removeanywhere(int index){
if(head==null)return;
if(index<1 ||index >size)
return;
int i=1;
doublelinkedlist2
curent=head;
while(i<index){
curent=curent.next;
i++;
}
if(curent.next==null){
curent.privious.next=null;
size--;
}else
if(curent.privious==null){
curent=curent.next;
curent.privious=null;
head=curent;
size--;
}else{
curent.privious.next=curent.next;
curent.next.privious=curent.privious;
size--;
}
}
public static void insertanywhere(Clock c,int
index){
int i=1;
if(head==null)
return;
if( index<1 ||
index>size)
return;
doublelinkedlist2 curent;
curent=head;
while(i<index){
curent=curent.next;
i++;
}
if(curent.privious==null){
doublelinkedlist2 newlist=new
doublelinkedlist2(null,c,curent);
curent.privious=newlist;
head=newlist;
size++;
}
else
{
doublelinkedlist2 newlist=new
doublelinkedlist2(curent.privious,c,curent);
curent.privious.next=newlist;
curent.privious=newlist;
size++;
}
}
public static void display(){
while(head!=null){
Clock
c=(Clock)head.c;
System.out.println(c.tostring());
head=head.next;
}
System.out.println("size:"+size);
}
public static void isempty(){
if(size<=0)
System.out.println("empty");
else
System.out.println("not empty:");
}
}
output:
j
hour is:5minute is:55second is:32
hour is:4minute is:1second is:12
hour is:3minute is:2second is:23
hour is:2minute is:1second is:22
hour is:6minute is:42second is:22
size:5