JPHamlett
01-11-2011, 03:48 PM
Ok, so I'm in a computer programming class at school, and were doing arrays. Here's the assignment
http://www.district196.org/evhs/academics/apcomputerscience/labs/ch6/AddingtoandRemovingFromanIntegerList1.htm
Here's my code so far.
Main Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package addingtoandremovingfromarrays;
import cs1.Keyboard;
public class Main{
static IntegerList list = new IntegerList(10);
//-------------------------------------------------------
// Create a list, then repeatedly print the menu and do what the
// user asks until they quit
//-------------------------------------------------------
public static void main(String[] args)
{
printMenu();
int choice = Keyboard.readInt();
while (choice != 0)
{
dispatch(choice);
printMenu();
choice = Keyboard.readInt();
}
}
//-------------------------------------------------------
// Do what the menu item calls for
//-------------------------------------------------------
public static void dispatch(int choice)
{
int loc;
switch(choice)
{
case 0:{
System.out.println("Bye!");
break;
}
case 1:{
System.out.println("How big should the list be?");
int size = Keyboard.readInt();
list = new IntegerList(size);
list.randomize();
break;
}
case 2:{
list.selectionSort();
break;
}
case 3 :{
System.out.println("What number?");
int number = Keyboard.readInt();
list.addElement(number);
break;
}
case 4:{
list.print();
break;
}
default:
System.out.println("Sorry, invalid choice");
}
}
//-------------------------------------------------------
// Print the user's choices
//-------------------------------------------------------
public static void printMenu()
{
System.out.println("\n Menu ");
System.out.println(" ====");
System.out.println("0: Quit");
System.out.println("1: Create a new list (** do this first!! **)");
System.out.println("2: Sort the list using selection sort");
System.out.println("3: Increase array size.");
System.out.println("4: Print array");
System.out.print("\nEnter your choice: ");
}
}
Construer Class
package addingtoandremovingfromarrays;
// ************************************************** **************
// IntegerList.java
//
// Define an IntegerList class with methods to create, fill,
// sort, and search in a list of integers.
//
// ************************************************** **************
public class IntegerList{
int[] list; //values in the list
int arraySize;
//-------------------------------------------------------
//create a list of the given size
//-------------------------------------------------------
public IntegerList(int size)
{
list = new int[size];
arraySize = size;
}
//-------------------------------------------------------
//fill array with integers between 1 and 100, inclusive
//-------------------------------------------------------
public void randomize()
{
for (int i=0; i<list.length; i++)
list[i] = (int)(Math.random() * 100) + 1;
}
public void increaseSize(){
int[] temp = new int[arraySize];
temp = list;
list = new int[arraySize + 1];
for (int i = 0; i < temp.length; i++)
list[i] = temp[i];
list[list.length] = -1;
}
public void addElement(int elem){
if (list[list.length - 1] != -1)
increaseSize();
list[list.length - 1] = elem;
}
//-------------------------------------------------------
//print array elements with indices
//-------------------------------------------------------
public void print()
{
for (int i=0; i<list.length; i++)
System.out.println(i + ":\t" + list[i]);
}
//-------------------------------------------------------
//return the index of the first occurrence of target in the list.
//return -1 if target does not appear in the list
//-------------------------------------------------------
public int search(int target)
{
int location = -1;
for (int i=0; i<list.length && location == -1; i++)
if (list[i] == target)
location = i;
return location;
}
//-------------------------------------------------------
//sort the list into ascending order using the selection sort algorithm
//-------------------------------------------------------
public void selectionSort()
{
int minIndex;
for (int i=0; i < list.length-1; i++)
{
//find smallest element in list starting at location i
minIndex = i;
for (int j = i+1; j < list.length; j++)
if (list[j] < list[minIndex])
minIndex = j;
//swap list[i] with smallest element
int temp = list[i];
list[i] = list[minIndex];
list[minIndex] = temp;
}
}
}
This is the debug
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at addingtoandremovingfromarrays.IntegerList.increase Size(IntegerList.java:44)
at addingtoandremovingfromarrays.IntegerList.addEleme nt(IntegerList.java:50)
at addingtoandremovingfromarrays.Main.dispatch(Main.j ava:57)
at addingtoandremovingfromarrays.Main.main(Main.java: 24)
Java Result: 1
I cant seem to get IncreaseSize() to work, any help?
http://www.district196.org/evhs/academics/apcomputerscience/labs/ch6/AddingtoandRemovingFromanIntegerList1.htm
Here's my code so far.
Main Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package addingtoandremovingfromarrays;
import cs1.Keyboard;
public class Main{
static IntegerList list = new IntegerList(10);
//-------------------------------------------------------
// Create a list, then repeatedly print the menu and do what the
// user asks until they quit
//-------------------------------------------------------
public static void main(String[] args)
{
printMenu();
int choice = Keyboard.readInt();
while (choice != 0)
{
dispatch(choice);
printMenu();
choice = Keyboard.readInt();
}
}
//-------------------------------------------------------
// Do what the menu item calls for
//-------------------------------------------------------
public static void dispatch(int choice)
{
int loc;
switch(choice)
{
case 0:{
System.out.println("Bye!");
break;
}
case 1:{
System.out.println("How big should the list be?");
int size = Keyboard.readInt();
list = new IntegerList(size);
list.randomize();
break;
}
case 2:{
list.selectionSort();
break;
}
case 3 :{
System.out.println("What number?");
int number = Keyboard.readInt();
list.addElement(number);
break;
}
case 4:{
list.print();
break;
}
default:
System.out.println("Sorry, invalid choice");
}
}
//-------------------------------------------------------
// Print the user's choices
//-------------------------------------------------------
public static void printMenu()
{
System.out.println("\n Menu ");
System.out.println(" ====");
System.out.println("0: Quit");
System.out.println("1: Create a new list (** do this first!! **)");
System.out.println("2: Sort the list using selection sort");
System.out.println("3: Increase array size.");
System.out.println("4: Print array");
System.out.print("\nEnter your choice: ");
}
}
Construer Class
package addingtoandremovingfromarrays;
// ************************************************** **************
// IntegerList.java
//
// Define an IntegerList class with methods to create, fill,
// sort, and search in a list of integers.
//
// ************************************************** **************
public class IntegerList{
int[] list; //values in the list
int arraySize;
//-------------------------------------------------------
//create a list of the given size
//-------------------------------------------------------
public IntegerList(int size)
{
list = new int[size];
arraySize = size;
}
//-------------------------------------------------------
//fill array with integers between 1 and 100, inclusive
//-------------------------------------------------------
public void randomize()
{
for (int i=0; i<list.length; i++)
list[i] = (int)(Math.random() * 100) + 1;
}
public void increaseSize(){
int[] temp = new int[arraySize];
temp = list;
list = new int[arraySize + 1];
for (int i = 0; i < temp.length; i++)
list[i] = temp[i];
list[list.length] = -1;
}
public void addElement(int elem){
if (list[list.length - 1] != -1)
increaseSize();
list[list.length - 1] = elem;
}
//-------------------------------------------------------
//print array elements with indices
//-------------------------------------------------------
public void print()
{
for (int i=0; i<list.length; i++)
System.out.println(i + ":\t" + list[i]);
}
//-------------------------------------------------------
//return the index of the first occurrence of target in the list.
//return -1 if target does not appear in the list
//-------------------------------------------------------
public int search(int target)
{
int location = -1;
for (int i=0; i<list.length && location == -1; i++)
if (list[i] == target)
location = i;
return location;
}
//-------------------------------------------------------
//sort the list into ascending order using the selection sort algorithm
//-------------------------------------------------------
public void selectionSort()
{
int minIndex;
for (int i=0; i < list.length-1; i++)
{
//find smallest element in list starting at location i
minIndex = i;
for (int j = i+1; j < list.length; j++)
if (list[j] < list[minIndex])
minIndex = j;
//swap list[i] with smallest element
int temp = list[i];
list[i] = list[minIndex];
list[minIndex] = temp;
}
}
}
This is the debug
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at addingtoandremovingfromarrays.IntegerList.increase Size(IntegerList.java:44)
at addingtoandremovingfromarrays.IntegerList.addEleme nt(IntegerList.java:50)
at addingtoandremovingfromarrays.Main.dispatch(Main.j ava:57)
at addingtoandremovingfromarrays.Main.main(Main.java: 24)
Java Result: 1
I cant seem to get IncreaseSize() to work, any help?