I seem to be stuck on what seems to be a pretty basic assignment. I was wondering if anyone could possibly lead me to the right direction and explain to me what is wrong. I have created an array with premade values inserted. Now I have to get the min/max values of this array and display them as well. I keep getting these two errors
".java:126: error: method getMax in class HighArray cannot be applied to given types;"
".java:126: error: method getMin in class HighArray cannot be applied to given types;"
If anyone could possibly help me out and explain why this is, it would be greatly appreciated. Thank you!
class HighArray
{
private long[] a;
private int nElems;
public HighArray(int max)
{
a = new long[max];
nElems = 0;
}
//Search Method
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;
}
//Insert method
public void insert(long value)
{
a[nElems] = value;
nElems++;
}
//Delete method
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;
}
}
//Display Array Contents
public void display()
{
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println(" ");
}
//Max Method
public static int getMax(int[] a)
{
int maxValue = a[0];
for(int i=1;i < a.length;i++)
{
if(a[i] > maxValue)
{
maxValue = a[i];
System.out.print("The max value is" + a[i]);
}
}
return maxValue;
}
//Min Method
public static int getMin(int[] a)
{
int minValue = a[0];
for(int i=1;i<a.length;i++)
{
if(a[i] < minValue)
{
minValue = a[i];
System.out.print("The min value is" + a[i]);
}
}
return minValue;
}
}
public class Assignment
{
public static void main(String[] args)
{
int maxSize = 100;
HighArray 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();
arr.getMax();
arr.getMin();
int searchKey = 35;
if( arr.find(searchKey) )
System.out.println("Found" + searchKey);
else
System.out.println("Can't Find " + searchKey);
arr.delete(00);
arr.delete(55);
arr.delete(99);
arr.display();
}
}
Variable shadowing is the problem.
So,
Code
Same for min
Methods:
public static int getMax(int[] a)
andpublic static int getMin(int[] a)
have
int[]
as their input parameter,but they are later called without any parameters:
arr.getMax();
andarr.getMin();
.This is the cause of the error you are getting from the compiler.
EDIT:
You probably want to modify your methods not to be static and not to have any input parameters (the array
a
would be used directly from the object and not passed to the method), so you can use methods on the object of the class like this:arr.getMax();
.To do so change the code in the following way:
public static int getMax(int[] a)
-->public long getMax()
public static int getMin(int[] a)
-->public long getMin()
* Note: The return type of
getMax
andgetMin
methods is changed fromint
tolong
, becauselong
is the type of array in theHighArray
class.You have to change you getMin and getMax method.
It should also not be static otherwise you can't access the value array in that method. Also your array is of type long so the return value should be long too.