// Demos - Here is the bare minimum incantation to produce output. // There's at least a dozen ways to screw this up. // Let's make as many mistakes as possible, and remember the // error messages and whether compile versus run-time. public class ViewlocityExercisesIntro { public static void main( String[] args ) { System.out.println( "hello" ); } } // Spell the name of the class wrong D:\java> javac ViewlocityExercisesIntro.java ViewlocityExercisesIntro.java:1: class ViewlocityExercisesIntroX is public, should be declared in a file named ViewlocityExercisesIntroX.java public class ViewlocityExercisesIntroX { ^ 1 error // Spell the name of the class wrong // leave out 1st public // delete ViewlocityExercisesIntro.class D:\java> java ViewlocityExercisesIntro Exception in thread "main" java.lang.NoClassDefFoundError: ViewlocityExercisesIntro // leave out static D:\java> java ViewlocityExercisesIntro Exception in thread "main" java.lang.NoSuchMethodError: main // change void to int D:\java> javac ViewlocityExercisesIntro.java ViewlocityExercisesIntro.java:2: missing return statement public static int main( String[] args ) { ^ 1 error // add "return 123;" D:\java> java ViewlocityExercisesIntro Exception in thread "main" java.lang.NoSuchMethodError: main // leave out "String[] args" D:\java> java ViewlocityExercisesIntro Exception in thread "main" java.lang.NoSuchMethodError: main // leave out just "[]" D:\java> java ViewlocityExercisesIntro Exception in thread "main" java.lang.NoSuchMethodError: main // Demos - Something as simple as a variable gets complicated very quick. // Java does not have "global" variables. // This is probably pre-mature, but people may wonder about static. public class ViewlocityExercisesIntro { public static void main( String[] args ) { int variable; System.out.println( "variable is " + anObject.variable ); } } // D:\java> javac ViewlocityExercisesIntro.java // ViewlocityExercisesIntro.java:67: variable variable might not have // been initialized // System.out.println( "variable is " + variable ); // ^ // 1 error // initialize variable // move variable out of main() and into ViewlocityExercisesIntro // D:\java> javac ViewlocityExercisesIntro.java // ViewlocityExercisesIntro.java:67: non-static variable variable cannot // be referenced from a static context // System.out.println( "variable is " + variable ); // ^ // 1 error // put static in front of "int variable" and remove initialization // put private in front of "static int variable" // create SeparateClass and move "private static int variable" to it // D:\java> javac ViewlocityExercisesIntro.java // ViewlocityExercisesIntro.java:68: cannot resolve symbol // symbol : variable variable // location: class ViewlocityExercisesIntro // System.out.println( "variable is " + variable ); // ^ // 1 error // change private to public on "static int variable" // same error // add "SeparateClass." to variable in println() // remove static from "public static int variable" // D:\java> javac ViewlocityExercisesIntro.java // ViewlocityExercisesIntro.java:68: non-static variable variable cannot // be referenced from a static context // System.out.println( "variable is " + SeparateClass.variable ); // ^ // 1 error // add "SeparateClass anObject = new SeparateClass()" to main() // change "SeparateClass.variable" to anObject.variable // boolean type, initialization, boolean expression, if-else public class ViewlocityExercisesIntro { public static void main( String[] args ) { boolean variable = true; if (variable == true) System.out.println( "variable is true" ); else System.out.println( "variable is false" ); } } // variable is true // if, else if, else public class ViewlocityExercisesIntro { public static void main( String[] args ) { int number = 1000; if (number < 10) System.out.println( "number is small" ); else if (number < 100) System.out.println( "number is medium" ); else if (number < 1000) System.out.println( "number is large" ); else System.out.println( "number is enormous" ); } } // number is enormous // another boolean expression public class ViewlocityExercisesIntro { public static void main( String[] args ) { boolean variable = false; if (variable) System.out.println( "variable is true" ); else System.out.println( "variable is false" ); } } // variable is false // designating a "block" of statements public class ViewlocityExercisesIntro { public static void main( String[] args ) { boolean variable = false; if (variable) { System.out.print( "variable is" ); System.out.println( " true" ); } else { System.out.print( "variable is" ); System.out.println( " false" ); } } } // variable is false // forgetting "{ }"s public class ViewlocityExercisesIntro { public static void main( String[] args ) { boolean variable = true; if (variable) { System.out.print( "variable is" ); System.out.println( " true" ); } else System.out.print( "variable is" ); System.out.println( " false" ); } } // variable is true // false // while loop, i = i + 1, i += 1, i++ public class ViewlocityExercisesIntro { public static void main( String[] args ) { int i = 1; while (i < 11) { System.out.print( i + " " ); i += 1; } System.out.println(); } } // 1 2 3 4 5 6 7 8 9 10 // for loop public class ViewlocityExercisesIntro { public static void main( String[] args ) { for (int i=1; i < 11; i++) System.out.print( i + " " ); System.out.println(); } } // 1 2 3 4 5 6 7 8 9 10 // infinite loop, break statement public class ViewlocityExercisesIntro { public static void main( String[] args ) { int i = 1; while (true) { System.out.print( i + " " ); i += 1; if (i == 11) break; } System.out.println(); } } // 1 2 3 4 5 6 7 8 9 10 // Problem - produce the following output // 1 2 3 4 5 6 7 8 9 10 // 1 2 3 4 5 6 7 8 9 // 1 2 3 4 5 6 7 8 // 1 2 3 4 5 6 7 // 1 2 3 4 5 6 // 1 2 3 4 5 // 1 2 3 4 // 1 2 3 // 1 2 // 1 // Answer - loop in a loop public class ViewlocityExercisesIntro { public static void main( String[] args ) { for (int i=11; i > 1; i--) { for (int j=1; j < i; j++) System.out.print( j + " " ); System.out.println(); } } } // integer division, modulus operator public class ViewlocityExercisesIntro { public static void main( String[] args ) { for (int i=1; i < 11; i++) System.out.print( (i / 4) + " " ); System.out.println(); for (int i=1; i < 11; i++) System.out.print( (i % 4) + " " ); System.out.println(); } } // 0 0 0 1 1 1 1 2 2 2 // 1 2 3 0 1 2 3 0 1 2 // Problem - produce the following output // 2 4 6 8 10 // Answer - for, if, modulus operator public class ViewlocityExercisesIntro { public static void main( String[] args ) { for (int i=1; i < 11; i++) if (i % 2 == 0) System.out.print( i + " " ); System.out.println(); } } // array, initialization, start at index of 0 public class ViewlocityExercisesIntro { public static void main( String[] args ) { int[] array = new int[6]; for (int i=0; i < 6; i++) System.out.print( array[i] + " " ); System.out.println(); } } // 0 0 0 0 0 0 // array, alternate initialization, no "new" public class ViewlocityExercisesIntro { public static void main( String[] args ) { int[] array = { 6, 5, 4, 3, 2, 1 }; for (int i=0; i < 6; i++) System.out.print( array[i] + " " ); System.out.println(); } } // 6 5 4 3 2 1 // arrays are smart - they know their own length public class ViewlocityExercisesIntro { public static void main( String[] args ) { int[] array = { 6, 5, 4, 3, 2, 1 }; for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); } } // 6 5 4 3 2 1 // Problem - total the array of integers public class ViewlocityExercisesIntro { public static void main( String[] args ) { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int total; System.out.println( "total is " + total ); } } // total is 55 // Answer - initialize total, step through array public class ViewlocityExercisesIntro { public static void main( String[] args ) { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int total = 0; for (int i=0; i < array.length; i++) total += array[i]; System.out.println( "total is " + total ); } } // total is 55 // Problem - send the command line arguments to standard out public class ViewlocityExercisesIntro { public static void main( String[] args ) { } } // D:\java> java ViewlocityExercisesIntro one two three four // one // two // three // four // Answer - args is an array public class ViewlocityExercisesIntro { public static void main( String[] args ) { for (int i=0; i < args.length; i++) System.out.println( args[i] ); } } // Random class, nextInt() method import java.util.Random; public class ViewlocityExercisesIntro { public static void main( String[] args ) { Random randomNumberGenerator = new Random(); int[] array = new int[15]; for (int i=0; i < array.length; i++) array[i] = randomNumberGenerator.nextInt( 8 ); for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); } } // 4 3 0 4 0 4 3 6 7 1 7 7 2 4 1 // Problem - count the number of occurrences of each integer import java.util.Random; public class ViewlocityExercisesIntro { public static void main( String[] args ) { Random randomNumberGenerator = new Random(); int[] numberArray = new int[15]; for (int i=0; i < numberArray.length; i++) numberArray[i] = randomNumberGenerator.nextInt( 8 ); for (int i=0; i < numberArray.length; i++) System.out.print( numberArray[i] + " " ); System.out.println(); int[] countingArray = new int[8]; } } // 7 7 5 5 7 1 4 0 3 5 3 4 3 4 0 // number of 0s 1s 2s 3s 4s 5s 6s 7s // 2 1 0 3 3 3 0 3 // Answer - using an array element to index into an array import java.util.Random; public class ViewlocityExercisesIntro { public static void main( String[] args ) { Random randomNumberGenerator = new Random(); int[] numberArray = new int[15]; for (int i=0; i < numberArray.length; i++) numberArray[i] = randomNumberGenerator.nextInt( 8 ); for (int i=0; i < numberArray.length; i++) System.out.print( numberArray[i] + " " ); System.out.println(); int[] countingArray = new int[8]; for (int i=0; i < numberArray.length; i++) countingArray[ numberArray[i] ]++; System.out.println( "number of 0s 1s 2s 3s 4s 5s 6s 7s" ); System.out.print( " " ); for (int i=0; i < countingArray.length; i++) System.out.print( countingArray[i] + " " ); System.out.println(); } } // 4 3 0 3 3 7 6 1 6 1 3 0 2 4 0 // number of 0s 1s 2s 3s 4s 5s 6s 7s // 3 2 1 4 2 0 2 1 // create a new abstraction - write a function // a function encapsulates reusable functionality // a function has a "signature" - name, arguments (number and type), return type // in Java, they must be static (sort of) public class ViewlocityExercisesIntro { public static int computeTheTotal( int[] xyz ) { int total = 0; for (int i=0; i < xyz.length; i++) total += xyz[i]; System.out.println( "computeTheTotal: total is " + total ); return total; } public static void main( String[] args ) { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; System.out.println( "main: the total is " + computeTheTotal( array ) ); } } // computeTheTotal: total is 55 // main: the total is 55 // functions don't have to return anything public class ViewlocityExercisesIntro { public static void swap( int[] xyz ) { int temp = xyz[0]; xyz[0] = xyz[1]; xyz[1] = temp; } public static void main( String[] args ) { int[] array = { 123, 321 }; System.out.println( "before swap - " + array[0] + ", " + array[1] ); swap( array ); System.out.println( "after swap -- " + array[0] + ", " + array[1] ); } } // before swap - 123, 321 // after swap -- 321, 123 // Problem - write a sort() function public class ViewlocityExercisesIntro { public static void main( String[] args ) { int[] array = { 8, 6, 4, 2, 9, 1, 7, 3, 5 }; sort( array ); for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); } } // 1 2 3 4 5 6 7 8 9 // Answer - brain-dead bubble sort public class ViewlocityExercisesIntro { public static void sort( int[] xyz ) { for (int temp, i = xyz.length-1; i > 1; i--) for (int j=0; j < i; j++) if (xyz[j] > xyz[j+1]) { temp = xyz[j]; xyz[j] = xyz[j+1]; xyz[j+1] = temp; } } public static void main( String[] args ) { int[] array = { 8, 6, 4, 2, 9, 1, 7, 3, 5 }; sort( array ); for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); } } // 1 2 3 4 5 6 7 8 9 // Java does not allow passed arguments to stay changed // (pass by value) public class ViewlocityExercisesIntro { public static void changeTheArgument( String str ) { str = str + str; System.out.println( "str is " + str ); } public static void main( String[] args ) { String object = "abcdef"; changeTheArgument( object ); System.out.println( "object is " + object ); } } // str is abcdefabcdef // object is abcdef // Java DOES allow the "contents" of passed objects to stay changed // actually passing an "object reference" // we would call this a "pointer" it the word didn't belong to the devil // not changing the object reference, changing what it refers to public class ViewlocityExercisesIntro { public static void changeTheArgument( StringBuffer str ) { str.append( str ); System.out.println( "str is " + str ); } public static void main( String[] args ) { StringBuffer object = new StringBuffer( "abcdef" ); changeTheArgument( object ); System.out.println( "object is " + object ); } } // str is abcdefabcdef // object is abcdefabcdef // The same is true for primitive types. An argument can be changed in a // function, but it won't stay changed when the function returns. public class ViewlocityExercisesIntro { public static void changeTheArgument( int arg ) { arg = arg + arg; System.out.println( "arg is " + arg ); } public static void main( String[] args ) { int number = 123; changeTheArgument( number ); System.out.println( "number is " + number ); } } // arg is 246 // number is 123 // this be be "fixed" by converting the int to an object (an array) public class ViewlocityExercisesIntro { public static void changeTheArgument( int[] arg ) { arg[0] = arg[0] + arg[0]; System.out.println( "arg[0] is " + arg[0] ); } public static void main( String[] args ) { int[] number = { 123 }; changeTheArgument( number ); System.out.println( "number[0] is " + number[0] ); } } // arg[0] is 246 // number[0] is 246 // another "one element array parameter" example public class ViewlocityExercisesIntro { public static boolean count( int[] value ) { if (value[0] > 10) return false; System.out.print( value[0] + " " ); value[0]++; return true; } public static void main( String[] args ) { int[] i = { 1 }; while ( count(i) ) { } System.out.println(); } } // 1 2 3 4 5 6 7 8 9 10 // replacing the loop with recursion public class ViewlocityExercisesIntro { public static void count( int value ) { if (value > 1) count( value - 1 ); System.out.print( value + " " ); } public static void main( String[] args ) { count( 10 ); System.out.println(); } } // ----- print() first ----- // 10 9 8 7 6 5 4 3 2 1 // ----- print() last ----- // 1 2 3 4 5 6 7 8 9 10