Question

In: Computer Science

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

/**
* This constructor initializes the list to the same values
* as in the parameter.
*
* @param element the initial elements for the list
*/
public ListOfNVersion03PartB(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 ListOfNVersion03PartB(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 first element in the list
*/
public int getFirst()
{
return thisNumber;
  
} // method getFirst
  
/**
* prints this object
*/
public void printNode()
{
System.out.print("[" + nodeID + "," + thisNumber + "]->");

} // method printListNode

/**
* stringifies this object
*/
public String toStringNode()
{
return "[" + nodeID + "," + thisNumber + "]->";

} // method toStringNode


/**
* form a string from the tail of a list
*/
private String toStringTail()
{
String s = toStringNode();

if ( next != null )
s = s + next.toStringTail();

return s;

} // method toStringTail

/**
* @return A summary of the contents of the whole list.
*/
public String toString()
{
String s = toStringNode();

if ( next != null )
s = s + next.toStringTail();

return s;

} // method toString

/**
* @return the sum of the elements of the array
*/
public int sum()
{
if( next == null )
return thisNumber;
else
return thisNumber + next.sum();

} // method sum

/**
* @return the number of times the replacement was made (i.e. 0 or 1)
*
* @param replaceThis the element to be replaced
* @param withThis the replacement
*/
public int replaceOnce(int replaceThis, int withThis)
{

return 999;

} // method replaceOnce

/**
* @return the value of the smallest element in the list
*/
public int minVal()
{
return 999;

} // method minVal

/**
* Inserts an element in the first position. The elements already in the
* list are pushed up one place.
*
* @param newElement the element to be inserted
*/
public void insertFirst(int newElement)
{
ListOfNVersion03PartB temp = new ListOfNVersion03PartB(thisNumber);
temp.next = next;
next = temp;
thisNumber = newElement;

} // method insertFirst

/**
* 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)
{
// add and/or modify code to complete the method

if ( next == null )
next = new ListOfNVersion03PartB(newElement);
else
next.insertLast(newElement);

} // method insertLast

/**
* No change if there is only one element in the list. Otherwise, it moves up one place all
* elements in the list, by overwriting each "thisNumber" with "next.thisNumber", thus
* removing the first value in the list. The last node in the list is removed.
*/
public void shuffleUp()
{
System.out.println("This method is NOT part of the lab test");
System.out.println("It will probably be part of the assignment");
  
} // method shuffleUp

/*
* @return the number of deletions made (i.e. 0 or 1)
*
* @param element the element to be deleted
* @param predecessor a reference to the previous node in the linked list
* i.e. the node where "next" points to this node
*/
private int delete2(int element, ListOfNVersion03PartB predecessor)
{
System.out.println("This method is NOT part of the lab test");
System.out.println("It will probbaly be part of the assignment");
return 99; // delete this line and complete the method for the assignment

} // method delete2

/**
* @return the number of deletions made (i.e. 0 or 1)
*
* @param element the element to be deleted
*/
public int delete(int element)
{
if ( (thisNumber == element) && (next == null) )
{
System.out.println("Error when attempting to delete '" + element + "'. The list must contain at least one element");
return 0;
}

if ( thisNumber == element )
{
// if thisNumber == element is true then the first "if" statement implies that next != null
thisNumber = next.thisNumber;
next = next.next;
return 1;
}

if ( next == null )
{
// if next == null is true then the first "if" statement implies that thisNumber != element
return 0;
}

return next.delete2(element, this);

} // method delete
public static void main(String[] args)
{
int [] a = {3,7,1,7};
ListOfNVersion03PartB list = new ListOfNVersion03PartB(a);
System.out.println(list.toString());
int n = list.getFirst();
System.out.println("First element is: " + n);
System.out.println(list.toString());
int m = list.sum();
System.out.println("The sum of the values is: " + m);
int ro1 = list.replaceOnce(2,3);
System.out.println("Number of replacements of value 2 with 1 is: " + ro1);
System.out.println(list.toString());
int ro2 = list.replaceOnce(3,4);
System.out.println("Number of replacements of value 3 with 4 is: " + ro2);
System.out.println(list.toString());
int ro3 = list.replaceOnce(7,8);
System.out.println("Number of replacements of value 7 with 8 is: " + ro3);
System.out.println(list.toString());
int mv = list.minVal();
System.out.println("Minimum value is: " + mv);
int ro4 = list.replaceOnce(1,7);
System.out.println("Number of replacements of value 3 with 4 is: " + ro4);
System.out.println(list.toString());
int ro5 = list.replaceOnce(8,1);
System.out.println("Number of replacements of value 3 with 4 is: " + ro5);
System.out.println(list.toString());
list.insertFirst(2);
System.out.println(list.toString());
}

} // class ListOfNVersion03PartA

Solutions

Expert Solution

package Array;

/**
* 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 ListOfNVersion02PartA
{   
   public int[] list; // Note: no "= {0, 1, 2, 3}" now

   /**
   * This constructor initializes the list to the same values
   * as in the parameter.
   *
   * @param element the initial elements for the list
   */
   public ListOfNVersion02PartA(int [] element)
   {
       // make "list" be an array the same size as "element"
       list = new int[element.length];
      
       // Loops till length of the parameter array
       for(int c = 0; c < element.length; c++)
           // Assigns each element of parameter element array to
           // implicit array
           list[c] = element[c];
   } // constructor ListOfNVersion01Skeleton(int [] element)

   /**
   * @return the number of elements stored in this list
   */
   public int getListSize()
   {
       return list.length;
   } // method getListSize

   /**
   * @return the last element in the list
   */
   public int getLast()
   {
       return list[list.length-1];
   } // method getLast

   /**
   * prints the contents of the list, in order from first to last
   */
   public void printList()
   {
       System.out.print("{");
       // Loops till number of elements in the list
       for(int c = 0; c < list.length; c++)
           if(c == list.length-1)
               // Displays current index position value
               System.out.print(list[c]);
           else
               System.out.print(list[c] + ", ");
       System.out.print("}");
   } // 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)
   {
       // Counter for number of times found
       int count = 0;
      
       // Loops till number of elements in the list
       for(int c = 0; c < list.length; c++)
          
           // Checks if parameter element is equals to list current current index position value
           if(element == list[c])
               // Increase the counter by one
               count++;
       // Returns the counter
       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)
   {
       // Counter for number of replacement
       int countReplacement = 0;
      
       // Loops till number of elements in the list
       for(int c = 0; c < list.length; c++)
       {
           // Checks if list current current index position value is equals to
           // parameter replaceThis value
           if(list[c] == replaceThis)
           {
               // Assigns the parameter withThis value
               // at fond index position c
               list[c] = withThis;
               // Increase the counter by one
               countReplacement++;
           }
       }
       // Returns the counter
       return countReplacement;
   } // method replaceAll

   /**
   * @return the first position in list occupied by the parameter value, or -1 if it is not found
   *
   * @param findThis the value to be found
   */
   public int findUnSorted(int findThis)
   {
       // Loops till number of elements in the list
       for(int c = 0; c < list.length; c++)
          
           // Checks if list current current index position value is equals to
           // parameter findThis value
           if(list[c] == findThis)
               // Returns the loops variable value as found index position
               return c;
      
       // At the end of the for loop returns -1 for not found
       return -1;
       // add and/or modify code to complete the method
   } // method findUnSorted

   /**
   * @return the position of the smallest element in the array, between positions "first" and "last"
   */
   public int minPos()
   {
       // Initially smallest element position is considered as 0
       int minPos = 0;
       // Initially smallest value is considered as 0th index position value
       int minValue = list[0];
      
       // Loops from 1 index position to number of elements in the list
       // Starts from 1 because 0th index position is already considered as
       // minimum
       for(int c = 1; c < list.length; c++)
          
           // Checks if list current index position value is less than
           // the earlier minimum value
           if(list[c] < minValue)
           {
               // Update the minimum position by assigning loop variable value
               minPos = c;
               // Assigns the current index position value as the minimum
               minValue = list[c];
           }
       return minPos;
   } // method minPos
  
   /**
   * Inserts an element in the last position. The elements already in the
   * list are pushed down one place, and the element that was previously
   * first is lost from the list.
   *
   * @param newElement the element to be inserted
   */
   public void insertLast(int newElement)
   {   
       // Loops till number of elements in the list
       for(int c = 0; c < list.length-1; c++)
           list[c] = list[c + 1];
       list[list.length-1] = newElement;          
   } // method insertLast
  
   public static void main(String []s)
   {
       // Creates an array
       int [] element = {0, 1, 2, 3};
      
       // Creates an object of class ListOfNVersion02PartA
       // using parameterized constructor by passing array element
       ListOfNVersion02PartA li = new ListOfNVersion02PartA(element);
      
       // Calls the method to test
       System.out.println("\n ************ List Contents ************ ");
       li.printlnList();
       System.out.print("\n List Size: " + li.getListSize());
       System.out.print("\n List Last Element: " + li.getLast());
       System.out.print("\n Number of 2's in List: " + li.countElement(2));
       System.out.print("\n Replace 2 with 22");
       li.replaceAll(2, 22);
       System.out.println("\n ************ List Contents ************ ");
       li.printlnList();
       System.out.print("\n Insert 22 at the end.");
       li.insertLast(22);
       System.out.println("\n ************ List Contents ************ ");
       li.printlnList();
       System.out.print("\n Number of 22 in List: " + li.countElement(22));
   }// end of main method
} // class ListOfNVersion02PartA

Sample Output:


************ List Contents ************
{0, 1, 2, 3}

List Size: 4
List Last Element: 3
Number of 2's in List: 1
Replace 2 with 22
************ List Contents ************
{0, 1, 22, 3}

Insert 22 at the end.
************ List Contents ************
{1, 22, 3, 22}

Number of 22 in List: 2


Related Solutions

/** * 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}"...
give an arbitrary list of integers, how many are 3? (in scheme)
give an arbitrary list of integers, how many are 3? (in scheme)
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements a polynomial as a linear array of terms ordered by exponent. Each polynomial is also reduced to simplest terms, that is, only one term for a given exponent. public class Polynomial { // P is a linear array of Terms private Term[ ] P; … // Re-implement the six methods of Polynomial (including the constructor) … }
PWrite a VHDL function that accepts a vector of arbitrary length and integer that specifies the...
PWrite a VHDL function that accepts a vector of arbitrary length and integer that specifies the number of bits the input vector to be rotated to the left and returns another vector. For instance functions accepts two inputs: 0101011 and 3 and it returns 1011010.
Let us choose seven arbitrary distinct positive integers, not exceeding 24. Show that there will be...
Let us choose seven arbitrary distinct positive integers, not exceeding 24. Show that there will be at least two subsets chosen from these seven numbers with equal total sums. (Keep in mind that sets, and hence subsets, have no repeated elements.) Hint: How many subsets can you form altogether? What is the largest total sum of such a subset?
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
Give an example of a program that uses the nongeneric version of a class from the...
Give an example of a program that uses the nongeneric version of a class from the STL and the equivalent program that uses the generic version. How do the two implementations differ? Why is having the syntax of generics better?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT