Вы находитесь на странице: 1из 15

11 – ICT (Object-Oriented Programming: JAVA)

A.Y. 2019-2020

PROGRAMMING BASICS

 CHARACTER SET

1. ALPHABETIC CHARACTERS  These are the uppercase and lowercase letters of alphabet (A-Z and a-z)
2. NUMERIC CHARACTERS  These are the positive or negative numbers with or without decimal places.
3. SPECIAL CHARACTERS  These are the characters that have specific meanings in a particular
programming language.

EXAMPLES: space = + - * / \ ^ ( ) % # $ ! , . ; : ? < > “ _

 CONSTANT

A constant is any specific value that does not change. There are 3 major types of constants: character
constants, string constants and numeric constants.

1. CHARACTER CONSTANT  It consists of a single number, symbol, or letter. It is usually enclosed in


single quotation marks.

EXAMPLES: ‘a’, ‘b’, ‘c’ …. ‘z’


‘A’, ‘B’, ‘C’ …. ‘Z’
‘0’, ‘1’, ‘2’ …. ‘9’
‘_’, ‘+’, ‘-’ …. ‘&’

2. STRING CONSTANT  It is a sequence of characters enclosed in quotation marks (typically “ ”). They
may be used to label output and print out textual messages.

EXAMPLES: “hello”
“Juan Dela Cruz”
“0912-345-6789”
“P1,224.04”

3. NUMERIC CONSTANT  It is any positive or negative number consisting of digits 0 through 9 and a
decimal point. Negative constants need a leading minus sign (-), a plus sign (+) is optional on a positive
number. Numeric constants cannot contain commas. Generally, they can be expressed in two different
forms.

a. INTEGER CONSTANT  It is a whole number (range values depend on the data type given to a
specific variable). They do not have decimal points or any other punctuation.

EXAMPLES: 0 -5678 91898


+1217 61291 -929

b. DECIMAL CONSTANT (Floating Point Numbers)  It is any positive or negative real number that
contains a decimal point.

EXAMPLES: 0.911 +706.02 -329.97


826.68 -42568.08 8.08

 RESERVED WORDS

These are certain words that have special meaning or function in specific programming language. These
include all commands, statements, function names, and operator names. Reserved words CANNOT be used as
variable names but they can be a part of a name.

 VARIABLES

These are data location in computer’s memory with arbitrary chosen name as address. These indicate the
locations where data are stored. They provide a way of referring to the data stored in them by name.

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
A variable can store one data only at any given time. The concept of a variable may be graphically
illustrated below:

24 This is the variable of the memory location of our data.

AGE
This is the content of the variable. At this point in time,
the data is 24.

This is the variable name. It is an arbitrary name we have


chosen for our variable.

The values of the variable may be set to a constant (where the value is fixed until such time you set it again
to another value) or to a result of calculations or various input statements in a program (where the value
stored in the variable may vary from one processing time to another).

 VARIABLE NAMES

As the word implies, a variable name is an arbitrary name or label chosen to refer to the data stored in a
particular memory location.

Any variable name may be chosen to label a variable. However, a variable name that is descriptive and
meaningful of the type of data it stands for could enhance clarity or the program.

For example, the variable name, “grade” may be used to name a variable that will be used to store a student’s
grade.

NOTE: Variable and variable names may be used synonymous in the programming proper.

RULES IN CREATING A VARIABLE NAME

1. A variable name can contain letters, numbers, and an underscore (_).


2. The first character of a variable name must be a letter (or underscore in some Programming Languages).
3. A variable name CANNOT be a PL’s reserved word.
4. A variable name CANNOT contain a space.

EXAMPLES: firstName lastName


(Valid names) year_level grade9
basicPay salary

EXAMPLES: rate/hour  Rule #1


(Invalid names) 1stNumber  Rule #2
Private  Rule #3
gross pay  Rule #4

DATA REPRESENTATION
 TYPES OF NUMBER SYSTEM

1. DECIMAL SYSTEM  This is commonly used by humans for counting. The symbols used are called Arabic
numerals consisting of 10 symbols (0 1 2 3 4 5 6 7 8 9). The base of the decimal system is ten (10).
2. BINARY SYSTEM This is used in digital computers. They operate one of two states, either on or off,
true or false, north or south magnetization, and other more. It uses only two symbols: 0 and 1. The base
of binary system is two (2).
3. OCTAL SYSTEM  This is a shorthand method for replacing groups of binary digits by single octal digit to
reduce number of digits required in representing any number. It has a base of eight consisting of 0, 1, 2,
3, 4, 5, 6, 7. The base of an octal is eight (8).
4. HEXADECIMAL SYSTEM  This is the simplest method of expressing value represented by binary
numerals. It is a combination of 10 numbers and 6 letters of the alphabet, namely: 0, 1, 2, 3, 4, 5, 6, 7,

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
8, 9, A, B, C, D, E, and F. These 6 letters represent digits 10 through 15 and these are treated as ordinary
numerals. The base of hexadecimal system is sixteen (16).

 COMPUTER CODES

1. BCD (Binary Coded Decimal System)  A very common representation of the upper case letters, the
digits (0-9), special characters + - * , . () $ = ‘ and space (48 characters). It uses 6 bits.
2. SBCD (Standardized Binary Coded Decimal System)  Provides few variations of common characters
and can be found in most computer machine manuals.
3. ASCII (American Standard Code for Information Interchange)  This is one of the codes to
represent more than 64 characters that includes special characters and lower case letters. This was
designed to be an American National Code.
 Most microcomputer manufacturers used ASCII to represent data.
4. EBCDIC (Extended BCD Interchange Code)  This is an extension BCD that is used by mainframes
and minicomputer. Like ASCII, each byte contains eight bits with maximum of 256 characters that can be
represented.
 The table below illustrates how numbers and letters are coded in both ASCII and EBCDIC.

CHARACTER ASCII EBCDIC


0 0101 0000 1111 0000
1 0101 0001 1111 0001
2 0101 0010 1111 0010
3 0101 0011 1111 0011
4 0101 0100 1111 0100
5 0101 0101 1111 0101
6 0101 0110 1111 0110
7 0101 0111 1111 0111
8 0101 1000 1111 1000
9 0101 1001 1111 1001
A 1010 0001 1100 0001
B 1010 0010 1100 0001
C 1010 0011 1100 0011
D 1010 0100 1100 0100
E 1010 0101 1100 0101

 NUMBER SYSTEM CONVERSIONS

1. DECIMAL TO ANY NUMBER SYSTEM

To convert decimal to any system, repeated division by the new number system’s base technique is used.
The steps are as follows:

a. Divide the decimal number to be converted by the new base (for example, divide 2 for binary, 8
for octal, and 16 for hexadecimal).
b. Write the remainder on the right side.
c. Repeat the process until the quotient is zero (0).
d. Collect all the remainders starting from the last quotient going upward and write them from left to
right.

 DECIMAL TO BINARY

7410 Binary Answer: 7410 = 10010102

2 74
2 37 r. 0
2 18 r. 1
2 9 r. 0

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
2 4 r. 1
2 2 r. 0
2 1 r. 0
0 r. 1

 DECIMAL TO OCTAL

7410 Octal Answer: 7410 = 1128


** To get the remainder if the answer follows 2 or more digits of decimal numbers, just multiply 8 to the decimal numbers. for

this example, when you divide 74 by 8, you will get 9.25. To get the final remainder, just multiply ‘8’ to the excess decimal

which is .25 and you will get 2.

8 74
8 9 r. 2
8 1 r. 1
8 0 r. 1

 DECIMAL TO HEXADECIMAL

7410 Hexadecimal Answer: 7410 = 4A16


** To get the remainder if the answer follows 2 or more digits of decimal numbers, just multiply 16 to the decimal numbers. for

this example, when you divide 74 by 16, you will get 4.625. To get the final remainder, just multiply ‘16’ to the excess decimal

which is .625 and you will get 10 then convert it to its equivalent character.

16 74
16 4 r. 10 or A
16 0 r. 4

2. ANY NUMBER SYSTEM TO DECIMAL

 BINARY TO DECIMAL

10010102 Decimal Answer: 10010102 = 7410

1 0 0 1 0 1 0
| | | | | | |
26 25 24 23 22 21 20

64 + 0 + 0 + 8 + 0 + 2 + 0 = 74 10

 OCTAL TO DECIMAL

1128 Decimal Answer: 1128 = 7410

1 1 2
| | |
82 81 80

64 + 8 + 2 = 7410

 HEXADECIMAL TO DECIMAL

4A16 Decimal Answer: 4A16 = 7410

4 A or 10
| |
161 160

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020

64 + 10 = 7410

3. BINARY TO OCTAL

The steps for converting binary to octal are as follows:

a. Divide the binary digits into groups of three. Fill in with zeroes to the left of the first digit of the
given number in order to complete the grouping.
b. Using the place value the binary system (see Conversion Table), add the position with
corresponding 1 digit or bit.

10010102 Octal Answer: 10010102 = 1128

001 001 010

1 1 2

4. BINARY TO HEXADECIMAL

The steps for converting binary to hexadecimal are as follows:

a. Divide the binary digits into groups of four. Fill in with zeroes to the left of the first digit of the
given number in order to complete the grouping.
b. Using the place value of the binary system (see Conversion Table), add the positions with
corresponding 1 digit or bit.

10010102 Hexadecimal Answer: 10010102 = 4A16


0100 1010

4 A

5. OCTAL TO BINARY

The steps in converting octal to binary are as follows:

a. Convert each octal digit into a three bit patterns.


b. The zeroes to the left of the left-most digit are ignored.

1128 Binary Answer: 1128 = 10010102

001 001 010

6. HEXADECIMAL TO BINARY

The steps in converting octal to binary are as follows:

a. Convert each hexadecimal digit into a four bit patterns.


b. The zeroes to the left of the left-most digit are ignored.

4A16 Binary Answer: 4A16 = 10010102

0100 1010

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020

OBJECT ORIENTED PROGRAMMING

PROGRAMMING PROCESS

1. Defining the problem


 determine the requirement the program must meet
 Input – data / requirements that are inputted
 Process – the operations
 Output – shows the result

2. Planning the solution


 select the best method for solving the problem
 Algorithm – a set of rules for solving a problem in a finite number of steps
 Pseudocode – an informal high-level description of the operating principle of a computer
program or other algorithm
 Flowchart – a type of diagram that represents an algorithm, workflow or process, showing
the steps as boxes of various kinds, and their order by connecting them with arrows.

3. Coding the program


 prepare the set of instructions for the computer to execute
 program must be keyed in a form that the computer can understand
 the correct usage of programming language
 Ex. of earlier programming languages – BASIC, COBOL, Pascal, FORTRAN, and C language.

4. Testing the program


 perform debugging and testing of the program, using sample input data

Methods:
1. Program Checking – proofreading
2. Translating – uses translators to convert the program in a form that the computer understands
3. Debugging – the process of directing, locating, and correcting bugs within a program
 Bugs – errors in programs

2 types of Bugs
1. Syntax Errors – character or string (syntax/command) incorrectly placed in a command
or instruction that causes a failure in execution.

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
2. Semantic Errors – logical errors that produce incorrect results when the instructions
are executed. The syntax of the source code may be valid, but the algorithm being
employed is not.

5. Documenting the program


 write up the full specifications for other program users
 used to make programs readable by other programmers

OOP (Object Oriented Programming)


 It is a software development methodology in which a program is conceptualized as a group of objects that
work together.
JAVA Programming language
 It was originally developed by Sun Microsystems which was initiated by James Gosling and released in
1995 as core component of Sun Microsystems’ Java platform, and promised Write Once, Run Anywhere (WORA),
providing no-cost run-times on popular platforms.
JAMES ARTHUR GOSLING
 He is a Canadian computer scientist, best known as the father of the Java programming language.

JAVA BASIC ELEMENTS


Object  Objects are the core of Java Programs. Object may represent a real world problem which we are trying to
found solution to. Objects have states and behaviors.
Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating.
An object is an instance of a class.
Attributes  are the characteristics that define an object
Class  A class can be defined as a template/blue print that describes the behaviors/states that object of its type
support.
2 Components of a Java Class
a. Class Declaration  contains the class name and the access modifier.
b. Class Body  comes after the class declaration and it is contained within braces. It contains the functions
and variables.
Methods  A method is basically a behavior. It contains the logic of the application, it does operation on the data. A
class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions
are executed. It is also a self-contained block of program code, similar to a procedure.
Variables  These are the container of data. Object state is carried by variables.
 Instance Variables  Each object has its unique set of instance variables. An object's state is created by
the values assigned to these instance variables.

FUNDAMENTAL CONCEPTS OF OOP


1. ABSTRACTION  is the process of hiding the working style of an object, and showing the information of an
object in understandable manner.
2. POLYMORPHISM  means “many forms” – it describes the feature of language that allows the same word or
symbol to be interpreted correctly in different situations based on the context.

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
3. ENCAPSULATION  is like enclosing in a capsule, wherein it hides the data and methods within an object.
4. INHERITANCE  the ability to create classes that share or acquire the attributes and methods of other
existing classes.
a. SUBCLASS  class that inherits from another class.
b. SUPERCLASS  class that gives the inheritance.
 NOTE: A class can have only one superclass, but each class can have an unlimited number of subclasses.
JAVA  A Java program is made up of classes and objects, which, in turn, are made up of methods and variables.
Java Program Types
1. Java Applets – programs that are embedded in a Web Page
2. Java Application – Stand-alone applications
a. Console Applications – support character output to a computer screen in a DOS window
b. Windowed Applications – create a GUI with elements

STATEMENT  is a simple command that causes something to happen. It also represents a single action taken in a
Java program.
 Statements in Java are grouped using the opening brace ({) and closing brace (}). A group of statements
organized between these characters is called a block or block statement
EXPRESSION  is a statement that produces a value.
 The value produced by a statement is called its return value.

Anatomy of Java Statement


System.out.println (“First java application”);

VARIABLES
VARIABLE  A variable is a place where information can be stored while a program is running.

3 KINDS OF VARIABLES IN JAVA


1. INSTANCE VARIABLE  used to define an object’s attributes.
2. CLASS VARIABLE  define the w an entire class of objects and apply to all instances of it.
3. LOCAL VARIABLE  used inside method definitions or even smaller blocks of statements within a method.

VARIABLE DECLARATION
 Variables in the main method
public static void main(String[] args) {
int total;
String reportTitle;
boolean active;
}

 Variables of the same type

String street, city, state;

 Variables with assigned values

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
int zipCode = 90210;
int box = 350;
boolean pbs = true;
String name = “Zoom”, city = “Boston”, state = “MA”;

Rules of Java Legal Identifier

1. Starts with [a-z][A-Z], or _, or $. Just don’t begin with a digit.


2. First character can be followed by any series of letters or digits.
3. Must not be a Java Keyword.

Correct/Legal Class/Variable Names


 Variable: myAge, yourAge, myName

 Class: MyClass1, Calculator, MySuperClass, MySubClass

Numeric-type Conversion
CORRECT
int hoursWorked = 37;
double payRate = 6.73;
double grossPay = hoursWorked * payRate;
INCORRECT
int hoursWorked = 37;
double payRate = 6.73;
int grossPay = hoursWorked * payRate;

DATA TYPES
PRIMITIVE DATA TYPES  integers, floating-point numbers, characters, and Boolean values which are built-in parts
of the Java language rather than objects, which makes them more efficient to use.
INTEGER DATA TYPE
 Data type that can hold either positive or negative numbers.

FLOATING POINT NUMBERS  are numbers with a decimal point

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
 float  can handle any number from 1.4E-45 to 3.4E+38

 double  can be used for more precise numbers ranging from 4.9E-324 to 1.7E+308

CHARACTER TYPE (‘char’)  used for individual characters, such as letters, numbers, punctuation, and other
symbols.

BOOLEAN TYPE (‘boolean’)  this data type holds either true or false

REMEMBER:
 All these variable types are listed in lowercase, and you must use them as such in programs.

 There are classes with the same names as some of these data types but with different capitalization—for
example, Boolean and Char

CLASS TYPES
In addition to the primitive data types, a variable can have a class as its type, as in the following examples:
String lastName = “Hopper”;
Color hair;
VolcanoRobot vr;
When a variable has a class as its type, the variable refers to an object of that class or one of its subclasses.

ASSIGNING VALUES TO A VARIABLE


CONSTANT (Constant Variable)  is a variable with a value that never changes.
 To declare a constant, use the final keyword before the variable declaration and include an initial value for
that variable, as in the following:
final float PI = 3.141592;
final boolean DEBUG = false;
final int PENALTY = 25;

COMMENTS
 are information included in a program strictly for the benefit of humans trying to figure out what’s going on
in the program. The Java compiler ignores comments entirely when preparing a runnable version of a Java source file.

3 KINDS OF COMMENTS

1. Single-line Comment  starts with 2 slashes (“//”)


int creditHours = 3; // set up credit hours for course
2. Block Comment  begins with “/*” and end it with “*/”
/* This is a BLOCK COMMENT
This is a BLOCK COMMENT */
3. Official Comment  begins with “/**” and end it with “*/”

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
 interpreted to be official documentation on how the class and its methods work.

USING “Scanner” CLASS


METHODS
 nextLine() – does not retrieve data until the user presses Enter

 nextInt()

 nextDouble()

 next()

import java.util.Scanner
public class GetUserInfo
{
public static void main (String[] args)
{
String name;
int age;
Scanner inputDevice = new Scanner(System.in);
System.out.print(“Please enter your name >> “);
name = inputDevice.nextLine();
System.out.print(“Please enter your age >> “);
age = inputDevice.nextInt();
}
}

USING “printf()” METHOD

printf() method  automatically uses Formatter to create a formatted string.

SYNTAX: System.out.printf(“format-string”[,arg1,arg2..]);

Ex. double grade = 43.78024756;


System.out.printf(“Your grade is %.2f.”, grade);

%d A decimal integer [byte, short, int, long].

%f A floating-point number.

A new line character appropriate to the platform running the application.


%n
You should always use %n, rather than \n.

%c A Character (Capital C will make a character in UPPERCASE)

%s A String (Capital S will make a string in UPPERCASE)

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020

JAVA BASIC OPERATORS


1. ARITHMETIC OPERATORS  are used in mathematical expressions in the same way that they are used in
algebra.

Operator Description Example


+ Addition - Adds values on either side of the operator A + B will give 30

- Subtraction - Subtracts right hand operand from left hand A - B will give -10
operand
* Multiplication - Multiplies values on either side of the operator A * B will give 200

/ Division - Divides left hand operand by right hand operand B / A will give 2

% Modulus - Divides left hand operand by right hand operand and B % A will give 0
returns remainder
++ Increment - Increases the value of operand by 1 B++ gives 21

-- Decrement - Decreases the value of operand by 1 B-- gives 19

2. RELATIONAL OPERATORS

Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example


== Checks if the values of two operands are equal or not, if yes (A == B) is not true.
then condition becomes true.
!= Checks if the values of two operands are equal or not, if (A != B) is true.
values are not equal then condition becomes true.
> Checks if the value of left operand is greater than the value (A > B) is not true.
of right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of (A < B) is true.
right operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal (A >= B) is not true.
to the value of right operand, if yes then condition becomes
true.
<= Checks if the value of left operand is less than or equal to (A <= B) is true.
the value of right operand, if yes then condition becomes
true.

3. LOGICAL OPERATORS
Assume Boolean variables A holds true and variable B holds false, then:

Operator Description Example


&& Called Logical AND operator. If both the operands are non- (A && B) is false.
zero, then the condition becomes true.
|| Called Logical OR Operator. If any of the two operands are (A || B) is true.
non-zero, then the condition becomes true.
! Called Logical NOT Operator. Use to reverses the logical !(A && B) is true.
state of its operand. If a condition is true then Logical NOT
operator will make false.

4. ASSIGNMENT OPERATORS

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
Operator Description Example
= Simple assignment operator, Assigns values from right side C = A + B will assign value of A +
operands to left side operand B into C
+= Add AND assignment operator, It adds right operand to the left C += A is equivalent to C = C + A
operand and assign the result to left operand
-= Subtract AND assignment operator, It subtracts right operand C -= A is equivalent to C = C - A
from the left operand and assign the result to left operand
*= Multiply AND assignment operator, It multiplies right operand C *= A is equivalent to C = C * A
with the left operand and assign the result to left operand
/= Divide AND assignment operator, It divides left operand with the C /= A is equivalent to C = C / A
right operand and assign the result to left operand
%= Modulus AND assignment operator, It takes modulus using two C %= A is equivalent to C = C %
operands and assign the result to left operand A

DECISION MAKING STATEMENTS


 …IF STATEMENTS
1. if statement  An if statement consists of a Boolean expression followed by one or more
statements.
 SYNTAX
if (Boolean_expression)
{ //Statements will execute if the Boolean expression is true
}

2. if… else statement  An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false.
 SYNTAX
if (Boolean_expression)
{ // Executes when the Boolean expression is true
}
else
{ //Executes when the Boolean expression is false
}

3. if… else if… else statement  An if statement can be followed by an optional else if...else
statement, which is very useful to test various conditions using single if...else if statement.
 SYNTAX
if (Boolean_expression1)
{ //Executes when the Boolean expression 1 is true
}
else if (Boolean_expression2)
{ //Executes when the Boolean expression 2 is true
}
else if (Boolean_expression3)
{ //Executes when the Boolean expression 3 is true
}
else
{// Executes when the none of the above condition is true.
-}

 SWITCH STATEMENT
 A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for each case.
 SYNTAX
switch(expression)
{
case value :
//Statements

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
break;//optional
case value :
//Statements
break;//optional
//You can have any number of case statements.
default://Optional
//Statements
}

JAVA MODIFIER TYPES


1. DEFAULT ACCESS MODIFIER – No Keyword

 A variable or method declared without any access control modifier is available to any other
class in the same package. The fields in an interface are implicitly public static final and the methods in
an interface are by default public.

Ex. String version ="1.5.1";


boolean processOrder ()
{
return true;
}

2. PRIVATE ACCESS MODIFIER – private

 Methods, Variables and Constructors that are declared private can only be accessed within
the declared class itself.

Ex. public class Logger


{
private String format;
public String getFormat()
{
return this.format;
}
}

NOTE: Here, the format variable of the Logger class is private, so there's no way for other classes to
retrieve or set its value directly. So to make this variable available to the outside world, we defined
public method: getFormat(), which returns the value of format.

3. PUBLIC ACCESS MODIFIER – public


 A class, method, constructor, interface etc declared public can be accessed from any other
class. Therefore fields, methods, blocks declared inside a public class can be accessed from any
class belonging to the Java Universe.

Ex. public static void main(String[] arguments)


{ // ...
}

4. PROTECTED ACCESS MODIFIER – protected


 Variables, methods and constructors which are declared protected in a superclass can be
accessed only by the subclasses in other package or any class within the package of the protected
members' class.
Ex. class AudioPlayer
{

Prepared By: Mr. Jerreck Reynald D. Navalta


11 – ICT (Object-Oriented Programming: JAVA)
A.Y. 2019-2020
protected boolean openSpeaker(Speaker sp)
{ // implementation details
}
}

class StreamingAudioPlayer
{
boolean openSpeaker(Speaker sp)
{ // implementation details
}
}
JAVA LOOP CONTROL
LOOPS or REPETITION STRUCTURE  allows the programmer to specify that an action is to be
repeated and executed several number of times while a condition remains true.

1. while LOOP  is a control structure that allows you to repeat a task a certain number of times,
while a condition remains true.

 SYNTAX
while (Boolean_expression)
{
//Statements
}

2. do…while LOOP  is exactly similar to while LOOP, except that a do...while LOOP is guaranteed
to execute at least one time.

 SYNTAX
do
{
//Statements
}while (Boolean_expression);

3. for LOOP  is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.

 SYNTAX
for (initialization; Boolean_expression; increment/decrement)
{
//Statements
}

Prepared By: Mr. Jerreck Reynald D. Navalta

Вам также может понравиться