Logout

Sample Class 2 (more to see what's possible...)

ReviewJB.java
/Users/adelaide/Public/Netbeans - All JSR Projects/Review For JB/src/reviewforjbpackage/ReviewJB.java
  1 
  2 package reviewforjbpackage;
  3 
  4 import java.io.BufferedReader;
  5 import java.io.IOException;
  6 import java.io.InputStreamReader;
  7 
  8 //This is the second sample class we did to get an idea of what Java programs can do.
  9 //And like the first sample class YOU DO NOT HAVE TO KNOW HOW TO DO ALL OF THIS; rather it is intended to
 10 //give you an idea of the kinds of things we will be able to do with Java.
 11 
 12 //And so again, never-the-less, I'll comment the things we did here that were a bit different from the first class.
 13 
 14 
 15 public class ReviewJB {
 16     public static void main(String[] args) {
 17 
 18         //Using the args array:
 19         System.out.println(args[0]); //Didn't work
 20         
 21         //Flow of Control:
 22 
 23         System.out.println("Flow of Control");
 24         System.out.println("Flow of control refers to the order in which the lines of code are exectuted.");
 25         System.out.println("There are three main ways to affect this: conditionals, loops, and methods.");
 26         
 27         
 28         //Conditonal Blocks
 29         System.out.println("Conditonal Blocks");
 30         if((1+3*9) != (2*18)){   //They are not equal, so the next line should print out.  != means not equal.  
 31             System.out.println("(1+3*9) does not equal (2*18)");
 32         }
 33         else{
 34             System.out.println("(1+3*9) equals (2*18)");
 35         }
 36 
 37 
 38         int x = 33;
 39         int y = 99;
 40         if(x == y){//And they don't so this block should be skipped, and the else block execute.
 41             System.out.println("x (33) is equal to y (99).");
 42         }
 43         else{
 44             System.out.println("x (33) is not equal to y (99).");
 45         }
 46 
 47 
 48         String a = "hello";
 49         String b = "Hello";
 50         if(a.equalsIgnoreCase(b)){//Yes, this is true, so the following line will execute.
 51             System.out.println("If we ignore capital and small letters, \"hello\" and \"Hello\" are equal.");
 52             //You may recall that the \ character allows us to put double quotes without ending our println.
 53         }
 54         else{
 55             System.out.println("If we ignore capital and small letters, \"hello\" and \"Hello\" are not equal. ");
 56         }
 57 
 58 
 59         if(a.equals(b)){//This actually is more sophisticated than it seems... more later on this.
 60             System.out.println("Normally to a computer \"hello\" and \"Hello\" are not equal.");
 61         }
 62         else{
 63             System.out.println("Normally to a computer \"hello\" and \"Hello\" are  not equal.");
 64         }
 65         System.out.println("");
 66         
 67         
 68         //Looping
 69         System.out.println("Looping");
 70         System.out.println("");
 71         int i = 0;
 72         while(i < 10){
 73             System.out.println("We have not reached 10 yet!!");
 74             i+=2;//So we should expect this to loop 5 times: when i is 0, 2, 4, 6, and 8.
 75         }
 76         System.out.println("Made it to 10!!!");
 77 
 78 
 79         //Printing stuff out
 80         System.out.println("printed stuff out");
 81         System.out.println("");//println prints and returns the cursor to the next line.
 82         System.out.print("a");//print prints and does not return the cursor to the next line.
 83         System.out.print("b");
 84         System.out.print("c");
 85         System.out.println("e");
 86         System.out.println("f");
 87         System.out.println("g");
 88 
 89 
 90         //Reading stuff in from the console
 91         System.out.println("read stuff in from the console");
 92         System.out.println("");
 93         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//For reading in from the console.
 94         System.out.println("Please enter a word");
 95         String theStringReadIn = "not set yet.";
 96         try {
 97             theStringReadIn = br.readLine();//Reading in the line has to be in a try block, in case there's an error.
 98         } catch (IOException ex) {
 99             System.out.println("This was an error on reading in after \"Please enter a word.\"");
100         }
101         System.out.println("Just to confirm, the word you entered was: " + theStringReadIn);
102 
103         //Outputting more than one thing in one line
104         System.out.println("output more than one thing in one line");
105         System.out.println("");
106         System.out.println("One more demo of this: " + (3*9) + " is the product of 3 and 9.");
107         //To output more than one thing on a line we use the + operator.
108         
109         
110         //Converting a String (a String is a word) to a number:
111         System.out.println("converted a String (word) to a number");
112 
113         try{
114             System.out.println("Please enter in one number:");
115             int firstInt = Integer.parseInt(br.readLine());//The method that converts a String to an int.
116             System.out.println("Now enter in another number");
117             int secondInt = Integer.parseInt(br.readLine());
118             System.out.println(firstInt + " x " + secondInt + " = " + firstInt * secondInt);
119 
120         }
121         catch(Exception e){//The catch block in case the try block above encountered an error.
122             System.out.println("This was an error inside the last part of the program");
123         }
124 
125     }
126 }