Java Basics Part 1 Lab Note
Java Basics Part 1 Lab Note
Java Basics
Step 1: Write the source code Xxx.java using a programming text editor (such as Sublime
Text, Atom, Notepad++, Textpad, gEdit) or an IDE (such as Eclipse or NetBeans).
Step 2: Compile the source code Xxx.java into Java portable bytecode Xxx.class using the
JDK Compiler by issuing command:
javac Xxx.java
Step 3: Run the compiled bytecode Xxx.class with the input to produce the desired output,
using the Java Runtime by issuing command:
java Xxx
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as
"Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
}
A Sample Program Illustrating Sequential, Decision and Loop
Constructs
Below is a simple Java program that demonstrates the three basic programming constructs:
sequential, loop, and conditional.
/**
* Find the sums of the running odd numbers and even numbers from a given
lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to
UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ...,
UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to
" + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + "
to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is:
" + absDiff);
}
}
Example code :
// if-then
int absValue = -5;
if (absValue < 0) absValue = -absValue; // Only one statement in the
block, can omit { }
int min = 0, value = -5;
if (value < min) { // More than one statements in the block, need { }
min = value;
System.out.println("Found new min");
}
// if-then-else
int mark = 50;
if (mark >= 50)
System.out.println("PASS"); // Only one statement in the block, can
omit { }
else { // More than one statements in the block,
need { }
System.out.println("FAIL");
System.out.println("Try Harder!");
}
// Harder to read without the braces
int number1 = 8, number2 = 9, absDiff;
if (number1 > number2) absDiff = number1 - number2;
else absDiff = number2 - number1;
Loop Flow Control
Input/Output
System.out.print() and println() do not provide output formatting, such as controlling the
number of spaces to print an int and the number of decimal places for a double.
Java SE 5 introduced a new method called printf() for formatted output (which is modeled
after C Language's printf()). printf() takes the following form:
printf(formattingString, arg1, arg2, arg3, ... );
You can also use method nextLine() to read in the entire line, including white spaces, but
excluding the terminating newline.
/**
* Test Scanner's nextLine()
*/
import java.util.Scanner; // Needed to use the Scanner
public class ScannerNextLineTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a string (with space): ");
// Use nextLine() to read entire line including white spaces,
// but excluding the terminating newline.
String str = in.nextLine();
in.close();
System.out.printf("%s%n", str);
}
}
Try not to mix nextLine() and nextInt()|nextDouble()|next() in a program (as you may need to
flush the newline from the input buffer).