/* * An intro to the look and built-in keywords of Java */ import java.lang.Math; import java.util.Scanner; class JavaIntro { public static void main(String[] arghs) { // General Java cautions // --------------------- // every statement ends in a curly brace or a semicolon! // whitespace doesn't matter (but it's good to pretend it does) // Java needs to know what type each variable is, because // variables are boxes to which data is assigned, // NOT labels bound to particular pieces of data /* These are handy block comments for commenting out many lines of code. They start with the sequence /* and end with the pair of characters */ // Examples of the "primitive" variable types int fave; // Java HAS to know the type of every variable! fave = 60; // you can assign a variable after creation //int fave = 42; // ERROR! you can NOT redeclare, even to the same type! // Try uncommenting the above line and see how the compiler complains... double big = 3.14; // in python these are called float instead of double big = JavaIntro.fac(5); // this calls the function below big = fac(10); // so does this boolean cool = true; // notice that booleans are NOT capitalized char coal = 'C'; // a character (char) is NOT a String // Examples of output System.out.println("My variables are"); // prints a line (println) System.out.println(" fave: " + fave); System.out.println(" big: " + big); System.out.println(" cool: " + cool); System.out.println(" coal: " + coal); // Example of input from the command-line Scanner inputter = new Scanner(System.in); // inputter is an object that reads in data System.out.print("Type your favorite integer: "); // prompt the user int userInt = inputter.nextInt(); // get the next int - for all methods, see Scanner in // http://java.sun.com/j2se/1.5.0/docs/api/ System.out.println("Your number " + userInt + " is my favorite, too!"); // Two fundamentally important composite (class) types String s = new String("This is a java String."); // creates a String object String s2 = "This is easier"; // the one exception to always using new double[] powers = new double[10]; // Array of ... (doubles) // example of a definite loop (for) for (int i=0 ; i<10 ; ++i) // CRAZY stuff ?! { powers[i] = Math.pow(2,i); // power is not an operator System.out.println("powers[" + i + "] is " + powers[i]); // no slicing } // example of an indefinite loop (while) int i = 0; while ( s.charAt(i) != 'g' ) // perhaps more natural { System.out.println( s.charAt(i) ); // no indexing OR slicing ! i = i+1; } // What will this print? double difference = sum(powers) - sum(powers); System.out.println("difference is " + difference); } // end of the main method // function: fac // inputs: an integer, x, the input to factorial // output: the factorial of x, represented as a double // public static double fac(int x) { if (x == 0 || x == 1) // conditions MUST be parenthesized { // || is or return 1.0; // && is and } // ! is not else if ( !(x >= 0) ) // same as (x<0), just for illustration { System.out.println("What?!"); x = -1; return x; } else { return x * fac(x-1); // java is as happy with recursion as Scheme... } } // function: sum // input: an double array named A // output: the sum of the input array // public static double sum(double[] A) { for (int i=1 ; i