Version 1: A simple program, comments, semicolon, output

/**

* Weight Calculator > Essentials1.java

*

* Displays your weight on the moon using console output.

* Illustrates a basic program, multi-line comments, output.

*

* @author Dale Reed

*/

public class Essentials1 {

public static void main(String[] args) {

// Prints "On the moon you weight 30 pounds." to the console.

System.out.println("On the moon you weight 30 pounds.");

}

}

/* Output:

On the moon you weight 30 pounds.

*/

Version 2: Variables, assignment, arithmetic, string concatenation

/**

* Weight Calculator > Essentials2.java

*

* Displays your calculated weight on the moon using console output.

* Illustrates variable declarations, identifiers, initialization, assignment,

* arithmetic expression, string concatenation, escape sequences

*

* @author Dale Reed

*/

public class Essentials2 {

public static void main(String[] args) {

// variable declarations

int earthWeight; // weight on the earth

// Set the weight on earth. In the future we will get this from the user.

earthWeight = 220; // time to go on a diet...

// compute the result

double moonweight; // weight on the moon

moonweight = earthWeight * 0.15;

//display the result

System.out.print( "If on earth you weigh " + earthWeight + " pounds,");

System.out.println( " on the moon you'll weigh " + moonweight + " \n\n");

}

}

/* Output:

If on earth you weigh 220 pounds, on the moon you'll weigh 33.0

*/

Version 3: Precedence, parenthesis, type cast, named constants

/**

* Weight Calculator > Essentials3.java

*

* Displays a table of calculated weights on Jupiter using console output.

* Illustrates precedence and parenthesis, type cast, named constant, while loop

*

* @author Dale Reed

*/

public class Essentials3 {

public static void main(String[] args) {

// variable declarations

int earthWeight = 100; // weight on the earth, declared and initialized

int jupiterMassFactor = 318; // Jupiter has 318 times the mass of Earth

int jupiterRelativeRadius = 11; // Jupiter has radius 11 times greater than Earth's

// Calculate multiplier to compare earth and Jupiter weights

double weightMultiplier = (double)jupiterMassFactor /

(jupiterRelativeRadius * jupiterRelativeRadius);

System.out.print("Multiply Earth Weight by " + weightMultiplier);

System.out.print(" to find Jupiter weight. \n\n");

// display a table of weights on earth and Jupiter

final int WEIGHT_INCREMENT = 20; // increment between earth weights in table rows

System.out.println("Earth Jupiter "); // table heading

int loopCounter = 0; // loop iterations counter

while (loopCounter < 7) {

System.out.print( " " + earthWeight);

System.out.println( " " + (earthWeight * weightMultiplier) );

loopCounter = loopCounter + 1; // same as loopCounter++

earthWeight = earthWeight + WEIGHT_INCREMENT;

// The above line is the same as: earthWeight += WEIGHT_INCREMENT;

}

System.out.println("\nDone.");

}

}

/* Output:

Multiply Earth Weight by 2.628099173553719 to find Jupiter weight.

Earth Jupiter

100 262.8099173553719

120 315.3719008264463

140 367.9338842975207

160 420.4958677685951

180 473.05785123966945

200 525.6198347107438

220 578.1818181818182

Done.

*/

Scanner Class: Simpler form of I/O

/**

* I/O > Console I/O > ConsoleIOScanner.java

*

* Reads text from standard input using console I/O using the Scanner class.

* This does not work in versions of Java previous to Java 5 (aka 1.5)

* @author Dale Reed

*/

import java.util.Scanner;

public class ConsoleScannerInput {

public static void main(String[] args) {

Scanner keyboard = new Scanner( System.in);

System.out.print( "Enter your first name: " );

String first = keyboard.next(); // read a string

System.out.print( "Enter your last name and your age: " );

String last = keyboard.next(); // read a string

int age = keyboard.nextInt(); // read an integer

System.out.println( "Hello " + first + " " + last);

System.out.printf( "In one year you will be %d years old", age + 1);

}

}

/* Output: Assuming your input is: "Dale", "Reed 41"

Enter your first name: Dale

Enter your last name and your age: Reed 41

Hello Dale Reed

In one year you will be 42 years old

*/

Console IO without the use of the Scanner Class

/**

* I/O > Console I/O > ConsoleInput.java

*

* Reads text from standard input using console I/O. To use the System.in

* object, you must wrap it in a InputStreamReader object and then wrap it in

* a BufferedReader object. In addition, you must somehow handle the

* IOException that the .readLine() method throws. You can do this either by

* putting "throws IOException" after the method header or else enclose it

* in a try block. For this example, we add "throws IOException" to the end

* of the main() method header, which is generally the easiest way to go.

* @author Dale Reed, Feihong Hsu

*/

import java.io.*;

public class ConsoleInput {

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(

new InputStreamReader(System.in) );

System.out.print( "Enter your first name: " );

String first = reader.readLine();

// It is more difficult to combine the two inputs below on the same input line

System.out.print( "Enter your last name: " );

String last = reader.readLine();

System.out.print( "Enter your age: " );

int age = Integer.parseInt( reader.readLine() );

System.out.println( "Hello " + first + " " + last);

System.out.printf( "In one year you will be %d years old", age + 1);

}

}

/* Output: Assuming your input is "Dale", "Reed", "41"

Enter your first name: Dale

Enter your last name: Reed

Enter your age: 41

Hello Dale Reed

In one year you will be 42 years old

*/