Java Code Samples

__________________
__________________
class hello
{
	public static void main ( String[] args )
    {
      System.out.println("Hello World!");
    }
}
__________________
Output: 
Hello World!
__________________
__________________
/* Comments, Haiku Poetry
Write out three lines of a poem.
The poem describes a single moment in time,
using 17 syllables.
*/

// Write three lines of a poem to the computer monitor
class Haiku
{
	public static void main ( String[] args )
	{
        System.out.println("On a withered branch");
        System.out.println("A crow has just alighted:");
        System.out.println("Nightfall in autumn.");
	}
}
__________________
Output: 
On a withered branch
A crow has just alighted:
Nightfall in autumn.
__________________
__________________
class HelloPlanets
{
	public static void main ( String[] args )
	{
         String[] planets = {"Mercury", "Venus", "Earth", 
                             "Mars", "Jupiter",  "Saturn",  
                             "Uranus", "Neptune", "Pluto"};
         for ( int j=0; j< planets.length; j++ )
         {
           system.out.println("Hello " + planets[j] +"!" );
         }
	}
}
__________________
output: 
hello mercury!
hello venus!
hello earth!
hello mars!
hello jupiter!
hello saturn!
hello uranus!
hello neptune!
hello pluto!
__________________
__________________
class idexamples
{
	public static void main ( string[] args )
	{
        long   hoursworked = 40;    
        double payrate     = 10.0, taxrate = 0.10;
		    
        system.out.println("Hours Worked: " + hoursworked);
        system.out.println("Pay/hour   $: " + payrate); 
        system.out.println("pay Amount $: " + (hoursworked * payrate) );
        system.out.println("tax Rate    : " + taxrate);
        system.out.println("tax Amount $: " + (hoursworked * payrate * taxrate) );
    	system.out.println("Take home  $: " + ((hoursworked * payrate) - 
                                               (hoursworked * payrate * taxrate)) );
	}
}
__________________
output: 
hours worked: 40
pay/hour   $: 10.0
pay amount $: 400.0
tax rate    : 0.1
tax amount $: 40.0
take home  $: 360.0
__________________
__________________
import java.io.*;
class echo
{
	public static void main (string[] args) throws ioexception
			/* throws IOException - is necessary for programs that do input...
			It informs the compiler that main() does an input operation that might fail. 
			When the program is running and an input operation fails, the computer system 
			will be informed of the failure and the program will be gracefully halted. 
			When an input or an output operation fails, an exception is generated.
			An exception is an object (a chunk of main memory) that contains information about what went wrong. 
			The section of code where the problem occurred can deal with the problem itself, or pass the exception on. 
			Passing the exception on is called throwing an exception. */ 
	{
            InputStreamReader inStream = new InputStreamReader(System.in);
            BufferedReader stdin = new BufferedReader( inStream );
 
            String inData;

            System.out.println("Enter the data:");
            inData = stdin.readLine();

            System.out.println("You entered: " + inData );
	}
}
__________________
1st Output: 
Enter the data:

(Enter a response:)
(enter something...)

2nd Output/Response:
You entered: enter something...
__________________
__________________
import java.io.*;
class EchoSquare
{
	public static void main (String[] args) throws IOException
	{ 
           BufferedReader stdin = 
               new BufferedReader ( new InputStreamReader(System.in));
	   //works the same as :
	   //InputStreamReader inStream = new InputStreamReader(System.in);
	   //BufferedReader stdin = new BufferedReader( inStream );
      
           String inData;
           int    num, square;                  // declaration of two int variables

           System.out.println("Enter an integer:");
           inData = stdin.readLine();
          
           num    = Integer.parseInt( inData ); // convert inData to int
           square = num * num ;                 // compute the square

           System.out.println("The square of " + inData + 
							" is: " + square );
	}
}
__________________
1st Output: 
Enter an integer:

(Enter a response:)
(4)

2nd Output/Response:
The square of (4) is: 16

An error exception might look like this:
Enter an integer:
14.92
java.lang.NumberFormatException: 14.92 
__________________
__________________
import java.io.*;
class RestaurantTip
{
	public static void main (String[] args) throws IOException
	{
	   String charData, tipData;
	   double basicCost, tipRate;
	   BufferedReader stdin = 
		   new BufferedReader ( new InputStreamReader(System.in));

	   System.out.println("Enter Meal cost:");
	   charData   = stdin.readLine();
	   basicCost  = ( Double.valueOf( charData  ) ).doubleValue();

	   System.out.println("Enter tip rate:") ;
	   tipData = stdin.readLine();
	   tipRate = Double.parseDouble( tipData )  ;

	   System.out.println("Meal $ " + basicCost + ", tax $ " + (basicCost*0.06) +
			      ", tip $ " + (basicCost*tipRate) + " = Total $ " + 
			      (basicCost + basicCost*0.06 + basicCost*tipRate));
	}
}
__________________
1st Output: 
Enter Meal cost:
(Enter a response:)
(39.45)

2nd Output:
Enter tip rate:
(Enter a response:)
(0.075)

Final Output/Response:
Meal $ 39.45, tax $ 2.367, tip $ 2.95875 = Total $ 44.77575
__________________
__________________

See Java Links for more info   and   Java Notes for my own notes, Java Keywords