Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I got two class, and i wonder how can i make it work without the get and set met

ID: 3808234 • Letter: I

Question

I got two class, and i wonder how can i make it work without the get and set method on WordNode and the intasnce variable on WordNode must be protected.

public class WordNode {
//private instance variables
protected Word word;
protected WordNode next;
  
//constructor with word
public WordNode(Word w)
{
word=w;
next=null;
}

public Word getWord() {
return word;
}   

public WordNode getNext() {
return next;
}

public void setNext(WordNode next) {
this.next = next;
}
  
public String toString()
{
return word.toString();
}
  
}

public class WordList {
  
private WordNode head,tail; //
int size;
  
//constructor will create an empty list
public WordList()
{
size=0; //empty list , so size is 0
head=null;
tail=null;
}
  
public boolean isEmpty()
{
return size==0;
}
public void append(Word value)
{
WordNode n=new WordNode(value);
  
if(isEmpty()) //list is empty
head=tail=n;
else
{
   tail.setNext(n); //make tail node's next point to the new node
tail=n; // now update tail to be new node   
}
size++; //increment the size since a node is added
}
  
public void insert(Word w)
{
WordNode n=new WordNode(w);
if(isEmpty()) //list is empty
head=tail=n;
else
{
WordNode prev=null,current=head;
//move to a location in list till you get a node which is greater than the word w to be inserted
//keep track of previous node as we proceed
while(current!=null)
{
if(current.getWord().compareTo(w)<0)
{
prev=current;
current=current.getNext();
}
else
break;
}
  
if(prev==null) //means we have to add it to beginning of the list , so will have to update first
{
n.setNext(head); //make the new node point to present first node
head=n;//make n as the first node
}
else
{
prev.setNext(n);
n.setNext(current);
if(current==null) //the node is added as the last node, so update last
{
tail=n;
}
}

}
size++; //increment the size since a node is added
}
  

public int getSize()
{
return size;
}
  
public WordNode get(int index)
{
WordNode n=head;
for(int i=0;n!=null && i<index;i++)
{
n=n.getNext();
}
  
return n;
}
}

Explanation / Answer

HI, I have removed the getter and setters of WordNode class.

I have modified the code accordingly.

Please let me know in case of any issue.

public class WordNode {

   //private instance variables

   protected Word word;

   protected WordNode next;

   //constructor with word

   public WordNode(Word w)

   {

       word=w;

       next=null;

   }

   public String toString()

   {

       return word.toString();

   }

}

public class WordList {

   private WordNode head,tail; //

   int size;

   //constructor will create an empty list

   public WordList()

   {

       size=0; //empty list , so size is 0

       head=null;

       tail=null;

   }

   public boolean isEmpty()

   {

       return size==0;

   }

   public void append(Word value)

   {

       WordNode n=new WordNode(value);

       if(isEmpty()) //list is empty

           head=tail=n;

       else

       {

           tail.next = n;

           tail.next = n; //make tail node's next point to the new node

           tail=n; // now update tail to be new node

       }

       size++; //increment the size since a node is added

   }

   public void insert(Word w)

   {

       WordNode n=new WordNode(w);

       if(isEmpty()) //list is empty

           head=tail=n;

       else

       {

           WordNode prev=null,current=head;

           //move to a location in list till you get a node which is greater than the word w to be inserted

           //keep track of previous node as we proceed

           while(current!=null)

           {

               if(current.word.compareTo(w)<0)

               {

                   prev=current;

                   current=current.next;

               }

               else

                   break;

           }

           if(prev==null) //means we have to add it to beginning of the list , so will have to update first

           {

               n.next = head; //make the new node point to present first node

               head=n;//make n as the first node

           }

           else

           {

               prev.next = n;

               n.next = current;

               if(current==null) //the node is added as the last node, so update last

               {

                   tail=n;

               }

           }

       }

       size++; //increment the size since a node is added

   }

   public int getSize()

   {

       return size;

   }

   public WordNode get(int index)

   {

       WordNode n=head;

       for(int i=0;n!=null && i<index;i++)

       {

           n= n.next;

       }

       return n;

   }

}