Generally I will run your Java program on jGrasp with Windows. HighArray.java Te
ID: 3741956 • Letter: G
Question
Generally I will run your Java program on jGrasp with Windows.
HighArray.java Template:
// highArray.java
// demonstrates array class with high-level interface
// to run this program: C>java HighArrayApp //////////////////////////////////////////////////////////////// class HighArray
{
private long[] a; // ref to array a
private int nElems; // number of data items //-----------------------------------------------------------
public HighArray(int max) {
a = new long[max]; nElems = 0;
}
// constructor
// create the array // no items yet
//----------------------------------------------------------- public boolean find(long searchKey)
{
int j;
for(j=0; j<nElems; j++)
if(a[j] == searchKey)
// find specified value
// for each element, // found item?
break; if(j == nElems)
return false; else
public void insert(long value) {
a[nElems] = value; nElems++;
}
// exit loop before end // gone to end?
// yes, can’t find it
// no, found it //-----------------------------------------------------------
return true; } // end find()
//----------------------------------------------------------- public boolean delete(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; }
// look for it
// can’t find it
// found it
// move higher ones down // decrement size
} // end delete() //----------------------------------------------------------- public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + “ “); // display it System.out.println(“”);
}
//-----------------------------------------------------------
} // end class HighArray //////////////////////////////////////////////////////////////// class HighArrayApp
{
public static void main(String[] args)
{
int maxSize = 100;
HighArray arr;
arr = new HighArray(maxSize);
arr.insert(77); arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33);
arr.display();
int searchKey = 35;
if( arr.find(searchKey) )
// array size
// reference to array // create the array
// insert 10 items
System.out.println(“Found “ + searchKey); else
System.out.println(“Can’t find “ + searchKey);
arr.delete(00); arr.delete(55); arr.delete(99);
arr.display();
} // end main()
} // end class HighArrayApp
// delete 3 items
// display items again
////////////////////////////////////////////////////////////////
Explanation / Answer
Please find the code below.
CODE
===================
class unfid {
private long[] a; // ref to array a
private int nElems; // number of data items //-----------------------------------------------------------
public unfid(int max) {
a = new long[max]; nElems = 0;
}
public void insert(long value) {
a[nElems] = value; nElems++;
}
// constructor
// create the array // no items yet
//-----------------------------------------------------------
public boolean find(long searchKey) {
int j;
for(j=0; j<nElems; j++) {
if(a[j] == searchKey)
// find specified value
// for each element, // found item?
break;
if(j == nElems)
return false;
}
// exit loop before end // gone to end?
// yes, can’t find it
// no, found it //-----------------------------------------------------------
return true;
} // end find()
//-----------------------------------------------------------
public boolean delete(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--;
}
// look for it
// can’t find it
// found it
// move higher ones down // decrement size
}
return true;
}// end delete() //-----------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it System.out.println(“”);
}
public long getMax() {
if(a.length == 0)
return -1;
long max = a[0];
for(int i=1; i<a.length; i++) {
if(a[i] > max) {
max = a[i];
}
}
return max;
}
public long getMin() {
if(a.length == 0)
return -1;
long min = a[0];
for(int i=1; i<a.length; i++) {
if(a[i] < min) {
min = a[i];
}
}
return min;
}
//-----------------------------------------------------------
} // end class HighArray ////////////////////////////////////////////////////////////////
public class unfidApp
{
public static void main(String[] args)
{
int maxSize = 100;
unfid arr;
arr = new unfid(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
int searchKey = 35;
if( arr.find(searchKey) )
// array size
// reference to array // create the array
// insert 10 items
System.out.println("Found " + searchKey);
else
System.out.println("Can’t find “" + searchKey);
arr.delete(00);
arr.delete(55);
arr.delete(99);
arr.display();
long y = arr.getMax();
System.out.println(y);
long z = arr.getMin();
System.out.println(z);
} // end main()
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.