examples -run these

 

ex1

public class HelloWorld {
            
              // A program to display the message
              // "Hello World!" on standard output
              public static void main(String[] args) {
                 System.out.println("Hello World!");
              }
                 
           }   // end of class HelloWorld

 

ex 2

 

 

         public class Interest {

            /*
               This class implements a simple program that
               will compute the amount of interest that is
               earned on $17,000 invested at an interest
               rate of 0.07 for one year.  The interest and
               the value of the investment after one year are
               printed to standard output.
            */
         
            public static void main(String[] args) {
            
                /* Declare the variables. */
            
                double principal;     // The value of the investment.
                double rate;          // The annual interest rate.
                double interest;      // Interest earned in one year.
                
                /* Do the computations. */
                
                principal = 17000;
                rate = 0.07;
                interest = principal * rate;   // Compute the interest.
                
                principal = principal + interest;
                      // Compute value of investment after one year, with interest.
                      // (Note: The new value replaces the old value of principal.)
                      
                /* Output the results. */
                      
                System.out.print("The interest earned is $");
                System.out.println(interest);
                System.out.print("The value of the investment after one year is $");
                System.out.println(principal);
                               
            } // end of main()
               
         } // end of class Interest

ex 3

public class TimedComputation {
     
        /* This program performs some mathematical computations and displays
           the results.  It then reports the number of seconds that the 
           computer spent on this task.
        */
        
        public static void main(String[] args) {
        
           long startTime; // Starting time of program, in milliseconds.
           long endTime;   // Time when computations are done, in milliseconds.
           double time;    // Time difference, in seconds.
           
           startTime = System.currentTimeMillis();
           
           double width, height, hypotenuse;  // sides of a triangle
           width = 42.0;
           height = 17.0;
           hypotenuse = Math.sqrt( width*width + height*height );
           System.out.print("A triangle with sides 42 and 17 has hypotenuse ");
           System.out.println(hypotenuse);
           
           System.out.println("\nMathematically, sin(x)*sin(x) + "
                                            + "cos(x)*cos(x) - 1 should be 0.");
           System.out.println("Let's check this for x = 1:");
           System.out.print("      sin(1)*sin(1) + cos(1)*cos(1) - 1 is ");
           System.out.println( Math.sin(1)*Math.sin(1) 
                                             + Math.cos(1)*Math.cos(1) - 1 );
           System.out.println("(There can be round-off errors when " 
                                           + " computing with real numbers!)");
           
           System.out.print("\nHere is a random number:  ");
           System.out.println( Math.random() );
           
           endTime = System.currentTimeMillis();
           time = (endTime - startTime) / 1000.0;
           
           System.out.print("\nRun time in seconds was:  ");
           System.out.println(time);
        
        } // end main()
        
     } // end class TimedComputation