Contents
1) Hello World Program
2) Sum Numbers Program
3) Sum Variables Program
4) Multiply Variables Program
5) Divide Variables Program
6) Count Numbers Program
7) Display Array Values Program
8) Edit Array Values Program
9) Modular Program
1) Hello World Program
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. PRINT "Hello World"
3. END
-----
Java Language sample code:
MyHello
|
import java.io.*;
class MyHello
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println("Hello World");
}
}
|
-----
-----
Java Language sample output:
2) Sum Numbers Program
-----
Flow Chart:
----
Pseudo Code:
1. BEGIN
2. PRINT 1 + 2
3. END
-----
Java Language sample code:
MySum
|
import java.io.*;
class MySum
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(1+2);
}
}
|
-----
-----
Java Language sample output:
3) Sum Variables Program
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INPUT var1, var2
3. PRINT var1+var2
4. END
-----
Java Language sample code:
MySumVar
|
import java.io.*;
class MySumVar
{
public static void main (String[] args) throws java.lang.Exception
{
//assume user input = 1 and 2
int var1 = 1;
int var2 = 2;
System.out.println(var1+var2);
}
}
|
-----
-----
Java Language sample output:
4) Multiply Variables Program
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INPUT var1, var2
3. PRINT var1 x var2
4. END
-----
Java Language sample code:
MyMultiplyVar
|
import java.io.*;
class MyMultiplyVar
{
public static void main (String[] args) throws java.lang.Exception
{
//assume user input = 1 and 2
int var1 = 1;
int var2 = 2;
System.out.println(var1*var2);
}
}
|
-----
-----
Java Language sample output:
5) Divide Variables Program
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INPUT var1, var2
3. IF var2=0
4. THEN PRINT "Error. Division by zero."
5. ELSE PRINT var1/var2
6. END
-----
Java Language sample code:
MyDivideVar
|
import java.io.*;
class MyDivideVar
{
public static void main (String[] args) throws java.lang.Exception
{
//assume user input = 1 and 0
int var1 = 1;
int var2 = 0;
if (var2==0) {
System.out.println("Error. Division by zero.");
}
else{
System.out.println(var1/var2);
}
}
}
|
-----
Live code at ideone.com:http://ideone.com/z6scOs
-----
Java Language sample output:
6) Count Numbers Program
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. PRINT "Start counting."
3. FOR i LOOP := 1 to 3
4. PRINT i
5. END LOOP
6. PRINT "Done."
7. END
-----
Java Language sample code:
Note:array index number starts with 0.
MyCounter
|
import java.io.*;
class MyCounter
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println("Start counting...");
for (int i=0;i<3;i++) {
System.out.println(i);
}
System.out.println("Counting done.");
}
}
|
-----
Live code at ideone.com:http://ideone.com/sEaUVG
-----
Java Language sample output:
7) Display Array Values Program
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INITIALIZE ARRAY arr[5] TO CONTAIN VALUES a,b,c,d,e
3. PRINT "Displaying array values."
4. FOR i := 1 to 5
5. PRINT arr[i]
6. END FOR LOOP
7. PRINT "Done."
8. END
-----
Java Language sample code:
MyArray
|
import java.io.*;
class MyArray
{
public static void main (String[] args) throws java.lang.Exception
{
String arr[]={"a","b","c","d","e"};
for (int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
}
}
|
-----
Live code at ideone.com:http://ideone.com/64jh6M
-----
Java Language sample output:
8) Edit Array Values Program
-----
Note:
This tutorial demonstrates the steps to edit an array value. The program displays the array values before and after the edit is done.
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INITIALIZE ARRAY arr[i] WITH VALUES 1,2,3,4,5
3. FOR i := 1 to 5
4. PRINT arr[i]
5. END FOR LOOP
6. INPUT indexnumber
7. INPUT arrayvalue
8. arr[indexnumber] = arrayvalue
9. FOR i := 1 to 5
10. PRINT arr[i]
11. END FOR LOOP
12. END
-----
Java Language sample code:
MyArrayUpdate
|
import java.io.*;
class MyArrayUpdate
{
public static void main (String[] args) throws java.lang.Exception
{
String arr[]={"a","b","c","d","e"};
System.out.println("Printing array values...");
for (int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
System.out.println("Printing Done.");
System.out.println("Updating...");
//assume user input 1,f
int n=1;
String v[]={"f"};
arr[n]=v[0];
System.out.println("Updating Done.");
System.out.println("Printing array values...");
for (int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
System.out.println("Printing Done.");
}
}
|
-----
-----
Java Language sample output:
9) Modular Program
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN displaydata
2. FOR all item in array PRINT item values
3. END displaydata
1. BEGIN updatedata
2. INPUT indexnumber, arrayvalue
3. arr[indexnumber]=arrayvalue
4. CALL displaydata
4. END updatedata
1. BEGIN main
2. CALL displaydata
3. CALL updatedata
4. END main
-----
Java Language sample code:
MyModule
|
import java.io.*;
class MyModule
{
public static void main (String[] args) throws java.lang.Exception
{
String arr[]={"a","b","c","d","e"};
displayData(arr);
updateData(arr);
}
private static void displayData(String arr1[]){
System.out.println("Printing array values...");
for (int i=0;i<arr1.length;i++) {
System.out.println(arr1[i]);
}
System.out.println("Printing Done.");
}
private static void updateData(String arr2[]){
System.out.println("Updating...");
//assume user input 1,f
int n=1;
String v[]={"f"};
arr2[n]=v[0];
System.out.println("Updating Done.");
displayData(arr2);
}
}
|
-----
-----
Java Language sample output:
-----