//InitArray.java
// initializing an array
public class InitArray {
public static void main( String args[] )
{ String output = "";
int n[]; // declare reference to an array
n = new int[ 10]; // dynamically allocate array
output += "Subscript\tValue\n";
for ( int i = 0; i < n.length; i++ )
output += i + "\t\t" + n[ i ] + "\n";
System.out.print("Default Initialization of Array of int Values\n" + output);
}
}
-------------------------------------
//InitArray.java
// initializing an array with a declaration
public class InitArray {
public static void main( String args[] )
{
String output = "";
// Initializer list specifies number of elements and
// value for each element.
int n[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
output += "Subscript\tValue\n";
for ( int i = 0; i < n.length; i++ )
output += i + "\t\t" + n[ i ] + "\n";
System.out.print("Initializing an Array with a List of Values in Brackets\n" + output);
}
}
//InitArray.java
// initialize array n to the even integers from 2 to 20
public class InitArray {
public static void main( String args[] )
{
final int ARRAY_SIZE = 10;
int n[]; // reference to int array
String output = "";
n = new int[ARRAY_SIZE]; // allocate array
// Set the values in the array
for ( int i = 0; i < n.length; i++ )
n[ i ] = 2 + 2 * i;
output += "Subscript\tValue\n";
for ( int i = 0; i < n.length; i++ )
output += i + "\t\t" + n[ i ] + "\n";
System.out.print("Initializing to Even Numbers from 2 to 20\n" + output);
}
}
//SumArray.java
// Compute the sum of the elements of the array
public class SumArray {
public static void main( String args[] )
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int total = 0;
for ( int i = 0; i < a.length; i++ )
total += a[i];
System.out.println("Sum the Elements of an Array\nTotal of array elements: " + total);
}
}
//StudentPoll.java
// Student poll program
public class StudentPoll {
public static void main( String args[] )
{
int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
int frequency[] = new int[ 11 ];
String output = "";
for ( int answer = 0; answer < responses.length; answer++)
++frequency[responses[ answer ] ];
output += "Rating\tFrequency\n";
for ( int rating = 1; rating < frequency.length; rating++ )
output += rating + "\t" + frequency[ rating ] + "\n";
System.out.println("Student Poll Program\n" + output);
}
}
//Histogram.java
// Histogram printing program
public class Histogram {
public static void main( String args[] )
{
int n[] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
String output = "";
output += "Element\tValue\tHistogram";
for ( int i = 0; i < n.length; i++ ) {
output += "\n" + i + "\t" + n[ i ] + "\t";
for ( int j = 1; j <= n[ i ]; j++ ) // print a bar
output += "*";}
System.out.println("Histogram Printing Program\n" + output);
}
}
//RollDie.java
// Roll a six-sided die 6000 times
public class RollDie {
public static void main( String args[] )
{
int face,frequency[] = new int[ 7 ];
String output = "";
for ( int roll = 1; roll <= 6000; roll++ ) {
face = 1 + (int) ( Math.random() * 6 );
++frequency[face ];
}
output += "Face\tFrequency";
for ( face = 1; face < frequency.length; face++ )
output += "\n" + face + "\t" + frequency[ face ];
System.out.println("Rolling a Die 6000 Times\n" + output);
}
}