In: Computer Science
/**
* 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];
// add whatever code is required to complete the constructor
} // constructor ListOfNVersion01Skeleton(int [] element)
/**
* @return the number of elements stored in this list
*/
public int getListSize()
{
return 999; // replace "999" with the correct answer
/* See Nielsen page 85-86,
* section 4.2.3 Retrieving the size of arrays: length
*
* See Parsons page 45,
* section 3.3.4 The Array “length” Field and also page 47
*/
} // method getListSize
/**
* @return the last element in the list
*/
public int getLast()
{
return 999; // replace "999" with the correct answer
/* See Nielsen page 85-86,
* section 4.2.3 Retrieving the size of arrays: length
*
* See Parsons page 45,
* section 3.3.4 The Array “length” Field and also page 47
*/
} // method getLast
/**
* prints the contents of the list, in order from first to
last
*/
public void printList()
{
System.out.print("{");
// add and/or modify code to complete the method
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)
{
// add and/or modify code to complete the method
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)
{
// add and/or modify code to complete the method
return 999;
} // 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)
{
// This algorithm is known as "linear search"
return 999;
// 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()
{
return 999;
// add and/or modify code to complete the method
} // 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)
{   
// add and/or modify code to complete the method
} // method insertLast
} // class ListOfNVersion02PartA
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