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 ListOfNVersion02PartB
{   
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 ListOfNVersion02PartB(int [] element)
{
// make "list" be an array the same size as "element"
list = new int[element.length];

for (int i = 0; i {
list[i] = element[i];
}


// add whatever code is required to complete the constructor


} // constructor ListOfNVersion01Skeleton(int [] element)

/**
* @return the first element in the list
*/
public int getFirst()
{
return list[0];

} // method getFirst

/**
* In ListOfN in this test, the toString method
* should return a String such that:
*
* 1. The String begins with an open brace "{" followed
* IMMEDIATELY (i.e. no space) by the first element
* of the list.
* 2. All remaining elements of the list are preceded by
* ", " (i.e. a comma folowed by a SINGLE space).
* 3. The String ends with a closing brace "}".
*
* e.g. "{1, 2, 3, 4}"
*
* @return A summary of the contents of the list.
*/
public String toString()
{
String s = "{"; // role: gatherer

for (int i = 0; i < list.length; i++)

if (i == 0)
{
s += list[i];
}
else
{
s += ", " + list[i];
}

s += "}";

// add and/or modify code to complete the method

return s;

} // method toString

/**
* @return the sum of the elements of the array
*/
public int sum()
{
int sum = 0;
  
for (int i = 0; i < list.length; i++)
{
sum += list[i];
}

// add and/or modify code to complete the method


return 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)
{   
  
for (int i = 0; i < list.length; i++)

{
if( list[i] == replaceThis)
{
list[i] = withThis;
return 1;
}
  
}

// add and/or modify code to complete the method


return 0;
  

} // method replaceOnce

/**
* @return the value of the smallest element in the array
*/
public int minVal()
{
int min = Integer.MAX_VALUE;
  
for (int i = 0; i < list.length; i++)

if (list[i] < min)
{
min = list[i];
}

// add and/or modify code to complete the method


return min;

} // method minVal

/**
* Inserts an element in the first position. The elements already in the
* list are pushed up one place, and the element that was previously
* last is lost from the list.
*
* @param newElement the element to be inserted
*/
public void insertFirst(int newElement)
{   
int temp = list[0];
list[0] = newElement;

for (int i = 1; i < list.length-1; ++i)
{
int curr = list[i];
list[i] = temp;
temp = curr;
}


// add and/or modify code to complete the method


} // method insertFirst
  
/*
* This method is NOT examinable in this test.
*
* Swaps two elements in the list.
*
* @param i the position of one of the elements to be swapped
* @param j the position of one of the elements to be swapped
*/
private void swap(int i, int j)
{
int temp; // role: temporary

temp = list[i];
list[i] = list[j];
list[j] = temp;

} // method swap

/**
* "So the first shall be last, and the last first"
* -- The Christian Bible, book of Matthew 20:16
*/
public void reverse()
{
for (int i = 0, j = list.length - 1; i < list.length / 2 && j > list.length / 2; i++, j--)
{
swap(i, j);


// add and/or modify code to complete the method


} // method reverse

}} // class ListOfNVersion02PartB

How do i fix this??:

Failed: Check insertFirst. expected:<{7,[1,4],2}> but was:<{7,[4,1],2}>

I figured out my mistake, thank you anyway!

(the reverse method needed a while loop and fixed my insertFirst method )

Solutions

Expert Solution

The lines in which i have made changes i have wriiten //changes after that line as comment .

If still have any query ask in comment section i will try to clearify it.

public class ListOfNVersion02PartB
{   
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 ListOfNVersion02PartB(int [] element)
{
// make "list" be an array the same size as "element"
list = new int[element.length];

for (int i = 0; i<element.length;i++) { //changes
list[i] = element[i];
}


// add whatever code is required to complete the constructor


} // constructor ListOfNVersion01Skeleton(int [] element)

/**
* @return the first element in the list
*/
public int getFirst()
{
return list[0];

} // method getFirst

/**
* In ListOfN in this test, the toString method
* should return a String such that:
*
* 1. The String begins with an open brace "{" followed
* IMMEDIATELY (i.e. no space) by the first element
* of the list.
* 2. All remaining elements of the list are preceded by
* ", " (i.e. a comma folowed by a SINGLE space).
* 3. The String ends with a closing brace "}".
*
* e.g. "{1, 2, 3, 4}"
*
* @return A summary of the contents of the list.
*/
public String toString()
{
String s = "{"; // role: gatherer

for (int i = 0; i < list.length; i++)

if (i == 0)
{
s += list[i];
}
else
{
s += ", " + list[i];
}

s += "}";

// add and/or modify code to complete the method

return s;

} // method toString

/**
* @return the sum of the elements of the array
*/
public int sum()
{
int sum = 0;
  
for (int i = 0; i < list.length; i++)
{
sum += list[i];
}

// add and/or modify code to complete the method


return 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)
{   
  
for (int i = 0; i < list.length; i++)
{
if( list[i] == replaceThis)
{
list[i] = withThis;
return 1;
}
  
}

// add and/or modify code to complete the method
return 0;
} // method replaceOnce

/**
* @return the value of the smallest element in the array
*/
public int minVal()
{
int min = Integer.MAX_VALUE;
  
for (int i = 0; i < list.length; i++)

if (list[i] < min)
{
min = list[i];
}

// add and/or modify code to complete the method


return min;

} // method minVal

/**
* Inserts an element in the first position. The elements already in the
* list are pushed up one place, and the element that was previously
* last is lost from the list.
*
* @param newElement the element to be inserted
*/
public void insertFirst(int newElement)
{   
int temp = list[0];
list[0] = newElement;

for (int i = 1; i < list.length; ++i)//changes
{
int curr = list[i];
list[i] = temp;
temp = curr;
}


// add and/or modify code to complete the method


} // method insertFirst
  
/*
* This method is NOT examinable in this test.
*
* Swaps two elements in the list.
*
* @param i the position of one of the elements to be swapped
* @param j the position of one of the elements to be swapped
*/
private void swap(int i, int j)
{
int temp; // role: temporary

temp = list[i];
list[i] = list[j];
list[j] = temp;



} // method swap

/**
* "So the first shall be last, and the last first"
* -- The Christian Bible, book of Matthew 20:16
*/
public void reverse()
{
for (int i = 0, j = list.length - 1; i < list.length / 2 ; i++, j--)//changes
{
swap(i, j);


// add and/or modify code to complete the method


} // method reverse

}

} // class ListOfNVersion02PartB

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...
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)
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT