public class Array{
int[] T;
int n;
/*constructor*/
public Array(int size ){
T=new int[size];
n=size;
}
/*add element into array*/
public void insert(int position,int elem){
T[position]=elem;
}
/*find minimum algorithm*/
public int findmin(){
int[] resultsArray=new int[2];
int min=T[0],index=0;
for (int i=1;i<n;i++){
if (T[i]<min){
min=T[i];
index=i;
}
}
resultsArray[0]=min;
resultsArray[1]=index;
return resultsArray;
}
}//end class
import java.io.*;
public class Findminimumapp{
/*====main method============*/
public static void main(String[] args) throws IOException {
int n=0;
int min=0;
int index=0;
String str=null;
/*construct new array object*/
System.out.print("give array size then press enter");
str=Utils.getString();
n=Integer.parseInt(str);
Array arrayObj=new Array(n);
/*read array*/
System.out.println("type"+n+"integers in separate lines");
System.out.println("==============");
for(int i=0;i<n;i++){
str=Utils.getString();
arrayObj.insert(i,Integer.parseInt(str));
}
//*printresults*/
min=arrayObj.findMin()[0];
index=arrayObj.findMin()[1];
System.out.println("minimum element in array is"+min);
System.out.println("minimum position is"+(index+1));
}//end main method
}//end class
Are you putting both classes in the same file? Public classes need to be
in a file that matches the class name. So you should have two files:
Array.java and Findminimumapp.java.
Actually, you should use initial caps for class names, so
FindMinimumApp.java. In general you should keep to coding standard -
including indentation, spaces and capitalisation. This is particularly
true if anyone else is ever going to read your code, or you ever read
anyone else's code. Oh and use meaningful variable names, and all the
usual stuff.
Tom Hawtin
imports apply to the file, not the class, and must be grouped at the
start of the file.
"class interface or enum" are the things that can be declared at the top
level, and so could immediately follow the end a top level class
declaration.
Most Java compilers require each public class to be in a file with the
matching name.
In general, if you have multiple classes that are sufficiently unrelated
that they need different import lists, even if only one of them is
public, consider putting them in separate files. You can still group
them together relative to the rest of the program by putting them in a
package.
Patricia
>class interface or enum expected
see
http://mindprod.com/jgloss/compileerrormessages.html#CLASSDCLEXPECTED
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com