In: Computer Science
Having some trouble with this Java Lab (Array.java)
Objective:
This lab is designed to create an array of variable length and insert unique numbers into it. The tasks in this lab include:
Create and Initialize an integer array
Create an add method to insert a unique number into the list
Use the break command to exit a loop
Create a toString method to display the elements of the array
Task 1:
Create a class called Array, which contains an integer array called ‘listArray’. The integer array should not be initialized to any specific size.
Task 2:
Create a Constructor with a single parameter, which specifies the number of elements that needs to be created for listArray. The listArray needs to be created to this size.
Task 3:
Create a ‘add’ method to class Array which will search through the elements of listArray. A looping mechanism should be used to search the array of elements. If the value passed into add is not already in the list, it should be added. Once the element is added, you need to use the break statement to exit to search so the value isn’t added to each space in the array. If all elements of the array contain non-zero numbers, no more values will be added.
Task 4:
Create a ‘toString’ method to output the non-zero elements of the array. Use the program template and the Sample Output as a reference to guide your application development.
Sample output:
Enter Number of Elements to Create in Array: 10
Enter Number to Add to List (-1 to Exit): 4
4 (added)
Enter Number to Add to List (-1 to Exit): 9
9 (added)
Enter Number to Add to List (-1 to Exit): 21
21 (added)
Enter Number to Add to List (-1 to Exit): 4
4 (duplicate)
Enter Number to Add to List (-1 to Exit): 7
7 (added)
Enter Number to Add to List (-1 to Exit): -1
Array List: 4, 9, 21, 7,
Test Code (ArrayTest.java):
package arraytest; import java.util.Scanner; public class ArrayTest { public static void main(String[] args) { int elements, input; Scanner keyboard = new Scanner( System.in ); Array arrayList; System.out.print( "Enter Number of Elements to Create in Array: " ); elements = keyboard.nextInt(); // Read number of elements arrayList = new Array( elements ); // Instantiate array with no elements do { System.out.print( "Enter Number to Add to List (-1 to Exit): " ); input = keyboard.nextInt(); // Read new Number to add if ( input != -1 ) { arrayList.add( input ); // Add to array if not -1 } } while ( input != -1 ); // call toString method to display list System.out.println( arrayList ); } }
PROGRAM CODE:
Array.java
package array;
public class Array {
int listArray[];
int size;
Array(int size)
{
listArray = new int[size];
size = 0;
}
public void add(int value)
{
for(int i=0; i<size; i++)
{
if(listArray[i] == value)
{
System.out.println(value + " (duplicate)");
return;
}
}
if(size != listArray.length)
{
listArray[size++] = value;
System.out.println(value +" (added)");
}
}
@Override
public String toString() {
String output = "ArrayList: ";
for(int i=0; i<size; i++)
{
output += listArray[i] + ",";
}
return output;
}
}
ArrayTest.java
package array;
import java.util.Scanner;
public class ArrayTest
{
public static void main(String[] args)
{
int elements, input;
Scanner keyboard = new Scanner( System.in );
Array arrayList;
System.out.print( "Enter Number of Elements to Create in Array: " );
elements = keyboard.nextInt(); // Read number of elements
arrayList = new Array( elements ); // Instantiate array with no elements
do
{
System.out.print( "Enter Number to Add to List (-1 to Exit): " );
input = keyboard.nextInt(); // Read new Number to add
if ( input != -1 )
{
arrayList.add( input ); // Add to array if not -1
}
} while ( input != -1 );
// call toString method to display list
System.out.println( arrayList );
}
}
OUTPUT:
Enter Number of Elements to Create in Array: 10
Enter Number to Add to List (-1 to Exit): 4
4 (added)
Enter Number to Add to List (-1 to Exit): 9
9 (added)
Enter Number to Add to List (-1 to Exit): 21
21 (added)
Enter Number to Add to List (-1 to Exit): 4
4 (duplicate)
Enter Number to Add to List (-1 to Exit): 7
7 (added)
Enter Number to Add to List (-1 to Exit): -1
ArrayList: 4,9,21,7,