Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java array program problem...input data, max, min...

843785Aug 27 2008 — edited Aug 27 2008
i am making an array program..
get 10 decimal values from user and find min, max, average, sum.,,
but i have some problem in my program..
somethin's wrong... please help me


import java.util.Scanner;

class Calcul
{
final int arraySize = 10;

// Instance Variables
double[] array;
int NUMITEMS;

// Constructor
{
array = new double[ arraySize ] ;
NUMITEMS = 10 ;
}

// Methods

public double sum ( )
{
double sum = 0 ;

for ( double j=0; j < NUMITEMS; j++ )
sum += array[ j ] ;

return sum ;
}

public double average ( )
{
double sum = 0 ;

for ( double j=0; j < NUMITEMS; j++ )
sum += array[ j ] ;

return sum / NUMITEMS ;
}

public double minimum( )
{
double min = array[ 0 ] ;

for ( double j=0; j < NUMITEMS; j++ )

if ( array[j] < min )
min = array[j] ;

return min ;
}

public double maximum( )
{
double max = array[ 0 ] ;

for ( double j=0; j < NUMITEMS; j++ )

if ( array[j] > max )
max = array[j] ;

return max ;
}

}

class InputArray
{

public static final int NUMITEMS = 10;

public static void main ( String[] args )
{

double[] array = new double[10];
double data;

Scanner scan = new Scanner( System.in );

// input the data
for ( int index=0; index < array.length; index++ )
{
System.out.println( "enter a decimal value: " );
data = scan.nextDouble();
array[ index ] = data ;
}

// write out the data
for ( int index=0; index < array.length; index++ )
{
System.out.println( "array[ " + index + " ] = " + array[ index ] );
}

System.out.println( "Average grade: " + average() + " Minimum: " + minimum() +
" Maximum: " + maximum() );
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 24 2008
Added on Aug 27 2008
2 comments
208 views