02-JAVA Programming Basic
02-JAVA Programming Basic
PROGRAMMING
BASIC
Program in PC
myProg. C++ Machine
Compiler language
cpp
for PC
Source Program in
C++ SUN
File Compiler Machine
for SUN language
Iteration structure
◦ Repetition – similar to C++
for statement
while statement
do…while statement
Nested loop
break and continue
Array
An array is a collection of data
value
An array is an indexed value of the
same type
Array of primitive data types
◦ Declaration
datatype variablename []; or datatype []
variablename;
◦ Array creation
eg. variablename = new dataype[size of array];
example
Then you can use the print() and println() method provided by the
PrintStream class
Input Statement
Using I/O console
◦ Using Scanner class
◦ Create a Scanner object by passing an
input stream to the constructor
Scanner s = new Scanner(System.in);
◦ You can use these method to read the data
from the keyboard :
Integer data value = nextInt()
Decimal data value = nextDouble();
String data value = next();
◦ Example :
Scanner s = new Scanner(System.in);
int num = s.nextInt();
String name = s.next();
Using dialog box
◦ You need to use javax.swing.JOptionPane package
◦ The method used is showInputDialog, and it has
String parameter which is a message displayed on the
monitor.
◦ The method returns String value if user enters a value
and null value if user cancels the input
◦ The String value can be converted to numeric by the
used of these method
Integer = Integer.parseInt()
Double = Double.parseDouble()
Foat = Float.parseFloat()
◦ Example
import javax.swing.*;
--
String input = JOptionPane.showInputDialog(null, “Enter
an integer number “);
int num = Integer.parseInt();
Output Statement
Using I/O Console
◦ By using System.out object through
the method print() or println()
◦ Example
int total = 4 +5;
System.out.print(“The total is “);
System.out.println(total);
System.out.println(“The total is “ + total);
Using dialog box
◦ You need to use javax.swing.JOptionPane package
◦ The method used is showMessageDialog, and it has
String parameter which is a message displayed on
the monitor screen.
◦ Example
import javax.swing.*;
--
JOptionPane.showMessageDialog(null,”Hello World!”);
String s1 = “JAVA is fun”;
JOptionPane.showMessageDialog(null, s1);