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