You are on page 1of 22

class If {

public static void main( String[] args )


{
if ( 5 > 1 ) System.out.println( "Five is greater than one." );

if ( 2 < 4 )
{
System.out.println( "Two is less than four." );
System.out.println( "Test succeeded." );
}
int num = 8;

if ( (( num > 5 ) && ( num < 10 )) || ( num == 12 ) )


System.out.println( "Number is 6-9 inclusive, or 12" );
}
}

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 ;
}

System.out.println( month+ "/" +year+ ": " +num+ "days" );


}
}

class For {
public static void main( String[] args )
{
int num = 0;

for ( int i = 1; i < 4 ; i++ )


{
System.out.println("Outer Loop i=" +i );

for ( int j = 1; j < 4; j++ )


{
System.out.print( "\tInner Loop j="+j );
System.out.println( "\t\tTotal num="+(++num) );
}
} }}
class While
{
public static void main( String[] args )
{
int num = 100 ;

while ( num > 0 )


{
System.out.println( "While Countdown: " + num );
num -= 10;
}
}
}

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;
}

System.out.println( "Running i=" +i+ " j="+j );


}
}
}
}
class Label
{
public static void main( String[] args )
{
outerLoop: for ( int i = 1; i < 4 ; i++ )
{
for ( int j = 1; j < 4 ; j++ )
{
if ( i == 2 && j == 3 )
{

System.out.println( "Breaks outerLoop when i=" +i+ " j=" +j ) ;


break outerLoop;

if ( i == 1 && j == 1 )

System.out.println( "Continues outerLoop when i=" +i+ " j=" +j ) ;

continue outerLoop;

System.out.println( "Running i=" +i+ " j="+j);


}
}
}
}
class Convert {
public static void main( String[] args )
{
float daysFloat = 365.25f;
String weeksString = "52";

int daysInt = (int) daysFloat;

int weeksInt = Integer.parseInt( weeksString );

int week = ( daysInt / weeksInt );

System.out.println( "Days per week: " + week );


} }

class Array {
public static void main( String[] args )
{
String[] str = { "Much ", "More", " Java" } ;

int[] num = new int[3];

num[0] = 100;
num[1] = 200;

str[1] = "Better";

System.out.println("String array length is " + str.length );


System.out.println("Integer array length is " + num.length );
System.out.println(num[0]+","+num[1]+","+num[2]);
System.out.println(str[0]+str[1]+str[2]);
} }
class Option
{
public static void main( String[] args )
{
if( args[0].equals("-en") )
{
System.out.println( "English option" );
}
else if( args[0].equals( "-es" ) )
{
System.out.println( "Spanish option" );
}
else System.out.println( "Unrecognized option" );
}
}
class Args
{
public static void main( String[] args )
{
if(args.length != 3)
{
System.out.println("Wrong number of arguments");
return;
}

int num1 = Integer.parseInt(args[0]);


int num2 = Integer.parseInt(args[2]);

String msg = args[0] + args[1] + args[2] + "=";

if( args[1].equals("+") ) msg += (num1 + num2);


else if( args[1].equals("-") ) msg += (num1 - num2);
else if( args[1].equals("x") ) msg += (num1 * num2);
else if( args[1].equals("/") ) msg += (num1 / num2);
else msg = "Incorrect operator";

System.out.println( msg );
}
}
class Loops {
public static void main( String[] args )
{
if( args.length > 0 )
{
for( int i = 0; i < args.length; i++ )
{

System.out.println( "args[" +i+ "] is | " + args[i] );

} }

String[] htm = { "HTML5", "in", "easy","steps" };

int j = 0;

while( j < htm.length )


{
System.out.println("htm[" +j+ "] is | " + htm[j] );
j++;
}

String[] xml = { "XML", "in", "easy","steps" };

int k = 0;

if( xml.length > 0 )


do
{
System.out.println( "\t\txml[" +k+ "] is | " + xml[k] );
k++;
} while( k < xml.length );
}}
class Elements {
public static void main( String[] args )
{
int[] kiosk_q1 = { 42000, 48000, 50000 };
int[] kiosk_q2 = { 52000, 58000, 60000 };
int[] kiosk_q3 = { 46000, 49000, 58000 };
int[] kiosk_q4 = { 50000, 51000, 61000 };

int[] outlet_q1 = { 57000, 63000, 60000 };


int[] outlet_q2 = { 70000, 67000, 73000 };
int[] outlet_q3 = { 67000, 65000, 62000 };
int[] outlet_q4 = { 72000, 69000, 75000 };

int[] sum = new int[ 12 ];


int total = 0;

for( int i = 0; i < kiosk_q1.length; i++ )


{
sum[i] = kiosk_q1[i] + outlet_q1[i];
sum[i+3] = kiosk_q2[i] + outlet_q2[i];
sum[i+6] = kiosk_q3[i] + outlet_q3[i];
sum[i+9] = kiosk_q4[i] + outlet_q4[i];
}

for( int i = 0; i < sum.length; i++ )


{
System.out.println( "Month "+(i+1)+" sales:\t" + sum[i]);
total += sum[i];
}

System.out.println( "TOTAL YEAR SALES\t" + total);


} }
class Dimensions {
public static void main( String[] args )
{
boolean[][] points = new boolean[5][20];

points[0][5] = true;
points[1][6] = true;
points[2][7] = true;
points[3][8] = true;
points[4][9] = true;

for( int i = 0; i < points.length; i++ )


{
for(int j = 0; j < points[0].length; j++ )
{
char mark = ( points[i][j] ) ? 'X' : '-';
System.out.print( mark );

}
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] );

float shortPi = (float) Math.PI;

float circ = shortPi * ( radius + radius );

float area = shortPi * (radius * radius);

System.out.print("With Pi commuted from " + Math.PI );


System.out.println(" to " + shortPi + "...");
System.out.println("A circle of radius " + radius + " cm" );
System.out.println("has a circumference of " + circ + " cm");
System.out.println("and an area of " + area + " sq.cm");
}
}

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 );

System.out.println( num +" squared is "+square );


System.out.println( num +" cubed is " +cube );
System.out.println( "Square root of "+num+" is "+sqrt );
}
}
class Round
{
public static void main( String[] args )
{
float num = 7.25f;

System.out.println( num + " rounded is " + Math.round(num) );


System.out.println( num + " floored is " + Math.floor(num) );
System.out.println( num + " ceiling is " + Math.ceil(num) );
}
}

class Compare
{
public static void main( String[] args )
{
float num1 = 24.75f;
int num2 = 25;

System.out.println( "Most is " + Math.max( num1, num2 ) );


System.out.println( "Least is " + Math.min( num1, num2) );
}
}
class Random
{
public static void main( String[] args )
{
float random = (float) Math.random() ;
System.out.println("Random number: " + random );

float multiplied = random * 10;


System.out.println("Multiplied number: " + multiplied );

int randomInt = (int) Math.ceil( multiplied );


System.out.println("Random Integer: " + randomInt );
}
}
class Lottery
{
public static void main( String[] args )
{
int[] nums = new int[50];

// Fill elements 1-49 with integers 1-49.


for( int i = 1; i < 50; i++ ) { nums[i] = i; }

// Shuffle the values in elements 1-49.


for( int i = 1; i < 50; i++ )
{
int r = (int) Math.ceil( Math.random() * 49 ) ;
int temp=nums[i]; nums[i]=nums[r]; nums[r]=temp;
}

// Display the values in elements one to six.


for ( int i = 1; i < 7; i++ )
{
System.out.print(Integer.toString(nums[ i ]) + " ");
if(i % 10 == 0) System.out.println();
}
}
}
class StringLength
{
public static void main( String[] args )
{
String lang = "Java";

String series = " in easy steps";

String title = lang.concat( series );

System.out.print( "\""+title + "\" has ");


System.out.println( title.length()+" characters" );
}
}
class StringComparison
{
public static void main( String[] args )
{
String password = "bingo";

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" };

int counter1 = 0, counter2 = 0, counter3 = 0;

for (int i = 0; i < books.length; i++ )


{
System.out.print( books[i].substring(0,4) + " | " );
if(books[i].endsWith("in easy steps")) counter1++;
if(books[i].startsWith("Java")) counter2++;
if(books[i].indexOf("easy") == -1 ) counter3++;
}

System.out.println("\nFound "+counter1+ " titles from this series");


System.out.println("Found "+counter2+ " Java title");
System.out.println("Found "+counter3+ " other titles");
}
}
class CharacterSwap
{
public static void main( String[] args )
{
String txt = "";

if( txt.isEmpty() ) txt = " Borrocudo ";

System.out.println( "String: " + txt );


System.out.println( "String Length: " + txt.length() );

txt = txt.trim();
System.out.println( "String: " + txt );
System.out.println( "String Length: " + txt.length() );

char initial = txt.charAt(0);


System.out.println( "First Letter: " + initial );

initial = txt.charAt( (txt.length() -1) );


System.out.println( "Last Letter: " + initial );

txt = txt.replace('o','a' );
System.out.println( "String: " + txt );
}
}

You might also like