Java Programming Module
Java Programming Module
E-LEARNING
LEARN
JAVA PROGRAMMING
Chapter 1: Introduction to Basic Computer Programming (JAVA)
Recognizing this, Sun Microsystem sin 1991 funded an internal corporate research project code-
named Green, which resulted in a C++-based language that its creator, James Gosling, called Oak after an
oak tree outside his window at Sun. it was later discovered that there already was a computer language
by that name. When a group of Sun people visited a local coffee shop, the name JAVA was suggested, and
it stuck. The Green project ran into some difficulties. The marketplace for intelligent consumer electronic
devices was not developing in the early 1990s as quickly as Sun had anticipated.
The project was in danger of being cancelled. By sheer good fortune, the World Wide Web
exploded in popularity in 1993, and Sun people saw the immediate potential using of Java to add dynamic
content, such as interactivity and animations, to web pages.
This breathed new life into the project. Sun formally announced Java at an industry conference in
May 1995. Java garnered the attention of the business community because of the phenomenal interest in
the World Wide Web. Java is now used to develop large-scale enterprise applications, to enhance the
functionality of web services (the computers that provide the content we see in our web browsers), to
provide applications for consumer devices (e.g., cell phones, pagers and personal digital assistants) and
for many other purposes.
Package
-A package is a namespace that organizes a set of related classes and interfaces.
Class
-Every program in java declaration declared by the user. This will determine the blue
print of the application.
{
-determines the beginning a class, main or procedures
}
-determines the end of a class, main or procedures
Void
-indicates that the program will perform a task but without any return of information
Main
-an identifier in which indicates a building block
(String[] args)
-a program building block or method
String.out.
-a standard output object, allows java application to display characters or information.
println
-a command that tells java application to print whatever object in a line
(“Hello World!”)
-the encapsulated word of the parenthesis will be printed out by the java program.
Semicolons ;
-indicates statement in a program
//
-is a single line comment used for explain a line of code
/**/
-a multiline comment used for documenting the whole application of program itself
Chapter 2: Basic Input and Output
Therefore Variables and data types scopes are the location of the declared variable to its data
type from a certain location.
package javasamp;
class javafirstprog{
int a; All methods and functions can access
Global variables
char c; those variables
Note:
-take note the positions of the variables
-declared variables from a data type are also called as “primitive type or built in type”
Operators operation
* Multiplication
/ Division
- Subtraction
+ Addition
^ Exponents
% Modulo(Gets the remainder)
Escape codes
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
\b Backspace
\f Form feed(page feed)
\a Alert (beep)
\’ Single quote(‘)
\” Double quote(“)
\? Question mark(?)
\\ Backslash(\)
Format specifiers
Example:
((5==5) && 3>6)) // evaluates to false (true && false).
((5==5) || (3>6) // evaluate to true (true|| false).
Logical operators
If conditional statements
It is where condition is the expression that is being evaluated. If this condition is true, statement
is executed. If it is false, statement is ignored (not executed) and the program continues right after this
conditional structure.
If (x==100)
System.out.println(x is 100)
If else conditional statements
do while loop
Its functionality is exactly the same as the while loop, exact that condition in the do while loop
is evaluated after the execution of statement instead of before, granting at least one execution of
statement even if condition is never fulfilled.
Try out this sample code:
/*Sample 8 Enter number (0 to end):
*number of echoes defined by user 12345
*/ You entered: 12345
import java.util.Scanner; Enter number(0 to end):
160277
public class Sample{ You entered: 160277
public static void main(String[]args){ Enter number (0 to end): 0
int x; You entered: 0
Scanner input= new Scanner(System.in);
do{
System.out.println(“You entered: 0 to end):”);
x=input.nextInt();
System.out.printf(“You entered: %d”,x);
}while(x!=0);
}
}
for loop Initialization
Condition
n=0, i=100 n!=1 n++, i--
for ( ; ; )
Increase
Its main function is to repeat statement while condition remains true, like the while loop. But in
addition, for loop provides specific locations to contain an initialization statement counter which is
initialized and increased on each iteration.
Try out this sample code:
/* Sample 9 10,9,8,7,6,5,4,3,2,1, FIRE!
*Custom countdown using while for loop
*/
import.java.util.Scanner;
public class Sample{
public static void main(String[] args){
for(n=10,n>0, n--)
System.out.printf(“%d ”,n);
}
System.out.printf(“FIRE!”);
}
Chapter 5: Array
Declaring and Creating Arrays
An array is a series of elements of the same type placed in contiguous memory allocations that
can be individually referenced by adding an index to a unique identifier.
Int billy[] = new int[5] {16, 2, 77, 40, 12071};
0 1 2 3 4
billy 16 2 77 40 12071
}
}
/*Sample14 Grade distribution:
*barchart array 00-09:
*/ 10-19:
public class SumArray{ 20-29:
public static void main(String[] args){ 30-39:
int array[] = {0,0,0,0,0,0,1,2,4,2,1}; 40-49:
System.out.println(“Grade distribution:”); 50-59:
for (int counter = 0; counter<array.length;counter++) 60-69:*
{ 70-79:**
if(counter==10) 80-89:****
System.out.printf(“%5d:”, 100); 90-99:**
else 100:*
System.out.printf(“%02d-%02d:”,counter*10,counter*10+9);
for (int stars=0;stars<array[counter];stars++)
System.out.print(“*”);
System.out.println();
}
}
}
Multidimensional Array
Multidimensional arrays can be described as “arrays of arrays”. For example, a bi dimensional
array can be imagined as a bi dimensional table made of elements, all of them of a same uniform data
type.
0 0 1 2 3 4
jimmy 1
2
jimmy[1][3]