This is what the program needs to do I need help with #4 and down, if you could
ID: 3624292 • Letter: T
Question
This is what the program needs to do I need help with #4 and down, if you could also help out with the extra stuff that would be great. Below this is the List class and the main program.I already have most of number 4 done I believe, but not positive.
1) Create a client program, that creates an instance of List, with a maxsize of 15.
2) Prompt the user for 10 values and insert them into the list.
3) Using the methods of the list, return, and print the contents of the list.
4) IN THE CLIENT program, write a bubble sort method that receives the list as a parameter, then use the operations of the list to bubble sort the list.
5) Print the sorted list.
6) In List, create a swap function, that takes two positions in the list, and swaps their values. This should not be used in your bubble sort implementation.
7) Swap the first and last elements in the list, and print the list again.
BONUS: Change the list implementation and your sorting method to take strings instead of ints.
BONUS BONUS: Change the list implementation to accept arbitrary objects. You can assume that the Client program will only ever put objects of one type into a given list instance, but the type should not be limited by the List class.
public class List{
// Description: This class implements a list ADT, which can
// be used to store unordered series of integers. This list is based on the
// List ADT described in your text.
// Data members:
private int currentSize;
private int maxSize;
private int[] elements;
public List() {
currentSize = 0;
maxSize = 50;
elements = new int[50];
}
public List(int maxElements) {
currentSize = 0;
maxSize = maxElements;
elements = new int[maxSize];
}
public boolean isFull() {
return (currentSize == maxSize);
}
public boolean isEmpty() {
return (currentSize == 0);
}
public int size() {
// PostCondition: THis method returns the # of element in the list
return (currentSize);
}
public int getMaxsize(){
// this method returns the maximum size of the list
return (maxSize);
}
public void insertAt( int newValue, int position) {
// if position is <=1 or > the current # of elements + 1
// print an error
if (position < 1 || position > currentSize +1)
throw new ListException(" Insert cancelled, Invalid position given");
else {
if (this.isFull()) throw new ListException("Insert Cancelled, " +
"List is full.");
else {
// adding first element
if (currentSize == 0) this.elements[0] = newValue;
else { // adding at end
if (position == currentSize + 1)
this.elements[position-1] = newValue;
else {
// shift elements to the left to make room for newValue.
for( int i= currentSize-1; i >= position-1; i--)
this.elements[i+1] = this.elements[i];
this.elements[position-1]= newValue;
} // adding in middle
}// else adding at end
currentSize++;
}
}
} // end insert
public int getValue(int position) {
if (position <1 || position> this.currentSize)
throw new ListException("Invalid position specified in getValue");
return (this.elements[position-1]);
} // end of retrieve
public int find(int value) {
int location= -999;
for (int i=0; i <= this.currentSize-1; i++) {
if (this.elements[i] == value) {
location = i+1;
break;
}
} // end for
return location;
}// end find
public int deleteAt(int position) {
int tempValue=-999;
if ( this.isEmpty() || position > currentSize || position < 1 )
throw new ListException("This delete can not be performed "
+ "an element at position " + position
+ " does not exist " );
else {
tempValue = this.elements[position-1];
for (int i=position-1; i< currentSize-1; i++)
this.elements[i] = this.elements[i+1];
this.elements[currentSize-1] = 0;
currentSize--;
}// end else
return tempValue;
} // end delete
} // end of class
____________________________Main Program______________________________________-
import java.util.*;
public class listTest
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
//1
List aList = new List(15);
//2
System.out.println("Enter 10 integers");
aList.insertAt(input.nextInt(),1);
aList.insertAt(input.nextInt(),2);
aList.insertAt(input.nextInt(),3);
aList.insertAt(input.nextInt(),4);
aList.insertAt(input.nextInt(),5);
aList.insertAt(input.nextInt(),6);
aList.insertAt(input.nextInt(),7);
aList.insertAt(input.nextInt(),8);
aList.insertAt(input.nextInt(),9);
aList.insertAt(input.nextInt(),10);
//3
for(int index=1; index<=aList.size();index++)
{
System.out.println(aList.getValue(index));
}
//4
/*public void bubbleSort( int List, aList)
{
boolean swapped;
do
{
swapped = false;
for(int index=1; i < alist.size; i++)
{
if(list[index-1]>list[index])
{
int temp =aList[index];
list[index] = list[index-1];
list[index-1] = temp;
swapped = true;
}
}
}while (swapped);
*/
}
}
_____________________________________Exception_________________________________
public class ListException extends RuntimeException{
public ListException ( String message){
super(message);
}
}
Explanation / Answer
4)
public static void bubbleSort(List list)
{
for(int i = 0; i < list.size(); i++)
{
boolean swaps = false;
for(int j = 1; j < list.size(); j++)
{
// check for swap
if(list.getValue(j) < list.getValue(j-1))
{
// swap elements
int temp = list.deleteAt(j-1); // remove element and re-insert, since there isn't a set method
list.insertAt(temp, j);
swaps=true;
}
}
// smart bubble sort check
if(!swaps) break;
}
}
5)
// print list
for(int i = 0; i < list.size(); i++)
{
System.out.print(list.getValueAt(i)+" ");
}
System.out.println();
6)
// this method is in list
public void swap(int i, int j)
{
int temp = elements[i];
elements[i] = elements[j];
elements[j] = temp;
}
7)
// swap first and last
list.swap(0, list.size()-1);
// print list
for(int i = 0; i < list.size(); i++)
{
System.out.print(list.getValueAt(i)+" ");
}
System.out.println();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.