import java.util.Scanner; class Sample { public static void main(String[] args) { //********************************************************************** // * // This is where you put the coding that will be executed when * // you enter "java Sample" * // * //********************************************************************** System.out.println("Hello, world!"); } void StealMe() { //********************************************************************** // * // This routine just holds samples of various sorts * // * // 1. Allocate and load integer, real, or String (i.e., character) * // SINGLE variables. * // * // The "int" "double" and "String" only appear the first time you * // set the variable. Later, for example, you would just type * // "number=4" * // * // The boolean variable can hold only two different values "true" or * // false". * // * //********************************************************************** int number=3; double Avagadro=6.0221E23; String name="Frank"; boolean isItRaining=true; //********************************************************************** // * // 2. Allocate and load arrays. * // * // Note that Java's first array entry is element ZERO. * // * //********************************************************************** double[][] a=new double[3][2]; a[0][0]=1.; a[1][0]=2.; a[2][0]=3.; a[0][1]=4.; a[1][1]=5.; a[2][1]=6.; //********************************************************************** // * // 3. Print to the screen * // * //********************************************************************** System.out.println(" Hello, world! \n a[1][1] is "+a[1][1]); //********************************************************************** // * // 4. Perform a "DO loop" * // * // Formally, the three arguments inside the "for" statement * // parentheses (separated by semi-colons) are: * // 1. A statement that will be performed BEFORE the loop starts. * // 2. An logical statement (IF statement) that governs whether the * // loop will be continued. If it is false, the loop ends * // (perhaps without ever having been run, if it is always false) * // 3. A statement that will be performed as the LAST statement of * // the loop. * // * // In practice, they are almost always used like in the example: * // 1. Initializing a counter of how many times the loop has been * // executing * // 2. Stating that the loop will continue only if the counter is * // below some upper limit (the upper limit is the number of * // times it will run, if the counter starts at zero). * // 3. Increment the counter. * // * //********************************************************************** // * // Notes:1. In Java, capitalization matters. So the variable n is * // different from N. * // 2. "continue" means to jump to skip the rest of the lines * // of the loop BUT go on to the next loop entry. * // 3. "break" means to jump completely out of the loop. * // 4. Inside an "IF" test, you can use < and > normally (along * // with either =< or <= or => or >=) BUT the equals sign * // has to appear TWICE. * // * //********************************************************************** int N=10; for(int i=0;i Avagadro) { System.out.println(" Too high. Guess again."); } else if(guess < Avagadro) { System.out.println(" Too low. Guess again."); } else { System.out.println(" Correct!"); stillGuessing=false; } //********************************************************************** // * // 8. Pull in a pre-existing Java object * // * // Look at what I did with the Scanner object: * // a. Look up the object in Java.doc * // b. Put in the import statement at the top of the file * // --> import java.util.Scanner; * // c. Create the object using a constructor. * // --> Scanner sc=new Scanner(System.in); * // d. Use the object's methods of interest * // --> double guess=sc.nextDouble(); * // * //********************************************************************** // * // 9. Put in comments: * // a. As I have done throughout this file, start line with // * // b. Insert // in middle of line. Rest of line is comment. * // c. Insert a /* (anywhere). Everything is a comment until * // you put in a */. * // * // I find this most useful for temporarily disabling lines * // of code. * // * //********************************************************************** } } }