Question

In: Computer Science

Please answer method getListSize, getLast, countElement and mainly method replaceAll. /** * This class maintains an...

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

Solutions

Expert Solution


/**
* 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


Related Solutions

Please add the following method as a part of the UnorderedList class definition: •print_list: the method...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method prints the elements of the list using the same format as a Python list (square brackets and commas as separators). In the main function of the modified UnorderedList.py, please test the new method to demonstrate that it works as expected. Please leave comments in the code to show what is done UnorderList.py file # Implementation of an Unordered List ADT as a linked list....
please answer as text You are given a Node class and a List class: public class...
please answer as text You are given a Node class and a List class: public class Node {        int           data;        Node       next;        Node(int d, Node n){             data = d;             next = n;       } } public class List{        Node header; } Write a java method insertNodeBefore( ) which takes two integers: int ref which is the data of the node that we need to insert a new node before, and int d which is...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
Task : (Please Answer in C#) Class Polynomials The class Polynomials is a collection of polynomials...
Task : (Please Answer in C#) Class Polynomials The class Polynomials is a collection of polynomials of either implementation stored in an instance of the generic library class List. class Polynomials { private List L; // Creates an empty list L of polynomials public Polynomials ( ) { … } // Retrieves the polynomial stored at position i in L public Polynomial Retrieve (int i) { … } // Inserts polynomial p into L public void Insert (Polynomial p) {...
/** * This class maintains an arbitrary length list of integers. * * In this version:...
/** * 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 ListOfNVersion03PartB {    private int thisNumber; // the number stored in this node...
/** * This class maintains an arbitrary length list of integers. * * In this version:...
/** * This class maintains an arbitrary length list of integers. * * In this version: * 1. The size of the list is fixed after the object is created. * 2. The code assumes there is at least one element in the list. * * This class introduces the use of loops. * * @author Raymond Lister * @version September 2015 * */ public class ListOfNVersion02PartB {    public int[] list; // Note: no "= {0, 1, 2, 3}"...
what are some examples of class merger and class expansion (please answer in psychology or behavioral...
what are some examples of class merger and class expansion (please answer in psychology or behavioral analysis)
Consider the following class and the main method below. Trace the code, then answer the questions...
Consider the following class and the main method below. Trace the code, then answer the questions on the right. public class SomeClass { private String aName; private int aNumber; private boolean amAwesome; public SomeClass(String name, int number){ aName = name; aNumber = number; amAwesome = true; } public SomeClass(String name, int number, boolean awesome){ aName = name; aNumber = number; amAwesome = awesome; } public void methodAwesome(int number){ if(amAwesome) aNumber += number - 5; amAwesome = !amAwesome; } public int...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
java please        * implement zip method in Q1 class to merge two linkedlists into...
java please        * implement zip method in Q1 class to merge two linkedlists into one.        * The elements in the result are made by concatenating elements in different        * linkedlists one after one. The order of the elements matters.        * For example, merge(list1, list2) should return [1,5,2,4,3,3,4,2,5,1].        * You can assume that arr1 and arr2 has the same size.        * HINT: You should use ListIterator to make it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT