/WRITE AN APPLICATION CLASS HIGHARRAYAPP.JAVA TO TEST ALL METHODS DEFINED IN HIG
ID: 3873665 • Letter: #
Question
/WRITE AN APPLICATION CLASS HIGHARRAYAPP.JAVA TO TEST ALL METHODS DEFINED IN HIGHARRAY.JAVA CLASS. THE APPLICATION SHOULD ALLOW THE USER TO DECIDE THE LENGTH OF THE ARRAY ( ASK FOR USER INPUT AND USE THAT VALUE WHILE INSTANTIATE THE OBJECT OF HIGHARRAY). YOUR PROGRAM SHOULD PROVIDE A MENU OF OPERATIONS. USE JAVA "SWITCH" STATEMENT TO BRANCH TO THE DIFFERENT OPERATIONS.
I'M HAVING A HARD TIME TO LET THE USER INPUT THE ARRAY. COULD YOU PLEASE CORRECT MY ERRORS ( I ONLY TRIED FOR CASE 1). THANK YOU
public class highArray
{
private long [] a;
private int nElems;
public highArray (int max)
{
a = new long [max];
nElems= 0;
}
public boolean find (long searchKey)
{
int j;
for(j=0; j<nElems; j++)
if (a[j] == searchKey)
break;
if (j == nElems)
return false;
else
return true;
}
public void insert (long value)
{
a[nElems]= value;
nElems++;
}
public boolean detele ( long value)
{
int j;
for (j=0; j<nElems; j++)
if (value == a[j])
break;
if (j==nElems)
return false;
else
{
for (int k=j; k<nElems; k++)
a[k]= a[k+1];
nElems--;
return true;
}
}
public String toString()
{
String ReturnList = "Your Data entered is: ";
for (int j=0; j<nElems;j++)
{
ReturnList += a[j]+ " ";
}
return ReturnList;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
public class HighArrayApp {
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in );
int length= scan.nextInt();
char option;
highArray arr;
arr = new highArray(length);
String menu = " 1) Insert a Data:";
menu += " 2) Remove a Data:";
menu += " 3) Search a Data:";
menu += " 4) Display all Data";
menu += " 5) Quit ";
System.out.println( ""Please select one of the following option " );
System.out.println( menu ); // print the menu
System.out.print( "Make a selection "
+ "or "5" to quit > " );
option = scan.next( ).charAt( 0 );
while ( option != '5')
{
switch ( option )
{
case '1':
System.out.print( "Please insert your data:" );
length= scan.nextInt();
arr.insert(length);
break;
case '2':
System.out.print( "Please insert data to be removed: " );
break;
case '3':
System.out.print( "Please select the data you would like to search " );
break;
case '4':
System.out.print( "Displaying Data entered:" );
break;
default:
System.out.println( "Please select a correct option" );
}
System.out.print("Make a selection "
+ "or "5" to quit > ");
option = scan.next( ).charAt( 0 );
}
System.out.println( "Thank you, Goodbye" );
}
}
Explanation / Answer
public class highArray
{
private long [] a;
private int nElems;
public highArray (int max)
{
a = new long [max];
nElems= 0;
}
public boolean find (long searchKey)
{
int j;
for(j=0; j<nElems; j++)
if (a[j] == searchKey)
break;
if (j == nElems)
return false;
else
return true;
}
public void insert (long value)
{
a[nElems]= value;
nElems++;
}
public boolean detele ( long value)
{
int j;
for (j=0; j<nElems; j++)
if (value == a[j])
break;
if (j==nElems)
return false;
else
{
for (int k=j; k<nElems; k++)
a[k]= a[k+1];
nElems--;
return true;
}
}
public String toString()
{
String ReturnList = "Your Data entered is: ";
for (int j=0; j<nElems;j++)
{
ReturnList += a[j]+ " ";
}
return ReturnList;
}
}
import java.util.Scanner;
public class HighArrayApp {
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in );
int length= scan.nextInt();
char option;
highArray arr;
arr = new highArray(length);
String menu = " 1) Insert a Data:";
menu += " 2) Remove a Data:";
menu += " 3) Search a Data:";
menu += " 4) Display all Data";
menu += " 5) Quit ";
System.out.println( ""Please select one of the following option " );
System.out.println( menu ); // print the menu
System.out.print( "Make a selection "
+ "or "5" to quit > " );
option = scan.next( ).charAt( 0 );
while ( option != '5')
{
switch ( option )
{
case '1':
System.out.println("Enter number of elements to enter");
length= scan.nextInt();
for(int i=0;i<length;i++)
{
System.out.print( "Please insert your data Number :"+(i+1));
int temp=scan.nextInt();
arr.insert(temp);
}
break;
case '2':
System.out.print( "Please insert data to be removed: " );
break;
case '3':
System.out.print( "Please select the data you would like to search " );
break;
case '4':
System.out.print( "Displaying Data entered:" );
break;
default:
System.out.println( "Please select a correct option" );
}
System.out.print("Make a selection "
+ "or "5" to quit > ");
option = scan.next( ).charAt( 0 );
}
System.out.println( "Thank you, Goodbye" );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.