JAVA - need some help with number 2,3,4,5 public class Node { /********* Instanc
ID: 3698184 • Letter: J
Question
JAVA - need some help with number 2,3,4,5
public class Node
{
/********* Instance Data **************/
private String item1; //Data field 1
private String item2; //Data field 2
private Node next; //Link to next node
/********** Contructors **************/
public Node()
//New empty node - no data, points to NULL
{
next = null;
}
public Node(String newItem1, String newItem2)
// New node with data that points to NULL
{
item1 = newItem1;
item2 = newItem2;
next = null;
} // end constructor
public Node(String newItem1, String newItem2, Node nextNode)
// New node with data that links to Next Node
{
item1 = newItem1;
item2 = newItem2;
next = nextNode;
} // end constructor
/************ Methods ***************/
//Method to set the value of the instance variable from Class ListInfo.
//Postcondition: instance variable = x;
public void setString(String newItem1, String newItem2)
{
item1 = newItem1;
item2 = newItem2;
}
//Method to return the value of the instance variable from Class ListInfo.
//Postcondition: The value of item is returned.
public String getFirst()
{
return item1;
}
//Method to return the value of the instance variable from Class ListInfo.
//Postcondition: The value of item is returned.
public String getLast()
{
return item2;
}
public void setNext(Node changeNext)
{
next = changeNext;
}
public Node getNext()
{
return next;
}
} // end class Node
public class ListNodes
{
//Instance data
//Nodes I use to traverse the list
Node head; //points to first node
Node first; //temp position for first node
Node current; //node currently being processed
Node prev; //node previously processed
//Fields of the node
String firstName; //Student's first name
String lastName; //Student's last name
Node next; //next node in list
public ListNodes()
{
head = null; //empty list
}
public void printString( ) //print out the list
{
first = head; //store head of list
System.out.println(" ");
for (current = first; current != null; current = current.getNext())
System.out.println(current.getFirst() + " " + current.getLast());
System.out.println(" ");
}
public void deleteNode (String lastN)
{
first = head;
current = first;
if (first != null) //list has nodes
{
//check first node
if (lastN.compareToIgnoreCase(first.getLast()) == 0)
{
first = first.getNext();
}
else //check rest of list
{
while(current.getNext() != null && lastN.compareToIgnoreCase(current.getLast()) != 0)
{
prev = current;
current = current.getNext();
}
//name not found
if (current.getNext() == null && lastN.compareToIgnoreCase(current.getLast()) != 0)
{
System.out.println("Name not found");
}
//name found at end of list
else if ((current.getNext() == null && lastN.compareToIgnoreCase(current.getLast()) == 0))
{
prev.setNext (null);
}
//name found within list
else //if ( (lastN.compareToIgnoreCase(current.getLast())) = 0 )
{
prev.setNext (current.getNext());
current.setNext (null);
}
}//end else
}//end if first
head = first; //update head to new first of list
}//end delete
public void insert (String firstN, String lastN)
{
first = head; //store head of list
Node newData = new Node (lastN, firstN); //place data in new node
if (first == null) //if list is empty
{
first = newData; //first points to new node
}
else //list is not empty
{
//does new data come first
if (first.getLast().compareToIgnoreCase(newData.getLast()) > 0)
{
//insert new data at front of list and return as head
newData.setNext(first); //new node points to first node
first = newData; //head now points to new node
}// end current.getLast
else //insert in list
{
prev = first;
current = first.getNext();
while (current != null &&
current.getLast().compareToIgnoreCase(newData.getLast()) < 0) //Stop at position
{
prev = current;
current = current.getNext();
}//end while current
//if new data goes at end of list
if (current == null)
prev.setNext(newData);
else //new data goes in the middle of the list
{
newData.setNext(current);
prev.setNext(newData);
}//end else middle of list
}//end else insert in list
}//end else not empty list
head = first; //update head of list
}//end insert
}//end class
Answer 1
public class Card {
public int faceValue;
public String faceValue1;
public int SuitValue;
public String SuitValue1;
public int CardNumber;
public Card(int faceValue, String faceValue1, int suitValue,
String suitValue1, int cardNumber) {
super();
this.faceValue = faceValue;
this.faceValue1 = faceValue1;
SuitValue = suitValue;
SuitValue1 = suitValue1;
CardNumber = cardNumber;
}
public Card(Comparable c) {
// TODO Auto-generated constructor stub
}
public int getFaceValue() {
return faceValue;
}
public void setFaceValue(int faceValue) {
this.faceValue = faceValue;
}
public String getFaceValue1() {
return faceValue1;
}
public void setFaceValue1(String faceValue1) {
this.faceValue1 = faceValue1;
}
public int getSuitValue() {
return SuitValue;
}
public void setSuitValue(int suitValue) {
SuitValue = suitValue;
}
public String getSuitValue1() {
return SuitValue1;
}
public void setSuitValue1(String suitValue1) {
SuitValue1 = suitValue1;
}
public int getCardNumber() {
return CardNumber;
}
public void setCardNumber(int cardNumber) {
CardNumber = cardNumber;
}
@Override
public String toString() {
return "Card [faceValue=" + faceValue + ", faceValue1=" + faceValue1
+ ", SuitValue=" + SuitValue + ", SuitValue1=" + SuitValue1
+ ", CardNumber=" + CardNumber + ", getFaceValue()="
+ getFaceValue() + ", getFaceValue1()=" + getFaceValue1()
+ ", getSuitValue()=" + getSuitValue() + ", getSuitValue1()="
+ getSuitValue1() + ", getCardNumber()=" + getCardNumber()
+ "]";
}
public Comparable getData() {
// TODO Auto-generated method stub
return null;
}
public Card getNext() {
// TODO Auto-generated method stub
return null;
}
public void setNext(Card temp) {
// TODO Auto-generated method stub
}
}
Answer 2:
public class LinkedListForDeckOfCards {
private Card first = null;
private Card current = null;
private Card previous = null;
public boolean isEmpty(){
return true;
}
public boolean contains(Comparable item){
current = first;
previous = null;
while ((current != null)&&(current.getData().compareTo(item) < 0)){
previous = current;
current = current.getNext();
}
return ((current != null) && (current.getData().compareTo(item) == 0));
}
public int size(){
int count = 0;
current = first;
previous = null;
while (current != null){
previous = current;
current = current.getNext();
count++;
}
return count;
}
public void add(Comparable c){
Card temp = new Card(c);
if (previous == null){
first = temp;
}else{
previous.setNext(temp); }
temp.setNext(current);
current = temp;
}
public void removecard(Comparable c){
if (previous == null){
first = first.getNext();
}else{
current = current.getNext();
if (previous == null){
first = current;
}else{
previous.setNext(current);
}
}
}
public void clearCard(){
first = null;
}
public void print(){
Card current = first;
while (current != null){
System.out.println(current.getData());
current = current.getNext();
}
}
}
Explanation / Answer
public class LinkedList { private Node first; private Node current; private Node last; private int currentIndex; private int numElements; public LinkedList() { this.first = null; this.last = null; this.numElements = 0; this.current = null; this.currentIndex = -1; } private class Node { Node next; Node previous; Object data; } public boolean hasNext() { return (current != null && current.next != null); } public Object next() { if (!this.hasNext()) { throw new IllegalStateException("No next"); } current = current.next; return current.data; } public boolean hasPrevious() { return (current != null && current.previous != null); } public Object previous() { if (!this.hasPrevious()) { throw new IllegalStateException("No previous"); } current = current.previous; return current.data; } int nextIndex() { int index = numElements; if (hasNext()) { index = this.currentIndex + 1; } System.out.println(index + "The current index is " + current); return index; } int previousIndex() { int index = -1; if (hasPrevious()) { index = this.currentIndex - 1; } System.out.println(index + "The current index is " + current); return index; } public void set(Object o) { if (this.current == null) { throw new IllegalStateException("No node found, cannot set."); } current.data = o; } public int size() { return numElements; } public void add(Object o) { Node newNode = new Node(); newNode.data = o; if (first == null) { first = newNode; last = newNode; newNode.next = null; } else if (first != null) { if (current == null) { newNode.previous = null; newNode.next = first; first.previous = newNode; first = newNode; } else if (current == last) { newNode.previous = current; newNode.next = null; current.next = newNode; last = newNode; } else { newNode.previous = current; newNode.next = current.next; current.next.previous = newNode; current.next = newNode; } } current = newNode; numElements++; currentIndex++; } public void remove() { if (current != null) { if (current == first && current == last) { first = null; last = null; } else if (current == last) { current.previous = null; last = current.previous; } else if (current == last) { current.previous.next = null; last = current.previous; } else { current.previous.next = current.next; current.next.previous = current.previous; } current = current.next; numElements--; } } } import java.util.Scanner; public class LinkedListTest { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String name; int index; LinkedList listOne = new LinkedList(); listOne.add(object o); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.