import javax.swing.*; /* */ public class Misc1 { public static void main(String[] args) { test1( ); test2( ); test3( ); test4( ); int[] scores = { 100, 91, 90, 89, 83, 82, 81, 75, 67, 66, 65, 0 }; for (int i = 0; i < scores.length; i++ ) { System.out.println( "Letter grade for " + scores[i] + " = " + letterGrade( scores[i] )); } } public static void test1( ) { // int[ ] score = new int[100]; // This works fine int[ ] score; score = new int[100]; score[1] = 45; score[2] = 90; score[99] = 80; score[1] = ( score[99] + score[2] ) / 2 ; System.out.println( score[1] ); } public static void test2( ) { double[ ] value = new double[ 4 ]; for ( int i = 0; i < 4; i++ ) value[ i ] = readDouble( ); System.out.println( "Ascending order:" ); for ( int i = 0; i < 4; i++ ) System.out.println( value[ i ] ); System.out.println( "Descending order:" ); for ( int i = 3; i >= 0; i-- ) System.out.println( value[ i ] ); } public static double readDouble( ) { // Commented out, because I got tired of typing in numbers. // return Double.parseDouble( // JOptionPane.showInputDialog( "Next value:" )); return (int) (Math.random( ) * 10000.0) / 100.0; } public static void test3( ) { float[ ] a = new float[5]; // creates a[0] … a[4] char[ ] b = new char[4]; // creates b[0] … b[3] int x = a.length; // x = 5 int y = b.length; // y = 4 // Since a and b are only references to an array, you can change them. a = new float[6]; // (can assign to any float array) int z = a.length; // z = 6 System.out.println( "x: " + x + " y: " + y + " z: " + z); // a.length = 40; // Illegal! Cannot change length float[ ] c; c = null; // System.out.println( c.length ); // Illegal: Null pointer exception double[ ] list = new double[10]; // valid indices are [0..9] // for ( int i = 0; i <= 10; i++ ) // Error: last pass (i=10) generates an // list[i] = 0.0; // ...ArrayIndexOutOfBoundsException for ( int i = 0; i < 10; i++ ) // Fixed list[i] = 0.0; } public static void test4( ) { // int[ ] grade; // int grade[ ]; // int[ ] a, b, c; // a, b, c are all int arrays // int a[ ], b, c[ ]; // a and c are int arrays, and b is just an int int[ ] cutOffs1 = { 90, 82, 75, 66 }; int[ ] cutOffs = new int[4]; cutOffs[0] = 90; cutOffs[1] = 82; cutOffs[2] = 75; cutOffs[3] = 66; for (int i = 0; i < cutOffs.length; i++ ) System.out.println( cutOffs1[i] + " = " + cutOffs[i]); } public static char letterGrade( int numeric ) { int[ ] cutOffs = { 90, 82, 75, 66 }; char[ ] letters = { 'A', 'B', 'C', 'D' }; for ( int i = 0; i < cutOffs.length; i++ ) { if ( numeric >= cutOffs[i] ) { return letters[i]; } } return 'F'; } }