i need theses questions to be solved in a simple java language using netbeans ,
ID: 3829104 • Letter: I
Question
i need theses questions to be solved in a simple java language using netbeans , plz use the same variables that i decleared and if u creat one make it understandable
I solved some parts of it i commented the questionS
THE QUESTIONS THAT I COMMENTED AND THERE IS SPACE UNDER IT , NEEDED TO BE SOLVE
here r my classes and the test class
CLASS NODE
package Problem1;
public class Node {
private Node prev;
private Comparable data;
private Node next;
public Node(Comparable newData) {
this(newData, null);
}
public Node(Comparable newData, Node n) {
data = newData;
next = n;
}
public Node(Node p, Comparable newData, Node n) {
prev = p;
data = newData;
next = n;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
public Object getData() {
return data;
}
public void setData(Comparable data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
CLASS MYLINKEDLIST
package Problem1;
public class MyLinkedList {
private Node head;
public void addToEnd(Comparable data) {
Node newNode = new Node(data);
Node current = head;
if (head == null) {
head = newNode;
} else {
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
}
//1. Insert a node at the front of the list
public void addFirst(Comparable data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
newNode.setNext(head);
head = newNode;
}
}
//2. Insert a Node in place, assuming that the list is ordered in ascending order.
public void insertAt(Comparable x, int pos) throws Exception {
Node current = head;
Node newNode = new Node(x);
if (pos < 0 || pos > size()) {
throw new Exception();
}
if (pos == 0) {
newNode.setNext(head);
head = newNode;
} else {
for (int i = 1; i <= pos - 1; i++) {
current = current.getNext();
}
newNode.setNext(current.getNext());
current.setNext(newNode);
}
}
//3. Delete node in front
public Node deleteFirstNode() {
if (head == null) {
return null;
}
Node temp = head;
head = head.getNext();
return temp;
}
//4. Delete last node
public Node deleteLastNode() {
Node current = head;
Node prev = null;
// case 1 ... list is empty
if (head == null) {
return null;
}
// case 2 .. list has only one element .. no previous
if (size() == 1) {
return deleteFirstNode();
}
// case 3 .. list has more than one element
while (current.getNext() != null) {
prev = current;
current = current.getNext();
}
prev.setNext(null);
return current;
}
//5. Method to return the size of the list
public int size() {
int size = 0;
for (Node n = head; n.getNext() != null; n = n.getNext()) {
size++;
}
return size;
}
//6. Method to find the node with the minimum value in the list – getMinimum()
//display the linked list
public void display() {
Node current = head;
while (current != null) {
System.out.println(current.getData().toString());
current = current.getNext();
}
}
public void DeletAtSpecific(int pos) {
Node current = head;
if (pos == 0) {
head = current.getNext();
} else {
for (int i = 1; i <= pos - 1; i++) {
current = current.getNext();
}
current.setNext(current.getNext().getNext());
}
}
}
CLASS CLOCK
package Problem1;
public class Clock implements Comparable {
//1. Add hour, minute, and second as attributes
private int hour;
private int minute;
private int second;
@Override
public int compareTo(Object t) {
Clock other = (Clock) t;
if (this.hour == other.hour && this.minute == other.minute && this.second == other.second) {
return 0;
}
if (this.hour < other.hour) {
return -1;
} else if (this.hour > other.hour) {
return 1;
} else if (this.minute < other.minute) {
return -1;
} else if (this.minute > other.minute) {
return 1;
} else if (this.second < other.second) {
return -1;
} else {
return 1;
}
}
//2. Add a constructor and a toString() method
Clock() {
this.hour = 0;
this.minute = 0;
this.second = 0;
}
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;
}
//4. Add methods to get and set all attributes.
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
}
CLASS MAIN METHOD TEST
package Problem1;
public class Test3 {
public static void main(String[] args) throws Exception {
//Create a new List (call it clockList) of type MyLinkedList
MyLinkedList Clock = new MyLinkedList();
Clock c1 = 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);
//Insert five Clock objects numbers to the list (not ordered).
Clock.addFirst(c3);
Clock.addToEnd(c4);
Clock.addFirst(c2);
Clock.addFirst(c5);
Clock.addToEnd(c1);
//3. Display all the clocks in the List
Clock.display();
//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
Clock.size();
//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
Clock.deleteFirstNode();
//10. Delete a clock from the back of the list
Clock.deleteLastNode();
//11. Order the list in ascending order
//12. Insert a new clock elements to its appropriate position in the List and display it
Clock c6 = new Clock(3, 06, 29);
Clock.insertAt(c6, 3);
}
}
Explanation / Answer
package problem1;
public class Node {
private Node prev;
private Comparable data;
private Node next;
public Node(Comparable newData) {
this(newData, null);
}
public Node(Comparable newData, Node n) {
data = newData;
next = n;
}
public Node(Node p, Comparable newData, Node n) {
prev = n;
data = newData;
next = n;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
public Comparable getData() {
return data;
}
public void setData(Comparable data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
package problem1;
public class MyLinkedList {
private Node head;
public void addToEnd(Comparable data) {
Node newNode = new Node(data);
Node current = head;
if(head == null) {
head = newNode;
} else {
while(current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
}
public void addFirst(Comparable data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
} else {
newNode.setNext(head);
head = newNode;
}
}
public void insertAt(Comparable data, int pos) throws Exception {
Node current = head;
Node newNode = new Node(data);
if(pos < 0 || pos > size()) {
throw new Exception();
}
if(pos == 0) {
newNode.setNext(head);
head = newNode;
}
else {
for(int i = 1; i < pos; i++) {
current = current.getNext();
}
newNode.setNext(current.getNext());
current.setNext(newNode);
}
}
public Node deleteFirstNode() {
if(head == null)
return null;
Node temp = head;
head = head.getNext();
return temp;
}
public Node deleteLastNode() {
Node current = head;
Node prev = null;
if(head == null)
return null;
if(size() == 1)
return deleteFirstNode();
while(current.getNext() != null) {
prev = current;
current = current.getNext();
}
prev.setNext(null);
return current;
}
public void deleteAtSpecific(int pos) {
Node current = head;
if(pos == 0)
head = current.getNext();
else {
for(int i = 1; i <= pos; i++) {
current = current.getNext();
}
current.setNext(current.getNext().getNext());
}
}
public int size() {
int size = 0;
for(Node n = head; n != null; n = n.getNext())
size++;
return size;
}
public Node getMinimum() {
Node current = head;
Node minNode = current;
if(head == null)
return null;
else {
while(current.getNext() != null) {
if(current.getNext().getData().compareTo(minNode.getData()) == -1)
minNode = current.getNext();
current = current.getNext();
}
}
return minNode;
}
public Node getMaximum() {
Node current = head;
Node maxNode = current;
if(head == null)
return null;
else {
while(current.getNext() != null) {
if(current.getNext().getData().compareTo(maxNode.getData()) == 1)
maxNode = current;
current = current.getNext();
}
}
return maxNode;
}
public void sortInAsc() {
for (Node p = head; p != null; p = p.getNext()) {
for (Node q = p.getNext(); q != null; q = q.getNext())
if (p.getData().compareTo(q.getData()) > 0) {
Node temp = p;
p.setData(q.getData());
q.setData(temp.getData());
}
}
}
public void sortInDesc() {
for (Node p = head; p != null; p = p.getNext()) {
for (Node q = p.getNext(); q != null; q = q.getNext())
if (p.getData().compareTo(q.getData()) < 0) {
Node temp = p;
p.setData(q.getData());
q.setData(temp.getData());
}
}
}
public boolean search(Comparable data) {
for (Node current = head; current != null; current = current.getNext()) {
if(current.getData().compareTo(data) == 0)
return true;
}
return false;
}
public void display() {
Node current = head;
while(current != null) {
System.out.println(current.getData().toString());
current = current.getNext();
}
}
}
package problem1;
public class Clock implements Comparable {
private int hour;
private int minute;
private int second;
public Clock() {
this.hour = 0;
this.minute = 0;
this.second = 0;
}
public Clock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
@Override
public int compareTo(Object t) {
Clock other = (Clock)t;
if(this.hour < other.hour)
return -1;
else if(this.hour > other.hour)
return 1;
else if(this.minute < other.minute)
return -1;
else if(this.minute > other.minute)
return 1;
if(this.second < other.second)
return -1;
else if(this.second > other.second)
return 1;
return 0;
}
@Override
public String toString() {
return "hour is:" + hour +
" minute is:" + minute +
" second is:" + second;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
}
package problem1;
public class Test {
public static void main(String[] args) throws Exception {
MyLinkedList clock = new MyLinkedList();
Clock c1 = 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);
clock.addFirst(c3);
clock.addToEnd(c4);
clock.addFirst(c2);
clock.addFirst(c5);
clock.addToEnd(c1);
clock.display();
System.out.println("Maximum:" + clock.getMaximum().getData().toString());
System.out.println("Minimum:" + clock.getMinimum().getData().toString());
System.out.println("Size:" + clock.size());
System.out.println(clock.search(c3));
System.out.println("First Node:" + clock.deleteFirstNode().getData().toString());
System.out.println("Last Node:" + clock.deleteLastNode().getData().toString());
System.out.println("Sorted in Asc");
clock.sortInAsc();
clock.display();
Clock c6 = new Clock(3, 6, 29);
clock.insertAt(c6, 3);
clock.display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.