0% found this document useful (0 votes)
52 views14 pages

Java Programming Module

This document provides an introduction to Java programming. It discusses what a programming language is and describes Java as a language that can "write once, run anywhere." The document then analyzes Java program structure, including packages, classes, methods, and data types. It also covers basic input/output in Java using System.out.println and the Scanner class, as well as operators, escape codes, and format specifiers. Examples are provided to demonstrate getting user input and output, dealing with strings, and saving records.

Uploaded by

lance laguerta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
52 views14 pages

Java Programming Module

This document provides an introduction to Java programming. It discusses what a programming language is and describes Java as a language that can "write once, run anywhere." The document then analyzes Java program structure, including packages, classes, methods, and data types. It also covers basic input/output in Java using System.out.println and the Scanner class, as well as operators, escape codes, and format specifiers. Examples are provided to demonstrate getting user input and output, dealing with strings, and saving records.

Uploaded by

lance laguerta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

DOT NODE

E-LEARNING

LEARN

JAVA PROGRAMMING
Chapter 1: Introduction to Basic Computer Programming (JAVA)

Computer programming is different from basic computer operations; it is a discipline wherein,


analysis and designs are utilized by using a programming language. From these discipline, programmers
can create several types of program application.

What is programming Language?


It is a language, used as a medium for interaction between human and computer or it is a set of
instruction that the computer will do a task. From this task, program applications are made.

The JAVA Language (“Write Once, Run Anywhere”)


The microprocessor revolutions most important contribution to date is that it made possible the
development of personal computers, which now number about a billion worldwide. Personal computers
have profoundly affected people’s lives and the ways organizations conduct and manage their business.
Microprocessors are having a profound impact in intelligent consumer-electronic devices.

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.

Analyzing the structure of the program

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

Variable and data type’s scopes


Variables are any type of characters, acronyms, and text used as only to assign such values.
While data types are declaration type, which determines the type of value to be assigned in a variable.
Name Description Size* Range*
char Character or small integer 1byte Signed: -128 to 127
Short Short integer 2bytes Signed: -32768 to 32767
Int Integer 4bytes Signed: -2147483648 to 2147483647
Long Long integer 4bytes Signed: -2147483648 to 2147483647
Float Floating point number 4bytes +/-3.4e +/-38(~7digits)
Double Double precision floating point number 8bytes +/-1.7e +/-308(~15digits)
bool Boolean value. It can take one of two 1byte True or false
values: true of false
Example:
Int x,y,z; - multiple declaration of variable in a single line
Float abc; - single variable declaration in a single line

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

Public static void main(String[] args){


int x; Local Variables
float z; Only that functions can access those
variables.
System.out.pritln(“fdseff”);
}
}

Note:
-take note the positions of the variables
-declared variables from a data type are also called as “primitive type or built in type”

Basic mathematical operators, escape codes and format specifiers.

Basic MATH operators

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

%s Specifies the format for string


%f Specifies the format for float
%d Specifies the format for decimal integer

Java Basic Output (Sytem.out.println)


Try out sample codes:

/*Sample 1 This is my first program


*prints an output of string in JAVA programming!
*/
Package sample1;
public class mysample{
public static void main(String[] args){
System.out.println(“This is my first program in JAVA programming!”);
}
}
/*Sample 2 Hello
*prints an output a modified string with the use of printf and code escapes World
*/
Package sample2;
public class mysample{
public static void main(String[] args){
System.out.printf(“%s\n%s\n”,”Hello”,”World”);
}
}

JAVA basic input (Scanner)


Try out this sample codes:
/*Sample 3 A
*gets a user input and prints the output A
*/
Package sample3;
Import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner Echo=new Scanner(System.in);
System.out.println(Echo.nextLine());
}
}

Dealing with strings


Try out this sample codes:
/* Sample 4 What’s your name? Juan
*gets a user input string and prints the output Juana
*/ Hello Juan Juana.
Package sample4; What is your favorite team?
import java.util.Scanner; The Isotopes
public class Main{ I like the isotopes too!

public static void main(String[] args) {


String mystring;
Scanner input=new Scanner(System.in);
System.out.println(“What’s your name?”); mystring=input.nextLine();
System.out.println(“Hello ” + mystring);
System.out.println(“What is your favorite team? ”);
mystring=input.netLine();
System.out.println(“I like ” + mystring + “ too!”);
}
}
/* Sample 4 What’s your name? Java
*gets a user input string and prints the output What’s your age? 13
*/ What’s your favorite
Package sample4; number?7
import java.util.Scanner; You are Java, age 13 and
public class Main{ your favorite number is 7
public static void main(String[] args) { New record is saved!”);
string mystring;
int age,fav;

Scanner input=new Scanner(System.in);


System.out.println(“What’s your name?”); mystring=input.nextLine();
System.out.println(“\nWhat is your age?”);age=input.nextInt();
System.out.println(“\nWhat is your favorite number?”);
fav=input.nextInt();
System.out.printf(“\n You are %s, age is %d and your favorite number is
%s \n”,mystring,age,fav);
System.out.printf(“New record is saved”);
}
}
Chapter 3: Conditional Statement
Relational Operators
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

(7==5) // evaluates to false.


(3!=2) // evaluates to true.
(5>4) // evaluates to true.
(5<6) //evaluates to false.
(6<=5) // evaluates to false.
(6>=6) //evaluates to true

Example:
((5==5) && 3>6)) // evaluates to false (true && false).
((5==5) || (3>6) // evaluate to true (true|| false).

Logical operators

AND (&&) OPERATOR OR(||) OPERATOR


a b a&&b a b a||b
true true true true true true
true false false true false true
false false false false true true
false false false false false false
The operator && The operator
corresponds with ||corresponds with
Boolean logical Boolean logical operation
operation AND. This OR. This operation results
operation results true if true if either one of its
both its two operands two operands is true, thus
are true, and false being false only when
otherwise. both operands are false
themselves.

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

Executes the other statement when it is false


If(x==100)
System.out.println (x is 100);
Else
System.out.println(x not 100);

Nested if condition statements


Nested statement of if statement wherein it is used a selective statement due to the array of
conditions.

Try out these sample codes:

/*Sample 5 Input first integer: 1


*A simple example on logical operator with conditional statement if else Input second integer: 1
*using AND operator The answer of Logical AND is 1
*/
Import java.util.Scanner;
public class Sample{
public static void main(String[] args){
int x, y;
Scanner input = new Scanner(System.in);
System.out.println(“Input first integer: ”); x=input.nextInt();
System.out.println(“Input second integer: ”); y=input.nextInt();
If(x==1 && y==1)
System.out.println(“\n The answer of logical AND is 1”);
}
}
/*Sample 6 Input first integer: 1
*A simple application example of if statement Input second integer:1
*nested if statement Input third integer:0
*/ 0 is not equal to 1,1
Import java.util.Scanner;
public class Sample{
public static void main(String[] args){
int x, y, z;
Scanner input = new Scanner(System.in);
System.out.println(“Input first integer:”); x=input.nextInt();
System.out.println(“\n Input second integer:”); y=input.nextInt();
System.out.println(“\n Input third integer:”); z=input.nextInt();
If(x==y &&y==z&&z==x)
System.out.printf(“\n %s is not equal to %s,%s”,x,y,z);
else if(x==y&&y!=z&&z!=x)
System.out.printf(“\n %s is not equal to %s,%s”,z,y,z);
else if(x!=y&&y==z&&z!=x)
System.out.printf(“\n %s is not equal to %s,%s”,x,y,z);
else if(x!=y&&y!=z&&z==x)
System.out.printf(“\n %s is not equal to %s,%s”,y,x,z);
}
}
Chapter4: Control Structures

Compound statement and operators(Counter)


Compound Sample Equivalent Explanation
Assignment Expression
+= a+=2
-= b-=3
*= c*=4
/= d/=d
%= e%=25
Compound
Operators
++ ++a(prefix)
a++ (suffix)
-- --a(prefix) a=1-a The variable a is decreased by 1
a—(suffix) a=a-1 The variable a is decreased by

Loop (Iteration) Structures


while loop
its functionality is simply to repeat statement while the condition set is true

Try out this sample code:

/*Sample 7 Enter the starting number>8


*custom countdown using while 8,7,6,5,4,3,2,1, FIRE !
*/
import java.util.Scanner;
public class Sample{
public static void main(String[] args){
int x;
Scanner input = new Scanner(System.in);
System.out.println(“Enter the stating number>”); x=input.nextInt();
while(x>0){
System.out.printf(“%d”,x);
x--;}
System.out.println(“FIRE!”);
}
}

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

Try out this sample codes:


/*Sample10 Index Value
*simple array 0 0
*/ 1 0
public class InitArray{ 2 0
public static void main(String args[] ){ 3 0
int array[] = new int[ 10 ]; 4 0
System.out.printf(“%s%8s\n”, “Index”, “Value”); 5 0
for (int counter=0; counter<array.length;counter++) 6 0
System.out.printf(“%5d%8d\n”, counter, array[counter]); 7 0
} 8 0
} 9 0

/*Sample11 Index Value


*simple array 0 32
*/ 1 27
public class InitArray{ 2 64
public static void main(String[] args){ 3 18
int array[] = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37}; 4 95
System.out.printf(“%s%8s\n”,”Index”, “Value”); 5 14
for(int counter= 0; counter < array.length;counter++) 6 90
System.out.printf(“%5d%8d\n”, counter,array[counter]); 7 70
} 8 60
} 9 37

/*Sample12 Index Value


*simple array 0 2
*/ 1 4
public class InitArray{ 2 6
public static void main(String[] args){ 3 8
final int ARRAY_LENGTH = 10; 4 10
int array[] = new int[ARRAY_LENGTH]; 5 12
for(int counter = 0; counter<array.length ;counter++) 6 14
array[counter]=2+2*counter; 7 16
System.out.printf(“%s%8s\n”,”Index”,”Value”); 8 18
for(int counter = 0; counter<array.length;counter++) 9 20
System.out.printf(“%5d%8d\n”, counter,array[counter]);

}
}
/*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();
}
}
}

/*Sample16 Rating Frequency


* Using the elements of array as Counters 1 2
*/ 2 2
public class StudentPoll{ 3 2
public static void main(String[] args){ 4 2
int responses[] = 5 5
{1,2,6,4,8,5,9,7,8,10,1,6,3,8,6,5,7,6,8,6,7,5,6,6,5,6,7,5,6,4,8,6,8,10} 6 11
int frequency[]= new int[11]; 7 5
for(int answer =0; answer<responses.lenght;answer++) 8 1
++frequency[responses[answer]]; 9 7
System.out.printf(“%s%10s”,”Rating”,”Frequency”); 10 3
for(int rating=1;rating<frequency.length;rating++)
System.out.printf(“%d%10d”,rating,frequency[rating]);
}
}

/*Sample13 Total of array elements:849


*simple array
*/

public class SumArray{


public static void main(String[] args){
int array[] = {87,68,94,100,83,78,85,91,76,87};
int total = 0;
for (int counter = 0; counter<array.length;counter++)
total+=array[counter];
System.out.printf(“Total of array elements:%d\n”,total);
}
}
Enhanced for Statements
It iterates through the elements of an array or a collection without using a counter(thus avoiding
the possibility of”stepping outside” the array).
for(parameter: arrayName)
statement

/*Sample17 Total of array elements: 849


*Enhance for statement
*/
public class EnhancedForTest{
public static void main(String[] args){
int array[] = {87,68,94,100,83,78,85,91,76,87};
int total=0;
for(int number:array)
total+=number;
System.out.printf(“Total of array elements: %d\n “,total);
}
}

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]

Note: multidimensional area can be a single array


int jimmy [3][5]; // is equivalent to
int jimmy [15] //(3*5=15)

/*Sample18 Values in array1 by row are


*Multidimensional Array 123
*/ 456
public class InitArray{ Values in array2 by row are
public static void main(String[] args){ 12
int array1[][]= {{1,2,3},{4,5,6}}; 3
int array1[][]= {{1,2},{3},{4,5,6}}; 456
System.out.println(“Values in array1 by row are”);
outputArray(array1); // displays array1 by row
System.out.println(“Values in array2 by row are”);
outputArray(array1); // displays array2 by row
}
public static void outputArray(int array[][]){
for(int row = 0; row<array.length;row++){
for (int column=0;column< array[row].length; column++)
System.out.printf(“%d”,array[row][column]);
System.out.println()
}
}
}

You might also like