Log In · Register

 
JAVA PROGRAM DUE 3/25 12 NOON !!!! HELP PLEASE, HELP ME FIX MY CODE !!!!!!!!!!
deteam
post Mar 24 2009, 08:51 PM
Post #1


Senior Member
*****

Group: Human
Posts: 525
Joined: Nov 2008
Member No: 695,913



CAN SOME ONE PLEASE FIX MY CODE!!!!

HERES WHAT I HAVE TO DO . . . .

CODE
Many applications require a list of values to remain in sorted order, even if new values are added to the list (or old values are removed). It can be very expensive (in terms of CPU time) to continually re-sort the array every time a new value is added. A better approach is to insert new values in their proper place as they are added to the list, shifting elements down as necessary.

Complete the SortedIntegerArray class (provided in the SortedIntegerArray.txt file). This class contains two instance variables: an int array (do NOT use an ArrayList!), and an integer that tracks how many values are currently stored in the array (a number between 0 and the length of the array).

Fill in the bodies of the following methods for your new data type:

1. SortedIntegerArray(): This constructor takes a single int argument. The constructor should set the size instance variable to 0 (the array is empty) and creates a new integer array large enough to hold the specified number of values (for example, SortedIntegerArray(10) would create a new instance with a 10-element array).
2. size(): This method returns the number of elements that are currently stored in the internal array.
3. get(): This method takes an integer position (0 through (length-1)) as its argument and returns the value stored in that position. If the position does not exist (because there are insufficient elements in the array), the method returns -1 instead.
4. print(): This method prints out the current contents of the array, in order. Be sure that your implementation only prints occupied elements of the array!
5. add(): This method takes an int value as its argument and inserts that value into its proper place in the array, in sorted order. If necessary, you may need to shift existing array elements down one place to accommodate the new value; use a loop to do this, starting from the end of the array. If the array is already full, do not insert the new value (you may print an error message if you wish, or you can simply allow the operation to fail silently). You may assume that the array will only store non-negative integers (0 or greater). Be sure to update the value of the size variable if your addition is successful!
6. Write a main() method that creates a new SortedIntegerArray object and displays the result of inserting several values into the array. Show that your implementation correctly places values into their sorted position, and that your size(), get(), and print() methods work correctly.
7. Extra credit: Add a remove() method to your array. This method should take an array position (0 through (length - 1)) as its argument. If the array position exists, remove the element at that position and shift any subsequent elements forward to close the gap. Don't forget to update the value of the size variable as well! This method should not return anything. (2 points, for a total of 12 points for the lab)



Heres My Code:

CODE
public class SortedIntegerArray
{
private int [] values; // Array that holds user data
private int size; // The number of elements that are currently
// contained in the values[] array

// Instance methods -- FILL THESE IN!

// Constructor
public SortedIntegerArray (int startingSize)
{
size= 0;
values = new int[startingSize];
}

public int size()
{
return size;
}

public int get(int position)
{
if(position < size)
return values [position];
else
return -1;

/*if (position >=0)
{
return -1;
}
if (position > size -1)
{
return -1;
}
else
return position;
*/
}
public void print ()
{
for (int i =0; i < size; i++)
{
System.out.print("" + values[i]);
System.out.println();
}
}

public void add (int value)
{
for (int x=0; x<size; x++)
{
for (int y=0; y<size-1; y++)
{
if(values[y]> values[y+1])
{
int temp = values[y];
values[y] = values[y+1];
values[y+1]= temp;

}
}
}
}



public static void main (String [] args)
{
SortedIntegerArray arr = new SortedIntegerArray(10);
arr.add(3);
arr.add(2);
arr.add(1);
arr.add(6);
arr.add(4);

arr.print();
System.out.println("Size is " + arr.size());
System.out.println("The Value of Index 4 is " + arr.get(4));
System.out.println("The Value of Index 6 is " + arr.get(6));

}

// EXTRA CREDIT METHOD
public void remove (int position)
{
if(position >= size() || position < 0)
{
System.out.println("positon " + position + " remove failed!");
return;
}
else if(position < size() - 1)
{
for(int i = position; i < size() - 1; i ++)
{
values[i] = values[i + 1];
}
size --;
}
else {
values[position] = 0;
size --;
}
}
}





HELP ME PLEASE !!!!!!!!!!!


Stay Up
-1-
Subliminal
 

Posts in this topic


Closed TopicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: