Single and Compound Statements
A single statement is a unit of action where we are telling the computer to do something.
Example showing the 3 ways of writing loops.
File: "loop1.java"
public class loop1
{
public static void main(String args[] )
{
int x1 = 4 ;
while( x1 < 10 )
{
System.out.println("First while loop x1:" + x1 ) ;
x1++ ;
}
do
System.out.println("Do while: x1:" + x1 ) ;
while( x1 < 5 ) ;
for( int x2=0 ; x2 < 10 ; x2 = x2 + 2 ) //x2++
{
System.out.println("For loop x2:" + x2 ) ;
}
}
}
File: "loop2.java"
public class loop2
{
public static void main(String args[] )
{
int arr1[] = new int[10] ;
for( int i1=0 ; i1<10 ; i1++ )
{
arr1[i1] = i1*2 ;
}
for( int i1=0 ; i1<10 ; i1++ )
{
System.out.print( arr1[i1] + " ") ;
}
System.out.println( ) ;
for( int x1 : arr1 )
{
System.out.print( x1 + " " ) ;
}
}
}
File: "loop3.java"
public class loop3
{
public static void function1()
{
for( int i1=0 ; i1<10 ; i1++ )
{
if ( i1 > 7 )
break ;
if ( i1 > 4 )
continue ;
System.out.println( "i1:" + i1 ) ;
} //for
System.out.println( "Outside the loop." ) ;
}
public static void function2()
{
for( int i1=0 ; i1<5 ; i1++ )
{
System.out.println( "i1:" + i1 ) ;
for( int j1=0 ; j1 < 3 ; j1++ )
{
System.out.println( "j1:" + j1 ) ;
if ( i1 > 2 )
break ;
if ( j1 > 1 )
continue ;
} //for
if( i1 > 2 )
break ;
} //for
System.out.println( "Outside the loop." ) ;
}
public static void function3()
{
int j1 = 0 ;
for( int i1=0 ; i1<5 ; i1++ )
{
System.out.println( "i1:" + i1 ) ;
if ( i1 > 1 )
continue ;
for( ; j1 < 3 ; j1++ )
{
if ( j1 > 1 )
continue ;
System.out.println( "j1:" + j1 ) ;
} //for
if( i1 > 2 )
break ;
} //for
System.out.println( "Outside the loop." ) ;
}
public static void function4()
{
label1:
for( int i1=0 ; i1<5 ; i1++ )
{
label2:
for( int j1=0 ; j1<5 ; j1++ )
{
System.out.println( "j1:" + j1 ) ;
//break label1 ;
continue label1 ;
} //for
System.out.println( "i1:" + i1 ) ;
} //for
}
public static void function5()
{
label1:
for( int i1=0 ; i1<5 ; i1++ )
{
if( i1 < 2 )
continue label1 ; //break label1 ;
//break label1 ;
// continue label1 ;
System.out.println( "i1:" + i1 ) ;
} //for
}
public static void function6()
{
for( int i1=0 ; i1<5 ; i1++ )
{
if( i1 < 2 )
continue ; //break label1 ;
//break label1 ;
// continue label1 ;
System.out.println( "i1:" + i1 ) ;
} //for
}
public static void main(String args[] )
{
function6() ;
}
}
File: "loop4.java"
public class loop4
{
public static void function1()
{
for( int i1=0 ; i1<10 ; i1++ )
{
if ( i1 < 5 || i1 == 7 )
continue ;
System.out.println( "i1:" + i1 ) ;
for( int j1=0 ; j1< 3 ; j1++ )
{
if ( j1 < 2 )
continue ;
System.out.println( "j1:" + j1 ) ;
if( j1 == 2 )
break ;
} //for
} //for
}
public static void function2()
{
for( int i1=0 ; i1<7 ; i1++ )
{
System.out.println( "i1:" + i1 ) ;
for( int j1=0 ; j1< 3 ; j1++ )
{
if ( i1 < 3 || i1 == 5 )
continue ;
if ( j1 < 2 )
continue ;
System.out.println( "j1:" + j1 ) ;
if( j1 == 2 )
break ;
} //for
} //for
}
public static void function3()
{
int i1 = 0 ;
outer:
while(true)
{
System.out.println("Outer while loop");
while(true)
{
i1++;
System.out.println("i1 = " + i1);
if(i1 == 1) {
System.out.println("continue");
continue;
}
if(i1 == 3) {
System.out.println("continue outer");
continue outer;
}
if(i1 == 5) {
System.out.println("break");
break;
}
if(i1 == 7) {
System.out.println("break outer");
break outer;
}
} //while
} //while
}
public static void main(String args[] )
{
function2() ;
} //main
}
File: "goto1.java"
public class goto1
{
public static void main(String args[] )
{
int i1 = 0 ;
int j1 = 1 ;
outer:
//System.out.println("Testing" ) ;
for( i1=0 ; i1< 10 ; i1++ )
{
System.out.println("i1:" + i1 ) ;
if ( i1 == 5 )
{
j1++ ;
System.out.println("j1:" + j1 ) ;
if( j1 == 5 )
break ;
for ( int k1=0 ; k1 < 5 ; k1++ )
{
System.out.println("k1:" + k1 ) ;
if( k1 == 3 )
continue outer ; //break outer ;
} //for
}//if
} //for
}
}
File: "goto1.java"
class home
{
int noOfBathrooms ;
int noOfBedrooms ;
}
public class return1
{
public static int function1()
{
int x1 = 4 ;
// return( x1 * 3 ) ;
return 24 ;
}
public static home function2()
{
return new home() ;
}
public static void function3()
{
int x1 = 4 ;
System.out.println( "Step 1" ) ;
if ( x1 == 4 )
return ;
System.out.println( "Step 2" ) ;
}
public static void main(String args[] )
{
// System.out.println( function1() ) ;
function3() ;
}
}
1)
Given an array of some strings find the longest prefix. As an example:
String arr1[] = {"Flowers" , "Flow" , "Float" } ;
The longest prefix is "Flo". Make sure to account for the cases in which the length of the
word may be small such as :
String arr1[] = {"Flowers" , "F" , "Float" } ;
Longest prefix is "F" in the above case.
2)
Given an array
With N being the number of elements in the array and K is the number of cyclic rotation compute the end result.
Arr1 = { 3 , 8, 9, 7, 6 }
K=3
Rotation1 will produce :
Arr1 = { 6, 3, 8, 9, 7 }
Rotation 2 will produce
Arra1 = { 7,6 ,3 , 8, 9 }
public static void doRotations( int arr1[] , int size , int K )
3)
Write a program to print out the prime factors of a number.
Prime factors are all prime numbers that when multiplied together give us the
actual number.
8 == 2 * 2 * 2
15 == 3 * 5
10 = 2 * 5
4)
Page 113 Ex 14
5)
Page 88 Exercises 2 and 3
6)
Page 128
Exercise 9 and 10
Ex 9 . Hint run a for loop keeping track of the last 2 values in the Fibonacci sequence.
3)
File:"primefactor.java"
import java.util.* ;
public class primefactor
{
public static void printFactors( int inputNo )
{
boolean notDone = true ;
while( notDone )
{
notDone = false ;
for( int i1 = 2 ; i1 < inputNo ; i1++ )
{
if( inputNo % i1 == 0 )
{
notDone = true ;
System.out.println(i1 ) ;
inputNo = inputNo / i1 ;
break ;
}
} //for
} //while
System.out.println( inputNo ) ;
}
public static void main(String args[] )
{
int no1 ;
Scanner reader = new Scanner(System.in) ;
System.out.println("Please Enter a number" ) ;
no1 = reader.readInt() ;
printFactors( no1 ) ;
} //main
}
6)
File: "vampire1.java"
//Trailing zerores mean a zero cannot exist on the right most
public class vampire1
{
public static void check(int left , int right , int result )
{
if( left * right == result )
{
System.out.println( "Number:" + result + " Left Part:" + left +
" Right Part:" + right ) ;
}
}
public static void processSubParts(int result , int arr1[] , int arr2[] )
{
String str1 = Integer.toString( result ) ;
String firstNo ; String secondNo ;
int firstInt = 0 ; int secondInt = 0 ;
//Process the 3 rows
firstNo = "" + str1.charAt( arr1[0] ) + str1.charAt( arr1[1] ) ;
secondNo = "" + str1.charAt( arr2[0] ) + str1.charAt( arr2[1] ) ;
//There will be 4 combinations
firstInt = Integer.parseInt( firstNo ) ;
secondInt = Integer.parseInt( secondNo ) ;
//System.out.println("First If" ) ;
check( firstInt, secondInt , result ) ;
secondNo = "" + secondNo.charAt(1) + secondNo.charAt(0) ;
secondInt = Integer.parseInt( secondNo ) ;
check( firstInt, secondInt , result ) ;
firstNo = "" + str1.charAt( arr1[1] ) + str1.charAt( arr1[0] ) ;
secondNo = "" + str1.charAt( arr2[0] ) + str1.charAt( arr2[1] ) ;
firstInt = Integer.parseInt( firstNo ) ;
secondInt = Integer.parseInt( secondNo ) ;
check( firstInt, secondInt , result ) ;
firstNo = "" + str1.charAt( arr1[1] ) + str1.charAt( arr1[0] ) ;
secondNo = "" + str1.charAt( arr2[1] ) + str1.charAt( arr2[0] ) ;
firstInt = Integer.parseInt( firstNo ) ;
secondInt = Integer.parseInt( secondNo ) ;
check( firstInt, secondInt , result ) ;
}
public static void process( int input1 )
{
int row1a[] = { 0, 1 } ; int row1b[] = { 2, 3 } ;
int row2a[] = { 0, 2 } ; int row2b[] = { 1, 3 } ;
int row3a[] = { 0, 3 } ; int row3b[] = { 1, 2 } ;
processSubParts( input1, row1a , row1b ) ;
processSubParts( input1, row2a , row2b ) ;
processSubParts( input1, row3a , row3b ) ;
}
public static void printvampireNumbers()
{
//process( 1260 ) ;
//9999
for( int i1=1000 ; i1 <= 9999 ; i1++ )
{
process( i1 ) ;
}
}
public static void main(String[] args)
{
printvampireNumbers() ;
}
}