Array Assignment Solution - Georgia Institute of Technology
Array Assignment Solution
// Java packages
import javax.swing.JOptionPane; // program uses JOptionPane
public class ArrAverage {
// main method begins execution of Java application
public static void main( String args[] )
{
String firstNumber; // first string entered by user
String secondNumber; // second string entered by user
String thirdNumber; // third string entered by user
int array[] = new int[3]; //declare and initiatlize the array
int sum = 0;
int avg = 0;
// read in first number from user as a string
firstNumber = JOptionPane.showInputDialog( "Enter first integer" );
System.out.println(firstNumber);
// read in second number from user as a string
secondNumber = JOptionPane.showInputDialog( "Enter second integer" );
System.out.println(secondNumber);
// read in third number from user as a string
thirdNumber = JOptionPane.showInputDialog( "Enter third integer" );
System.out.println(thirdNumber);
// convert numbers from type String to type int and store them in the array
array[0] = Integer.parseInt( firstNumber );
array[1] = Integer.parseInt( secondNumber );
array[2] = Integer.parseInt( thirdNumber );
// calculate the value for each array element
for (int i = 0; i < array.length; i++ )
sum += array[i];
// divide the "sum" or amount by the length of the array.
avg = sum/array.length;
// display result
JOptionPane.showMessageDialog( null, "The average of " +
array[0] +
" , " +
array[1] +
" , " +
array[2] +
" is " +
avg, "Average of 3 Integers", JOptionPane.PLAIN_MESSAGE );
System.exit( 0 ); // terminate application with window
} // end method main
} // end class ArrAverage
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- multi dimensional arrays edu
- java 2 d array worksheet university of pittsburgh
- arrays ecology lab
- programming in visual basic
- array problems in java northeastern university
- chapter 8 one dimensional arrays
- array assignment solution georgia institute of technology
- arraylists and primitive types school of computing and
- collections and generic data types
Related searches
- seton institute of reconstructive surgery
- michigan institute of real estate classes
- sports institute of tucson
- institute of physics iop
- institute of physics uk
- american institute of physics
- american institute of physics citation
- american institute of physics inc
- chicago institute of plastic surgery
- indian institute of public health
- nigerian institute of international affairs
- eric institute of education science