Codes 3 4 5
Codes 3 4 5
Codes 3 4 5
if ( 2 < 4 )
{
System.out.println( "Two is less than four." );
System.out.println( "Test succeeded." );
}
int num = 8;
class Else {
public static void main( String[] args )
{
int hrs = 21;
if ( hrs < 13 )
{
System.out.println( "Good morning: " + hrs );
}
else if ( hrs < 18 )
{
System.out.println( "Good afternoon: " + hrs );
}
else System.out.println( "Good evening: " + hrs );
}
}
class Switch {
public static void main( String[] args )
{
int month = 2, year = 2016, num = 31;
switch ( month )
{
case 4 : case 6 : case 9 : case 11 : num = 30 ; break ;
case 2 : num = (year % 4 == 0) ? 29 : 28; break ;
}
class For {
public static void main( String[] args )
{
int num = 0;
class DoWhile
{
public static void main( String[] args )
{
int num = 100 ;
do
{
System.out.println( "DoWhile Countup: " + num );
num += 10;
}
while ( num < 10 );
}
}
class Break
{
public static void main( String[] args )
{
for ( int i = 1; i < 4 ; i++ )
{
for ( int j = 1; j < 4 ; j++ )
{
if ( i == 1 && j == 1 )
{
System.out.println( "Continues innerLoop when i=" +i+ " j="+j );
continue;
}
if ( i == 2 && j == 1 )
{
System.out.println( "Breaks innerLoop when i=" +i+ " j="+j );
break;
}
if ( i == 1 && j == 1 )
continue outerLoop;
class Array {
public static void main( String[] args )
{
String[] str = { "Much ", "More", " Java" } ;
num[0] = 100;
num[1] = 200;
str[1] = "Better";
System.out.println( msg );
}
}
class Loops {
public static void main( String[] args )
{
if( args.length > 0 )
{
for( int i = 0; i < args.length; i++ )
{
} }
int j = 0;
int k = 0;
points[0][5] = true;
points[1][6] = true;
points[2][7] = true;
points[3][8] = true;
points[4][9] = true;
}
System.out.print("\n");
}
}
}
class Exceptions
{
public static void main( String[] args )
{
try
{
int num = Integer.parseInt(args[0]);
System.out.println( "You entered: "+num );
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println( "Integer argument required.");
}
catch( NumberFormatException e )
{
System.out.println( "Argument is wrong format.");
}
finally
{
System.out.println( "Program ends." );
}
}
}
class Pi {
public static void main( String[] args )
{
float radius = Float.parseFloat( args[0] );
class Power {
public static void main( String[] args )
{
int num = Integer.parseInt( args[0] );
int square = (int) Math.pow( num, 2 );
int cube = (int) Math.pow( num, 3 );
int sqrt = (int) Math.sqrt( num );
class Compare
{
public static void main( String[] args )
{
float num1 = 24.75f;
int num2 = 25;
try
{
if( args[0].toLowerCase().equals(password) )
{
System.out.println( "Password accepted." );
}
else
{
System.out.println( "Incorrect password." );
}
}
catch(Exception e)
{
System.out.println( "Password required." );
}
}
}
class StringSearch
{
public static void main( String[] args )
{
String[] books = { "Java in easy steps", "XML in easy steps",
"HTML in easy steps","CSS in easy steps", "Gone With the Wind",
"Drop the Defense" };
txt = txt.trim();
System.out.println( "String: " + txt );
System.out.println( "String Length: " + txt.length() );
txt = txt.replace('o','a' );
System.out.println( "String: " + txt );
}
}