Lab 6: Input & Output - UniMAP Portal

Use a Notepad editor to code a simple java program to display a text message in
a dialog box using methods from JOptionPane class ... Java programs. There are
three different editions of JDK available from Sun Mircrosystems: Java Standard
Edition (JSE), Java Enterprise Edition (JEE) and Java Micro Edition (JME).

Part of the document


LAB 1: YOUR FIRST JAVA PROGRAM




OBJECTIVES


1. Use a Notepad editor to code a simple java program to display a text
message on a DOS platform using System.out.println method.


2. Capable of creating a correct batch file containing statements to
instruct compiler to find the correct path of javac and java commands.



3. Compile the source code using javac command and run the bytecode
(which is generated during compilation process) by using java command


4. Use a Notepad editor to code a simple java program to display a text
message in a dialog box using methods from JOptionPane class


5. Use a Textpad editor as a simple Interactive Development Environment
(IDE) to compile a source code and run the bytecode generated after
the compilation process


6. Use the escape sequence to format text.


7. Identify Java identifiers.


8. Select proper types for numerical data.


9. Write arithmetic expressions in Java


10. Evaluate arithmetic expressions following the precedence rules.

11. Convert input string values to numerical data.


12. Use Scanner class to receive input data from keyboard


13. Use JOptionPane class to accept input data from an input dialog box.


14. Write Boolean expressions using relational and Boolean operators.


15. Evaluate given Boolean expression correctly.



16. Write Java program to solve simple problems

17. Implement selection control in a program.



18. Implement repetition control in a program.


NOTES


1. Java is an interpreted language. The source code (that has extension
.java) is translated into bytecode that has extension .class before it
can be implemented.




2. The Java Virtual Machine (JVM) is a program that reads Java byte code
instructions and executes them as they are read.


3. Java Development Kit (JDK) or Software Development Kit (SDK) is a
software used to create Java programs. There are three different
editions of JDK available from Sun Mircrosystems: Java Standard
Edition (JSE), Java Enterprise Edition (JEE) and Java Micro Edition
(JME). The edition of Java may be downloaded at:


http://java.sun.com


4. Java Standard Edition (JSE) provides all the essential software tools
necessary for writing Java applications and applets.




5. Java Enterprise Edition (JEE) provides tools for creating large
business applications that employ servers and provide services over
the web.




6. Java Micro Edition (JME) provides a small, highly optimized runtime
environment for consumer products such as cell phones, pagers, and
appliances.




7. The names of Java source code files end with .java. For example:


Program1.java


8. Three (3) steps to create Java application are as follows:


i. Create a source file (code a Java program) using a text editor,
either Notepad or Textpad editor),


ii. Compile the source file into a bytecode file using javac command,
with the following format and example:
javac Filename.java


For example:


javac Program1.java


iii. Run the bytecode file using java command.


java Filename


For example:


java Program1


Step (ii) and (iii) are done in DOS environment.



9. A .java file may contain many classes, but may only have one public
class. If a .java file has a public class, the class must have the
same name as the file. As an example, if the file named Program1.java
contains a public class, the class's name would be Program1. There
can be more than one class in a file, however only one can be declared
public .


10. Compiler will look at the presence of main() method to implement the
program. The main()method resides in a class of scope public. The
name of the class that consists method main() will become the name of
the folder that consists of the source code. For example, the name of
the program file below is TestProg.java because it has a class that
consists of method main().


class Program1 {


}
public class TestProg {
public static void main (String[] args){
System.out.println("Testing Program.");
}
}


11. Statements in Java are terminated with semicolons. This does not
include comments, class headers, method headers, and braces.


12. Identifier is a programmer-defined name that represents some element
of a program. It is a sequence of letters, digits, underscore (_),
and dollar sign ($) with the first one being nondigit. Example of
valid identifier:


Jun2009, data3, _calc, $RM50


13. Java is a case-sensitive language. It does not regard uppercase
letters as being the same character as their lowercase equivalent.


14. Key words are reserved and cannot be used for anything other than
their designeated purpose. There are numerous key words in Java such
as main, void, static, class, public, int and double. These reserve
words cannot be used as identifiers name.


15. A variable is a named storage location that stores data and the value
of the data may change while the program is running. A variable is
classified according to its data type, which determines the kind of
data that may be stored.


16. Java has eight primitive data types namely byte, short, int, long,
float, double and char.


17. Constant in Java is declared using the keyword final. Constants are
initialized with a value, and that value cannot change during the
execution of the program. An example of constant declaration:


final double MAX_VALUE = 100;


18. There are many operators for manipulating numeric values and
performing arithmetic operations. Some operators have higher
precedence than other operators. The operators are: ++ (increment), --
(decrement), + (addition), -(subtraction), *(multiplication), /
(division), and % (remainder).


19. The Boolean operators &&, &, ||, |, !, and ^ operate with Boolean
values and variables. The relational operators =
work with numbers and characters that yield a Boolean value.


20. The if and switch statement is used to create a decision structure,
which allows a program to have more than one path of execution. The
if statement evaluate a Boolean expression and will execute only when
the expression is true. Meanwhile, the switch expression must
evaluate to a char, byte, short, int, or enum. A case constant must
be a constant (i.e. final variable) and must match to the switch
expression type. The general format dan example:


The general format of if statement:


if (BooleanExpression)
statement;


Example of if statement:


if (temperature > 30 && distance > 5)
System.out.println("It's far. Need to take a cab");


The general format of switch statement:


switch (Expression){
case constant1: block of statements;
case constant2: block of statements;
default: block of statements;
}


Example of switch statement:


int y = 1;
switch(y){
case 1 :
System.out.println("rambutan");
break;
case 2 :
System.out.println("durian");
break;
case 3 :
System.out.println("manggis");
break;
default :
case 1 :
System.out.println("pineapple");
}




21. The if-else statement will execute one group of statements if its
Boolean expression is true, or another group if its Boolean expression
is false. The general format dan example:


if (BooleanExpression)
{ statements; }
else
{ statements; }


For example:


if (b > 0)
System.out.println("Positive");
else
{
System.out.println("Negative");
System.out.println("Please enter another number");
}




22. The if-else-if statement is a chain of if-else statements, while the
nested if statement is an if statement in a conditionally executed
code of another if statement. The switch statement can simulate the
if-else-if statement, as well as the nested if. The general format
and example:


The general format of if-else-if:


if (BooleanExpression)
{ statements; }
else if (BooleanExpression)
{ statements; }
else { statements; }




Example if-else-if:


if (b > 0)
System.out.println("Positive");
else if (b < 0)
System.out.println("Negative");
else
System.out.println("Zero");


The general format of nested if:


if (BooleanExpression)
{ statements;
if (BooleanExpression)
{ statements; }
else
{ statements; }
}
else if (BooleanExpression)
{ statements; }
else { statements; }


Example nested if:


if (age > 40)
{
System.out.println("Senior");
if (experienced > 5)
System.out.println("Have many experienced");
else
System.out.println("Little experienced");
}
else if (age < 25)