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

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 *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}"...
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...
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.
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) … }
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)
In short, you’re going to implement a linked-list class for storing integers, using a provided main...
In short, you’re going to implement a linked-list class for storing integers, using a provided main program to help you interact and test your work. You’ll want to build the linked-list class function by function, working in “Develop” mode to test out each function you write. main.cpp is a read only file linkedlist.h is the file to work on. main.cpp #include #include #include "linkedlist.h" using namespace std; int main() { linkedlist LL; string cmd; int value, key; // // user...
Following the Well Ordering Principle for Integers suppose n is an arbitrary integer greater than 1...
Following the Well Ordering Principle for Integers suppose n is an arbitrary integer greater than 1 and: Z={x is a positive integer such that x is greater than or equal to 2 and x divides n} Let y be the least element is set Z. Using proof by contradiction, show that y is prime.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT