The Basics
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.
*/
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
*/
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.
*/