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...
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...
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?
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.
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...
Create a class and name it MyArray. This class must have an internal array of integers...
Create a class and name it MyArray. This class must have an internal array of integers and the consumer should specify the maximum capacity when instantiating. MyArray class must provide following functions: 1- insert: This method receives and integer and inserts into the array. For simplicity you can assume the array is large enough and never overflows. 2- display: This method displays all integers stored in the array in the same order as they are inserted (first in first out)....
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors...
summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors / * private data members / static vs. not static / and the * "toString" method. * * SKELETON FOR LAB TEST. * * @author Raymond Lister * @version April 2015; */ public class SummerOO { public static int numSummers = 0; // The above variable is used to count the number of // instances of the class SummerOO that have been created. //...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT