Code 1
int sum = 7;
if ( sum > 20 )
{
System.out.print("You win ");
}
else
{
System.out.print("You lose ");
}
System.out.println("the prize.");
Code 2
int sum = 21;
if ( sum != 20 )
System.out.print("You win ");
else
System.out.print("You lose ");
System.out.println("the prize.");
Code 3
int count = 0;
while ( count <= 6 )
{
System.out.print( count + " " );
count = count + 2;
}
System.out.println( );
Code 4
int area;
String name;
Code 5
String strA;
String strB = new String("Cheese");
Code 6
String strA = new String("Roasted ");
strA = new String("Toasted ");
strA = new String("Fried ");
strA = new String("Baked ");
strA = new String("Beans ");
Code 7
String strA = new String("Roasted ");
String strB = new String("Acorns ");
strA = strB;
if ( strA == strB )
System.out.println("Two copies of a reference.");
else
System.out.println("Two different references.");
Code 8
String strA = new String("Roasted ");
String strB = strA;
String strC = strA;
String strD = strA;
String strE = strA;
Code 9
int x = 0;
while ( x < 500 )
{
System.out.println( x );
x = x + 5;
}
9a
for ( int x = 0; x < 500; x+=5 )
System.out.println( x );
9b
for ( float x = 0.0; x < 500.0; x += 5.0 )
System.out.println( x );
9c
for ( int x = 500; x >= 0; x-=5 )
System.out.println( x );
Code 10
for ( int j = -5; ________ ; j++ )
{
System.out.print( j + " " );
}
System.out.println( );
Code 11
for ( int j = 0; j < 5; j++ )
{
for ( int k = 0; k < 10 ; k++ )
System.out.print( "*" );
System.out.println( );
}
11a
**********
**********
**********
**********
**********
11b
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
11c
*****
*****
*****
*****
*****
*****
*****
*****
*****
*****
11d
*
*
*
*
*
Code 12
for ( int count = 0; count <= 20; count+=2 )
System.out.print( count + " " );
System.out.println( );
Code 13
double[][] values =
{ {1.2, 9.0, 3.2},
{9.2, 0.5, 1.5, -1.2},
{7.3, 7.9, 4.8} } ;
Code 14
double[][] values =
{ {1.2, 9.0, 3.2},
{9.2, 0.5, 1.5, -1.2},
{7.3, 7.9, 4.8} } ;