You are on page 1of 10

ICT 10 - 1st Quarter Examination Reviewer

Credits: Hielden Martinez and Jaden Lopena


Introduction to Java
❖ Formatting Conventions
➢ Italics - used to distinguished filenames, directories, or class names
➢ Boldface- used to indicate important terms in the lesson
➢ C o u r i e r - used to represent Java syntaxes
❖ Object-Oriented Programming (OOP)
➢ Was conceived to model real-world objects. In OOP, a class defines the common
variables (attributes) and methods (functions) of a set of objects.
➢ An object is an instance of a class and is specific. For example, all humans have
common attributes: name, age, birthday, etc.; and functions: getting old or
growing up.

Class (Attributes) Object (Specific)


Name Andres Bonifacio
Age 33
Birthday November 30. 1863

❖ Benefits of Java
➢ Java is simple - The Java programming language was designed to make OOP
easier. Once you understand how components in a program are treated as
objects, you will find Java fairly simple.
➢ Java is reliable - Java is reliable because it excludes some components that are
error prone. It also provides a way for programmers to anticipate and handle
errors in their program.
➢ Java is dynamic - Java makes it possible to reuse previously written programs
to lessen the workload of a programmer. Aside from this, it also allows
programmers to make revisions, additions and alterations to a program without
affecting other programs that might already be using it.
Classes are loaded while the program is run either from your own computer, from
another computer in your network, or from a computer connected to the Internet.
➢ Java is used in networking - The Java language has many classes to facilitate
Internet communications. Java classes can be loaded in most browsers to
facilitate secure transactions over the Internet.
➢ Java is secure - Java ensures that a program can go only where it was designed
to go. It eliminates the possibility of altering system data unintentionally.
➢ Java is free - Java can be downloaded from the Internet for free. Just visit the
site: http://java.sun.com/
➢ Java is portable - Java codes have the filename extension .java. When
compiled, a class file with .class extension is created. This file can then run on any
machine which has the Java Virtual Machine, thus, making it portable. Java-
enabled devices such as mobile phones are commonly used today.

1
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena
Java Keywords, Identifiers, Data Types, and Casting
❖ Java Keywords
➢ In computer programming, the right names and labels are important if you want
your program to work smoothly and without glitches. The wrong word or label
might destroy your entire program script if you are not careful- or at the very
least, make it impossible for the program to run smoothly.
➢ All Java keywords are in lowercase. Java is case-sensitive so if you capitalize any
letter of a keyword, it becomes an identifier.
❖ Identifiers
➢ Identifiers are user-defined names for methods, variables, constants, and
classes. In creating identifiers, follow these rules:
1. The first character of your identifier should start with a letter of the
alphabet, an underscore, or a dollar sign. After that, the identifier can
then be composed of alphanumeric characters and underscores.
2. Create identifiers that are descriptive of their purpose. If your identifier is
composed of several words, capitalizing the first letter of each word is a
good programming practice.
*65, 535 characters is the maximum length per identifier
3. Your identifier must contain no spaces. Examples of valid and invalid
identifiers are as follows:

Valid Identifiers Invalid Identifiers


$rate 3_rate
LastName Name,pls!
Last_Name Const
_main Full Name

❖ Data Types
➢ Java has two sets of data types: primitive and reference (non-primitive). A table
of primitive data types with a few examples of data they can handle is provided
below.

Data Type Example


boolean true, false
char ‘A’, ‘z’, ‘\n’, ‘6’
byte 1
short 11
int 167
long 11167L
float 63.5F
double 63.5

2
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena
➔ boolean - A logical data type. In Java, it has only two possible values: true or
false. It can also be represented by 0 (false) or 1 (true)
➔ char - A data type used for single characters. In Java, this type corresponds to a
16-bit Unicode character*.
*16-bit Unicode or Unicode Transformation Format (UTF-16) is a method of
encoding character data.
➔ Next line ‘\n’ - Represents a line break. It is composed of the backslash
character and the letter “n” but it is interpreted by Java as a single character.
➔ float | double - Are floating point literals—numbers with possible decimal
parts; float is 32 bits in length and double is 64 bits long.
➔ String - A reference data type often used in many applications. It is actually a
class that lets us store a sequence of characters.
➔ Variables - Are identifiers whose values can be changed. The general syntax for
declaring a variable is: <data_type> <identifier>;
➔ Constants - In contrast to variables, constants are identifiers whose values never
change once declared. The general syntax for declaring constants is: final<type>
<identifier> = <literal>;
➔ Casting - The process of assigning a value or variable of a specific type to a
variable of another type.
Compiling and Executing A Java Program Using an IDE
❖ BlueJ
➢ Is an Integrated Development Environment (IDE) that is freely available on the
Internet and is used to compile and run the Java applications provided.
Download link: http://www.bluej.org/download/download.html
➢ Steps in using BlueJ to compile and run Java applications:
1. Run BlueJ by double-clicking the BlueJ icon on the desktop or running it
from Start > All Programs > BlueJ > BlueJ
2. To create a new project, click Project > New Project
3. The New Project dialog box will appear. In the File Name field, input the
name of your project. Click Create
4. Double-click this icon to open README.TXT
5. Type the appropriate information
6. After updating README.TXT, click Class > Close
7. Click Edit > New Class or press Ctrl + N at the same time
8. Type “Welcome” as the name of the new class then click Ok
9. Double-click the ‘Welcome’ icon to open the Welcome class.
10. Delete all the text included within the editor [Select all text or press Ctrl +
A > Backspace] and type the following:
11. Click Class > Close or press Ctrl + W at the same time
12. Create another class and name it “M a i n”. Edit the Main class by
double-clicking the Main icon. Delete all the text included within the editor
and type the following:
➢ Compiling your program using BlueJ

3
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena
➔ To compile all classes in a specific package, go to the package to be
compiled and click Compile
➢ Executing your Program using BlueJ
1. Right-click the Main icon and select void main(String[] args).
2. Click OK
3. The terminal window of BlueJ will appear to show the output. After you
are done, close all BlueJ windows.
➢ Your first Java program
➔ Analyzing Welcome.java
//Welcome to Java
public class Welcome{
public void printWelcome() {
System.out.println(“Welcome to Java!”) ;
}
}
➔ Line 1 //Welcome to Java
- Is a single-line comment. A comment is read by the Java compiler,
but, as a command, it is actually ignored. Any text that follows two
slash symbols (//) is considered a comment

➔ Line 2 public class Welcome{


- Defines the beginning of the Welcome class. When you declare a
class public, it can be accessed and used by other classes
➔ Line 3 public void printWelcome() {
- Line 3 shows the start of the method printWelcome ()
➔ Line 4 System.out.println(“Welcome to Java!”) ;
- Shows how to print text in Java. The println () method displays the
message inside the parentheses on the screen.
➔ Line 5 and 6 }
}
- Lines 5 and 6 contain two closing braces. The brace on line 5 closes
the method printWelcome () and the brace on line 6 closes the
class Welcome.
Java Operators
❖ Operators - symbols that perform logical or mathematical functions on operands such as
variables, constants, and objects.
❖ Unary Operators - require only one operand. In Java, there are at least four unary
operators:
➢ negation ( - )
➢ bitwise complement ( ~ )
➢ increment ( ++ )
➢ decrement ( -- )
❖ Arithmetic Operators

4
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena

Operators Description

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulo

++ Increment

–- Decrement

❖ Conditional/Logical Operators

Operators Description

! NOT

| | Logical OR

&& Logical AND


❖ Truth Tables
➢ Truth tables will help us demonstrate the result of a logical operation. Below are
the truth that you will need for evaluating logical operations.
➢ The NOT(!) operator is a unary operator that negates the value of the operand

Operand 1 Result

true false

false true

➢ The OR (||) operator is a binary operator that returns true if at least one of its
operands is true.

Operand 1 Operand 2 Result

True | | true true

true | | false true

true | | true true

true | | false false

5
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena
➢ The bitwise exclusive OR or XOR (^) operator is a binary operator that returns
true when both operands have different values. Otherwise, it will return false.

Operand 1 Operand 2 Result

True ^ true false

true ^ false true

false ^ true true

false ^ false false

➢ The AND (&&) operator is a binary operator that returns true only when both
operands are true. Otherwise, it will return false

Operand 1 Operand 2 Result

True && true true

true && false false

false && true false

false && false false

.Java Conditions and If Statements


❖ Java supports the usual logical conditions from mathematics:
➢ Less than: a < b
➢ Less then or equal to: a <= b
➢ Greater than: a > b
➢ Greater then or equal to: a >= b
➢ Equal to: a == b
➢ Not equal to: a != b
❖ Java has the following conditional statements:
➢ Use if to specify a block of code to be executed, if a specified condition is true
➢ Use else to specify a block of code to be executed, if the same condition is false
➢ Use else if to specify a new condition to test, if the first condition is false
➢ Use switch to specify many alternative blocks of code to be executed
❖ Conditions for Decisions
➢ Decisions are made using statements like if, if-else, nested if, and switch. In this
lesson, we will examine how these conditional statements are applied to simple
programming problems. We will definitely use the Boolean expressions you
learned from the previous lesson. (Boolean expressions are conditions on how
these control structures will behaved based on the given inputs.) (Programming
and Databases, 2017)
➢ If Statement - simplest among all the conditional statements

6
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena
- If the condition in the if statement is true, the succeeding statement will
be executed
➔ It’s a good programming habit to enclose the statement’s that the if
statement will execute between two braces even if there is only one
statement
➔ Syntax: if (condition) {
// block of code to be executed if the condition is true
}
➔ If (20 > 18) {
System.out.println(“20 is greater than 18”);
➢ Else statement - to specify a block of code to be executed if the condition is false
➔ Syntax: if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
➔ int time = 20;
if (time < 18) {
System.out.println(“Good day.”);
} else {
System.out.println(“Good evening.”);
}
// Outputs “Good evening.”
➢ Else if statement - specify a new condition if the first condition is false
➔ Syntax: if (condition1) {
// block of code to be executed if condition1 is true
} else if (conditon2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition 2 is false
}
- At times, we are given condition that may not be true. When you need to
execute specific statements depending on whether the condition is true or
false, use the if-else statement.
- If the if expression evaluates to false, the statement within the scope of
the else part will be run.
➔ Syntax: variable = (condition) ? expressionTrue : expressionFalse;
➔ int time = 20;
String result = (time < 18)? "Good day." : "Good evening.";
System.out.println(result);
➢ Switch statement - can be alternative to the nested-if statement because in the
switch statement, you are allowed to have several options. However, the values to

7
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena
be tested in the switch statement are limited to those data types int and char.
Other types like short and byte are automatically converted to int.
➔ Instead of writing many if..else statements, you can use the switch
statement.
➔ The switch statement selects one of many code blocks to be executed:
◆ The switch expression is evaluated once.
◆ The value of the expression is compared with the values of each
case.
◆ If there is a match, the associated block of code is executed.
◆ The break and default keywords are optional, and will be
described later in this chapter
➔ Syntax: switch(expression) {
case c:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Reviewer from Sir Don Renzo Santos
Definition of Terms
➢ Accent mark (~) An example of unary operators.
➢ Concatenation- A process of joining values together.
➢ Char - A data type used for single characters.
➢ ++ - A representation that is used for increment.
➢ % - A shorthand operator that represents modulo.
➢ Variables- An identifier whose values can be changed.
➢ + - - A symbol that is used to represent concatenation.
➢ Truth Tables NOT (!)- A unary operator that negates the value of operand.
➢ Boolean- A logical data type which has only two possible values.
➢ Database- A software that collects related data, organizes, and manages data.
➢ OR (||)- A binary operator that returns true if at least one of its operands is true.
➢ a = = b - A binary operator that indicates the relationship between two operands.
➢ (‘ ’)- An enclosure that is used for assigning literals to identifiers declared as char.
➢ OR or XOR (^) - A binary operator that returns true when both operands have different
values.
➢ AND (&&)- A binary operator that returns true only when both operands are true.
Otherwise, it will return false.
➢ Store Capacity - ability to store enormous amounts of data
➢ Type of Keys
➔ Special Keys
➔ Alphanumeric Keys

8
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena
➔ Function Keys
Facts About Java
➢ In analyzing Welcom.java Line 1 is a single-line comment.
➢ The println () method in analyzing Welcom.java displays the message inside the
parentheses.
➢ Java is a popular programming language, created in 1995
➢ Java is one of the most popular programming languages globally.
➢ Java is an object-oriented language which gives a clear structure to programs and
allows code to be reused, lowering development costs.
➢ The brace on line 5 of analyzing Welcome.java closes the method printWelcome () and
the brace on line 6 closes the class Welcome.
➢ Java is simple, reliable, dynamic, used in networking, secure, and free.
➢ Java works on different platforms such as Windows, Mac,LINUX, Raspberry and Pi.
➢ Line 5 in analyzing Main.java declares the Main class.
➢ The word “greet” in analyzing Main.java is users defined.
➢ Java is owned by Oracle, and more than 3 billion devices run Java.
➢ Java has a large demand in the present job market.
➢ In analyzing Main.java, brace on line 9 closed the main method and the brace on line 10
indicates the end of the scope of the class Main.
➢ In analyzing Main.java program execution starts at line 6.
➢ Anything BETWEEN /* and */ in analyzing Main.java is considered a comment.
➢ All relational operators are binary operators primarily because they indicate the
relationship among two operands.
➢ In conditional statements, use if statement to specify a block of code to be executed, if
a specified condition is true.
➢ If the same condition is false in a conditional statement, use else to specify a block of
code to be executed.
➢ The (‘\n’) represent a line break, that composed of backlash character (\) and a letter “n”
but it is interpreted by java as a single character.
➢ Java is a case sensitive that uses lowercase format in all keywords.
Running Blue J
➢ STEP 1: Run BlueJ by double-clicking the BlueJ icon on the desktop or running it from
Start> All Programs> BlueJ>. BlueJ for Windows OS and for Mac OS go to Launchpad>
BlueJ.
➢ STEP 2: Click Browse for specific java version… and browse to the bin directory within
your Java installation folder. Click on the file name java and click open.
➢ STEP 3: Click the Java version that appears and click Launch BlueJ.
➢ STEP 4: To create a new project, click Project > New Project.
➢ STEP 5: The New Project dialogue box will appear. In the File name field, input the name
of your project. Click Create.
➢ STEP 6: The BlueJ window will look similar to the following picture. Double-click this icon
to open README.TEXT.
➢ STEP 7: Type the appropriate information.
➢ STEP 8: After updating README.TEXT, click Class > Close.

9
ICT 10 - 1st Quarter Examination Reviewer
Credits: Hielden Martinez and Jaden Lopena
➢ STEP 9: Click Edit > New Class or press Ctrl+N at the same time.
➢ STEP 10: Type “Welcome” as the name of the new class then click Ok.

10

You might also like