/* * An intro to the workings and built-in keywords of Java */ import java.lang.Math; // allows us to use the Math class of the library import java.util.Scanner; // ditto for the input Scanner class class JavaIntro { public static void main(String[] args) { // 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); // plus, not Python's comma System.out.println(" big: " + big); System.out.println(" cool: " + cool); System.out.println(" coal: " + coal); // Lists of consecutive memory addresses of // the same type of data are called "arrays" // // new is used to create the data for arrays // double[] powers; // powers is a reference to an array of doubles // powers is in the "stack" portion of the memory powers = new double[10]; // powers now refers to an array of 10 doubles // the array data is in the "heap" portion of memory System.out.println("powers is " + powers); System.out.println("powers[3] is " + powers[3]); System.out.println("powers.length is " + powers.length); /* * LOOPS! */ // example of a for loop ("definite" iteration) // for (int i=0 ; i<10 ; ++i) // crazy! Q: What should we use instead of 10? { System.out.println("powers[" + i + "] is " + powers[i]); } // another example of a for loop for (int i=0 ; i<10 ; ++i) { powers[i] = Math.pow(2,i); // power is not an operator in Java! } // example of our own function call printArray( powers ); // a different for loop - closer to Python's loops ("foreach" iteration) // double sum = 0.0; for (double austin : powers) { //System.out.println("An element in powers is " + austin); sum += austin; } System.out.println("The sum of powers is " + sum); // example of a while loop ("indefinite" iteration) int number_of_small_powers = 0; int i = 0; while ( powers[i] < 42.0 ) { number_of_small_powers += 1; // found another small power of 2! i += 1; // increment index for the next loop... } System.out.print("There are " + number_of_small_powers); System.out.print(" powers of 2 less than 42.0\n"); // 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/ inputter.nextLine(); // this discards the rest of the user's input line System.out.println("Your number " + userInt + " is my favorite, too!"); // make the above more realistic - check against 42! // Example of string handling 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 int length_of_s = s.length(); System.out.println( "s is " + s ); System.out.println( "s has length " + length_of_s ); System.out.println( "s2 is " + s2 ); System.out.println( "s2 has length " + s2.length() ); System.out.println(); // Strings are similar to arrays of characters - but with many more features! char char_from_s = s.charAt(2); char another_char_from_s = s.charAt(5); if (char_from_s == another_char_from_s) { System.out.println("char_from_s == another_char_from_s: both are " + char_from_s); } // No slicing - AARGH!! String i_blame = s.substring(11,15); // Two more substrings... String start_of_s = s.substring(0,8); String start_of_s2 = s2.substring(0,8); if (start_of_s == start_of_s2) { System.out.println("start_of_s == start_of_s2: both are " + start_of_s); } else { System.out.println("start_of_s != start_of_s2"); System.out.println("start_of_s is " + start_of_s); System.out.println("start_of_s2 is " + start_of_s2); } // What is going on here? // In data-organization languages like Java, it's even more // important to keep in mind how references keep data organized... // How to get from Strings to integers and vice-versa? String answer = "41"; int value_of_answer = Integer.parseInt( answer ); System.out.println("value_of_answer+1 is " + (value_of_answer+1)); String better_answer = "" + (value_of_answer+1); System.out.println("better_answer is " + better_answer); // a small user-interaction loop while (true) { System.out.print("\nAsk me anything! > "); String user_line = inputter.nextLine(); // a Scanner method returning a whole line System.out.println("You typed: " + user_line); System.out.println("Your line had " + user_line.length() + " characters."); System.out.println("If you were shouting, it'd be " + user_line.toUpperCase()); int q_location = user_line.indexOf( 'q' ); System.out.println("The location of 'q' in your string is at position " + q_location ); if (user_line.equals("quit")) { break; // get out of the while (true) loop! } } // Your task: create a more capable conversationalist! - Add signatures! // - palindromes, using two functions // - a small calculator // - anything else you'd like... ! } // end of the main method /* * These two functions are commented with a few lines explaining * - the name and its purpose * - details on the inputs * - details on what it outputs */ // function: fac, for computing factorial // inputs: an integer, x, the input to factorial // output: the factorial of x, represented as a double // public static double fac(int x) // this is the function's "signature" line { 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?!"); return -1; } else { return x * fac(x-1); // java is as happy with recursion as Scheme... } } // function: printArray, convenient for seeing an array's contents // input: an array of doubles // output: none (but it has the side-effect of printing) // public static void printArray(double[] A) { System.out.print("["); // left sq. bracket for (int i=0 ; i