// Welcome1.java
// A first program in Java
public class Welcome1 {
public static void main( String args[] )
{
System.out.println( "Welcome to Java Programming!" );
}
}
// Welcome2.java
// Printing a line with multiple statements
public class Welcome2 {
public static void main( String args[] )
{
System.out.print( "Welcome to " );
System.out.println( "Java Programming!"
);
}
}
// Welcome3.java
// Printing multiple lines with a single statement
public class Welcome3 {
public static void main( String args[] )
{
System.out.println( "Welcome\nto\nJava\nProgramming!" );
}
}
// Welcome4.java
// Printing multiple lines in a dialog box
import javax.swing.JOptionPane; // import class JOptionPane
public class Welcome4 {
public static void main( String args[] )
{
JOptionPane.showMessageDialog(
null, "Welcome\nto\nJava\nProgramming!" );
System.exit( 0 ); // terminate the program
}
}
// Addition.java
// An addition program
import javax.swing.JOptionPane; // import class JOptionPane
public class Addition {
public static void main( String args[] )
{
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2
// read in first number from user as a string
firstNumber =
JOptionPane.showInputDialog( "Enter first integer" );
// read in second number from user as a string
secondNumber =
JOptionPane.showInputDialog( "Enter second integer" );
// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
// add the numbers
sum = number1 + number2;
// display the results
JOptionPane.showMessageDialog(
null, "The sum is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE );
System.exit( 0 ); // terminate the program
}
}
// Comparison.java
// Using if statements, relational operators,
// and equality operators
import javax.swing.JOptionPane;
public class Comparison {
public static void main( String args[] )
{
String firstNumber, // first string entered by user
secondNumber, // second string entered by user
result; // a string containing the output
int number1, // first number to compare
number2; // second number to compare
// read first number from user as a string
firstNumber =
JOptionPane.showInputDialog( "Enter first integer:" );
// read second number from user as a string
secondNumber =
JOptionPane.showInputDialog( "Enter second integer:" );
// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
// initialize result to the empty string
result = "";
if ( number1 == number2 )
result = number1 + " == " + number2;
if ( number1 != number2 )
result = number1 + " != " + number2;
if ( number1 < number2 )
result = result + "\n" + number1 + " < " + number2;
if ( number1 > number2 )
result = result + "\n" + number1 + " > " + number2;
if ( number1 <= number2 )
result = result + "\n" + number1 + " <= " + number2;
if ( number1 >= number2 )
result = result + "\n" + number1 + " >= " + number2;
// Display results
JOptionPane.showMessageDialog(
null, result, "Comparison Results",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}