This class maintains a fixed-size list of 4 integers. It contains methods to initialize the list, get the size and last element, print the list, count/replace elements, find elements, swap elements, and sort the list. The class uses arrays and loops to implement the various list operations and algorithms like linear search and bubble sort.
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Maintains List of 4 Integers
1. /**
* This class maintains a list of 4 integers.
*
* This list is simple and unusual in it always contains exactly
the
* same number of elements. Later, when we know more Java,
we will
* study classes where the number of elements may vary.
*
* In this version 2, we use variables in the subscripts.
*
* @author Raymond Lister
* @version August 2014
*
*/
public class ListOf4V2PartA
{
private static final int listSize = 4;
private int[] list = {0, 1, 2, 3};
/**
* A constructor for objects of class ListOf4, which takes four
parameters
*
* @param one an initial element for the list, the first element.
* @param two an initial element for the list
* @param three an initial element for the list
* @param four an initial element for the list, the last element.
*/
public returnTypeIfAny nameOfConstructor(int one, int two,
int three, int four)
{
add whatever code is required to this constructor body
to initialize the four elements of array "list" to
2. the values given in the parameters.
} // constructor ListOf4(int one, int two, int three, int four)
/**
* @return the number of elements stored in this list
*/
public returntype getListSize(parametersIfAny)
{
return something;
} // method getListSize
/**
* @return the last element in the list
*/
public returntype getLast(parametersIfAny)
{
add whatever code is required here in the method body
} // method getLast
/**
* prints the contents of the list, in order from first to last
*/
public returntype printList(para metersIfAny)
{
// with variable subscripts ...
System.out.print("{");
int i = initial value; // role: stepper
System.out.print(list[array subscript]);
i = updated value;
System.out.print(", " + list[array subscript]);
copy and paste some of the code above, as many times as
3. required
System.out.print("}");
} // method printList
/**
* 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 returntype countElement(parametersIfAny)
{
int count = 0; // role: stepper
// with variable subscripts ...
int i = initial; // role: stepper
if ( list[subscript] == something ) doSomething;
i = updatedVal;
copy and paste some of the code above, as many times as
required
return returnValue;
4. } // method countElement
/**
* @return the number of times the replacement was made
*
* @param replaceThis the element to be replaced
* @param withThis the replacement
*/
public returntype replaceAll(parametersIfAny)
{
int count = 0; // role: stepper; number of replacements made
// with variable subscripts ...
int i = beginning value; // role: stepper
if ( list[sub] == something )
{
list[sub] = something;
increment something;
}
increment something;
copy and paste some of the code above, as many times as
required
return count;
} // 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
*/
5. public returntype findUnSorted(parametersIfAny)
{
// This algorithm is known as "linear search"
// with variable subscripts ...
int pos = initialValueForPos;
if ( list[subscript] == something ) return whatever;
pos = updatedVal;
copy and paste some of the code above, as many times as
required
return notFound;
} // method findUnSorted
/**
* @return the position of the smallest element in the array
*/
public returntype minPos(parametersIfAny)
{
int mostWantedHolder = initial value; // role: Most-wanted
holder
// variable mostWantedHolder remembers the position of the
// smallest number seen so far
// with variable subscripts ...
int i = initial value; // role: stepper
if ( list[subscript1] < list[subscript2] )
{
update mostWantedHolder
}
i = newVal;
6. copy and paste some of the code above, as many times as
required
return theAppropriateValue;
} // method minPos
/**
* 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
*/
public void swap(int i, int j)
{
int temp; // role: temporary
temp = list[i];
list[i] = list[j];
list[j] = temp;
} // method swap
/**
* Orders the elements of the list, with the first element smallest
and the
* last element largest. Does this using the bubblesort
algorithm.
*/
public returntype sortBubble(parametersIfAny)
{
/* This implementation uses the bubble sort algorithm. For an
explanation
* of how bubblesort works, google ...
* bubblesort java
7. */
// with variable subscripts ...
int i = initialize i; // role: stepper
if ( list[subscript1] > list[subscript2] ) swap(subscript3,
subscript4);
i = nextVal;
copy and paste some of the code above, as many times as
required
// At this point, the largest element must be in list[3].
reinitialize i
copy and paste some of the code above, as many times as
required
// At this point, the second largest element must be in list[2].
reinitialize i
copy and paste some of the code above, as many times as
required
// At this point, the smallest element must be in list[0].
} // method sortBubble
} // class ListOf4V2
Solution
/**
8. *
* This class maintains a list of 4 integers.
*
* This list is simple and unusual in it always contains exactly
the same number
* of elements. Later, when we know more Java, we will study
classes where the
* number of elements may vary.
*
* In this version 2, we use variables in the subscripts.
*
* @author Raymond Lister
* @version August 2014
*
*/
public class ListOf4V2PartA {
private static final int listSize = 4;
private int[] list = { 0, 1, 2, 3 };
/**
* A constructor for objects of class ListOf4, which takes four
parameters
*
* @param one
* an initial element for the list, the first element.
* @param two
9. * an initial element for the list
* @param three
* an initial element for the list
* @param four
* an initial element for the list, the last element.
*/
public ListOf4V2PartA(int one, int two, int three, int four) {
list[0] = one;
list[1] = two;
list[2] = three;
list[3] = four;
} // constructor ListOf4(int one, int two, int three, int four)
/**
* @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() {
// add whatever code is required here in the method body
return list[list.length - 1];
} // method getLast
10. /**
* prints the contents of the list, in order from first to last
*/
public void printList() {
// with variable subscripts ...
System.out.print("{");
for (int i = 0; i < list.length; i++) {
if (i == 0)
System.out.print(list[i]);
else
System.out.print(", " + list[i]);
}
System.out.print("}");
} // method printList
/**
* 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
11. *
* @param element
* the element to be counted
*/
public int countElement(int element) {
int count = 0; // role: stepper
// with variable subscripts ...
for (int i = 0; i < list.length; i++) {
if (list[i] == element)
count++;
}
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) {
int count = 0; // role: stepper; number of replacements
made
// with variable subscripts ...
12. for (int i = 0; i < list.length; i++) {
if (list[i] == replaceThis) {
list[i] = withThis;
count++;
}
}
return count;
} // 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"
// with variable subscripts ...
int pos = -1;
for (int i = 0; i < list.length; i++) {
if (list[i] == findThis)
return i;
}
return pos;
13. } // method findUnSorted
/**
* @return the position of the smallest element in the array
*/
public int minPos() {
int mostWantedHolder = list[0]; // role: Most-wanted
holder
// variable mostWantedHolder remembers the position of
the
// smallest number seen so far
int pos = 0;
// with variable subscripts ...
for (int i = 0; i < list.length; i++) {
if (list[i] < mostWantedHolder) {
mostWantedHolder = list[i];
pos = i;
}
}
return pos;
} // method minPos
/**
* Swaps two elements in the list
*
* @param i
* the position of one of the elements to be swapped
14. * @param j
* the position of one of the elements to be swapped
*/
public void swap(int i, int j) {
int temp; // role: temporary
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
/**
* Orders the elements of the list, with the first element
smallest and the
* last element largest. Does this using the bubblesort
algorithm.
*/
public void sortBubble() {
/*
* This implementation uses the bubble sort algorithm. For
an
* explanation of how bubblesort works, google ...
bubblesort java
*/
for (int i = (list.length - 1); i >= 0; i--) {
for (int j = 1; j <= i; j++) {
if (list[j - 1] > list[j]) {
15. int temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
// At this point, the smallest element must be in list[0].
} // method sortBubble
} // class ListOf4V2
/**
* @author
*
*/
public class TestListOf4V2PartA {
/**
* @param args
*/
public static void main(String[] args) {
ListOf4V2PartA listOf4V2PartA = new ListOf4V2PartA(5,
15, 4, 13);
System.out.println("getListSize()-->" +
listOf4V2PartA.getListSize());
System.out.println("getLast()-->" +
listOf4V2PartA.getLast());
listOf4V2PartA.printList();