In: Computer Science
Please answer method getListSize, getLast, countElement and mainly method replaceAll.
/**
* This class maintains an arbitrary length list of integers.
*
* In this version:
* 1. The size of the list is *VARIABLE* after the object is
created.
* 2. The code assumes there is at least one element in the
list.
*
* This class introduces the use of structural recursion.
*
* @author Raymond Lister
* @version May 2016
*
*/
public class ListOfNVersion03PartA
{   
private int thisNumber; // the number stored in this node
private ListOfNVersion03PartA next; // forms a linked list of
objects
private final int nodeID; // a unique ID for each object in the list
private static int nodeCount = 0; // the number of list objects that have been created
/**
* @param num the value to be stored in this object
*/
public ListOfNVersion03PartA(int num)
{
thisNumber = num;
next = null;
++nodeCount;
nodeID = nodeCount;
} // constructor(int num)
/**
* @param num the multiple values to be stored in the list, in that
order
*/
public ListOfNVersion03PartA(int [] num)
{
this(num[0]); // in this context, "this" invokes the other
constructor
for (int i=1 ; i insertLast(num[i]);
} // constructor(int [] num)
/**
* @return the number of elements stored in this list
*/
public int getListSize()
{
return 999;
} // method getListSize
/**
* @return the last element in the list
*/
public int getLast()
{
return 999;
} // method getLast
/**
* prints this object
*/
public void printNode()
{
System.out.print("[" + nodeID + "," + thisNumber + "]->");
} // method printListNode
/**
* prints the tail of a list
*/
private void printListTail()
{
printNode();
if ( next != null )
next.printListTail();
} // method printListTail
/**
* prints the contents of the list, in order from first to
last
*/
public void printList()
{
printNode();
if ( next != null )
next.printListTail();
} // method printList
/**
* This method is NOT examinable in this test.
*
* prints the contents of the list, in order from first to last,
and
* then moves the cursor to the next line
*/
public void printlnList()
{
printList();
System.out.println();
} // method printlnList
/**
* @return the number of times the element occurs in the list
*
* @param element the element to be counted
*/
public int countElement(int element)
{
return 999;
} // method countElement
/**
* @return the number of times the replacement was made
*
* @param replaceThis the element to be replaced
* @param withThis the replacement
*/
public int replaceAll(int replaceThis, int withThis)
{
return 999;
} // method replaceAll
/**
* @return a reference to the first object in the list that contains
the parameter value, or null if it is not found
*
* @param findThis the value to be found
*/
public ListOfNVersion03PartA findUnSorted(int findThis)
{
// This algorithm is known as "linear search"
if ( thisNumber == findThis )
return this;
if ( next != null )
return next.findUnSorted(findThis);
return null;
} // method findUnSorted
/**
* @return the reference to the object containing the smallest
element in the list
*/
public ListOfNVersion03PartA minRef()
{
// add and/or modify code to complete the method
ListOfNVersion03PartA minOfTail;
if ( next == null )
return this;
minOfTail = next.minRef();
if ( thisNumber <= minOfTail.thisNumber )
return this;
else
return minOfTail;
} // method minRef
/**
* Inserts an element in the last position. The pre-existing
elements in the
* list are unaffected.
*
* @param newElement the element to be inserted
*/
public void insertLast(int newElement)
{
if ( next == null )
next = new ListOfNVersion03PartA(newElement);
else
next.insertLast(newElement);
} // method insertLast
/**
* Inserts an element into a sorted list. NOTE: The pre-existing
elements in the
* list are sorted and the list remains sorted (ascending
order).
*
* @param newElement the element to be inserted
*/
public void insertSorted(int newElement)
{
System.out.println("This method is NOT part of the lab
test");
System.out.println("It will probbaly be part of the
assignment");
if ( next == null )
{
// insert some code here
}
else
{
// insert some code here
}
} // method insertLast
public static void main(String[] args)
{
int [] a = {2,3,5,2};
ListOfNVersion03PartA list = new ListOfNVersion03PartA(a);
list.printlnList();
int m = list.getListSize();
System.out.println("Size is: " + m);
int n = list.getLast();
System.out.println("Last element is: " + n);
list.printlnList();
int c1 = list.countElement(2);
int c2 = list.countElement(3);
int c3 = list.countElement(4);
System.out.println("Number of elements with a value of 2 found: " +
c1);
System.out.println("Number of elements with a value of 2 found: " +
c2);
System.out.println("Number of elements with a value of 2 found: " +
c3);
int ra1 = list.replaceAll(2,1);
System.out.println("Number of replacements of value 2 with 1 is: "
+ ra1);
list.printlnList();
int ra2 = list.replaceAll(3,4);
System.out.println("Number of replacements of value 3 with 4 is: "
+ ra2);
list.printlnList();
int ra3 = list.replaceAll(7,8);
System.out.println("Number of replacements of value 7 with 8 is: "
+ ra3);
list.printlnList();
list.findUnSorted(2);
list.findUnSorted(1).printNode();
System.out.println("");
list.findUnSorted(5).printNode();
System.out.println("");
list.minRef().printNode();
System.out.println("");
list.insertLast(4);
list.printlnList();
}
} // class ListOfNVersion03PartA
/**
* This class maintains an arbitrary length list of integers.
*
* In this version:
* 1. The size of the list is *VARIABLE* after the object is
created.
* 2. The code assumes there is at least one element in the
list.
*
* This class introduces the use of structural recursion.
*
* @author Raymond Lister
* @version May 2016
*
*/
public class ListOfNVersion03PartA {
    private int thisNumber; // the number stored
in this node
    private ListOfNVersion03PartA next; // forms a
linked list of objects
private final int nodeID; // a unique ID for each object in the list
private static int nodeCount = 0; // the number of list objects that have been created
    /**
     * @param num the value to be stored in
this object
     */
    public ListOfNVersion03PartA(int num) {
        thisNumber = num;
        next = null;
        ++nodeCount;
        nodeID = nodeCount;
} // constructor(int num)
    /**
     * @param num the multiple values to be
stored in the list, in that order
     */
    public ListOfNVersion03PartA(int[] num) {
        this(num[0]); // in this
context, "this" invokes the other constructor
        for (int i = 1;
i<num.length;i++)
           
insertLast(num[i]);
} // constructor(int [] num)
    /**
     * @return the number of elements stored in
this list
     */
    public int getListSize() {
        return nodeCount;
} // method getListSize
    /**
     * @return the last element in the
list
     */
    public int getLast() {
        if (next == null)
{
           
return thisNumber;
        } else {
           
return next.getLast();
        }
} // method getLast
    /**
     * prints this object
     */
    public void printNode() {
        System.out.print("[" +
nodeID + "," + thisNumber + "]->");
} // method printListNode
    /**
     * prints the tail of a list
     */
    private void printListTail() {
        printNode();
        if (next != null)
{
           
next.printListTail();
        }
} // method printListTail
    /**
     * prints the contents of the list, in
order from first to last
     */
    public void printList() {
        printNode();
        if (next != null)
{
           
next.printListTail();
        }
} // method printList
    /**
     * This method is NOT examinable in this
test.
     *
     * prints the contents of the list, in
order from first to last, and then
     * moves the cursor to the next line
     */
    public void printlnList() {
        printList();
       
System.out.println();
} // method printlnList
    /**
     * @return the number of times the element
occurs in the list
     *
     * @param element the element to be
counted
     */
    public int countElement(int element) {
        int count = 0;
        ListOfNVersion03PartA
current = this;
       
while(current!=null){
           
if(current.thisNumber==element)
            
count++;
           
current = current.next;
        }
        return count;
} // method countElement
    /**
     * @return the number of times the
replacement was made
     *
     * @param replaceThis the element to be
replaced
     * @param withThis the replacement
     */
    public int replaceAll(int replaceThis, int
withThis) {
        int count = 0;
        ListOfNVersion03PartA
current = this;
       
while(current!=null){
           
if(current.thisNumber==replaceThis){
            
count++;
            
current.thisNumber = withThis;
           
}
           
current = current.next;
        }
        return count;
} // method replaceAll
    /**
     * @return a reference to the first object
in the list that contains the
     * parameter value, or null if it is not
found
     *
     * @param findThis the value to be
found
     */
    public ListOfNVersion03PartA findUnSorted(int
findThis) {
// This algorithm is known as "linear search"
        if (thisNumber ==
findThis) {
           
return this;
        }
        if (next != null)
{
           
return next.findUnSorted(findThis);
        }
return null;
} // method findUnSorted
    /**
     * @return the reference to the object
containing the smallest element in
     * the list
     */
    public ListOfNVersion03PartA minRef() {
// add and/or modify code to complete the method
        ListOfNVersion03PartA
minOfTail;
        if (next == null)
{
           
return this;
        }
minOfTail = next.minRef();
        if (thisNumber <=
minOfTail.thisNumber) {
           
return this;
        } else {
           
return minOfTail;
        }
} // method minRef
    /**
     * Inserts an element in the last position.
The pre-existing elements in the
     * list are unaffected.
     *
     * @param newElement the element to be
inserted
     */
    public void insertLast(int newElement) {
        if (next == null)
{
           
next = new ListOfNVersion03PartA(newElement);
        } else {
           
next.insertLast(newElement);
        }
} // method insertLast
    /**
     * Inserts an element into a sorted list.
NOTE: The pre-existing elements in
     * the list are sorted and the list remains
sorted (ascending order).
     *
     * @param newElement the element to be
inserted
     */
    public void insertSorted(int newElement) {
        System.out.println("This
method is NOT part of the lab test");
        System.out.println("It
will probbaly be part of the assignment");
        if (next == null)
{
// insert some code here
        } else {
// insert some code here
        }
} // method insertLast
    public static void main(String[] args)
{
        int[] a = {2, 3, 5,
2};
        ListOfNVersion03PartA
list = new ListOfNVersion03PartA(a);
       
list.printlnList();
        int m =
list.getListSize();
        System.out.println("Size
is: " + m);
        int n =
list.getLast();
        System.out.println("Last
element is: " + n);
       
list.printlnList();
        int c1 =
list.countElement(2);
        int c2 =
list.countElement(3);
        int c3 =
list.countElement(4);
       
System.out.println("Number of elements with a value of 2 found: " +
c1);
       
System.out.println("Number of elements with a value of 3 found: " +
c2);
       
System.out.println("Number of elements with a value of 4 found: " +
c3);
        int ra1 =
list.replaceAll(2, 1);
       
System.out.println("Number of replacements of value 2 with 1 is: "
+ ra1);
       
list.printlnList();
        int ra2 =
list.replaceAll(3, 4);
       
System.out.println("Number of replacements of value 3 with 4 is: "
+ ra2);
       
list.printlnList();
        int ra3 =
list.replaceAll(7, 8);
       
System.out.println("Number of replacements of value 7 with 8 is: "
+ ra3);
       
list.printlnList();
       
list.findUnSorted(2);
       
list.findUnSorted(1).printNode();
       
System.out.println("");
       
list.findUnSorted(5).printNode();
       
System.out.println("");
       
list.minRef().printNode();
       
System.out.println("");
       
list.insertLast(4);
       
list.printlnList();
    }
} // class ListOfNVersion03PartA
Output
