0% found this document useful (0 votes)
12 views

Computer Programming 1

College Subject Notes

Uploaded by

Angel Jade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Computer Programming 1

College Subject Notes

Uploaded by

Angel Jade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

COMPUTER PROGRAMMING 1

AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

TYPES OF PROGRAMMING

COMPUTER PROGRAM – sequence of statements


intended to accomplish a certain task. It is a set of
instruction for a computer to follow.

PROGRAMMING – process of planning and creating


a program.
PSEUDOCODE
LOW-LEVEL LANGUAGES – languages that deal with
a computer’s hardware components. Method of describing computer algorithms using a
- MACHINE LANGUAGE – language that the combination of natural language and programming
computer can directly understand. It is language.
written in binary codes (0, 1) - Symbols are used for the common
- ASSEMBLY LANGUAGE – the symbolic form of operations: arithmetic (+, -, *, /), assignment
machine language that is easier for people (=), comparison (=, ≠, >, <, ≤, ≥), and logical
to read. (and, or)
ASSEMBLER – a program that translates - Certain keywords can be used as a
assembly language into machine language. command, e.g., PRINT, WRITE, READ, SET, GO
HIGH-LEVEL LANGUAGES – languages that use TO, etc.
natural languages, and has its own syntax. - Indentions is used to indicate branches and
loops of instructions
- SYNTAX – rules of the language
- COMMANDS – program statements that FLOWCHART
carry out tasks that the program has to
Visual Representation of an algorithm. It contains
perform
shapes describing how an algorithm or program
- COMPILER – translates a high-level language
operates.
into a low-level language
- INTERPRETER – acts as a compiler, but
translates one program statements at a time
- SYNTAX ERRORS – error that is encountered
during the process of translation
- LOGICAL ERRORS – when the syntax is
correct, but the output is not
- DEBUGGING – process of locating and
correcting the errors

PROGRAMMING CYCLE

ALGORITHM – problem-solving technique used in


solving programming problems. There are two
commonly used tools in representing an algorithm:
pseudocode and flowchart PROGRAMMING METHODOLOGIES
STEPS:
PROGRAMMING METHODOLOGIES – approach to
- PROBLEM ANALYSIS – analyze the problem analyzing such complex problems by planning
- ALGORITHM DESIGN – design an algorithm software development and controlling the
- CODING – implement the algorithm development process.
- EXECUTION – verify that the algorithm works
- PROCEDURAL PROGRAMMING – problem is
broken down into functions that perform one
task each.
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

- OBJECT-ORIENTED PROGRAMMING – public static void main(String[] args) { }


programs are organized around objects
public – access modifier
rather than actions, and data rather than
logic. static – means accessible and usable

void – means does not return any value when it is


called

main – name of the method

String[] args – represents the type of argument

IDENTIFIERS

An identifier is a name of a program component.


JAVA ENVIRONMENT
- It must begin with a letter of the English
JAVA – a high-level programming language that alphabet, underscore, or a dollar sign. It
was developed by Sun Microsystems. It was created cannot begin with a digit.
in May 1995 by James Gosling. It was later on passed - Identifiers can only have combinations of
to Java Oracle on January 27, 2010. latter, digits, underscores, or dollar sign. White
spaces are not allowed.
JAVA ENVIRONMENT MACHINE (JVM) – environment - Cannot be a reserved keyword.
that translates Java bytecode into machine
language RESERVED WORDS

SOURCE CODE – programming statements written in Words that have special predefined meaning in
a high-level language. Java language.

DEVELOPMENT ENVIRONMENT / INTEGRATED


DEVELOPMENT ENVIRONMENT (IDE) – a set of tools
that help write programs easily, such as NetBeans.

TYPES OF JAVA PROGRAM

APPLICATION – a stand-alone program.

- CONSOLE APPLICATION – supports character


output to a computer screen
- WINDOWED APPLICATION – creates a GUI SAVING, COMPILING, AND DEBUGGING SYNTAX
with elements such as menus, buttons, etc. ERRORS

APPLET – program embedded in a webpage. JAVA DEVELOPMENT KIT (JDK) – includes a complete
set of JRE tools for developing, debugging, and
SIMPLE JAVA PROGRAM
monitoring Java Applications.
public class FirstJavaProgram { JAVA RUNTIME ENVIRONMENT (JRE) – contains
public static void main(String[] args) { everything required to run Java Applications on a
system.
System.out.println(“hello world”);
SYNTAX ERRORS – invalid statements that the Java
} compiler might encounter.
} LOGICAL ERRORS – the program produces wrong
result even if the syntax is correct.
MAIN METHOD – In Java, execution always begins
with the main method.
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

COMMENTS FINAL - a Java reserved word used to specify that the


value stored in a variable is fixed and cannot be
COMMENTS are used to explain the details in a changed.
program. Comments are ignored by the compiler,
meaning they are not executed when the Java EXAMPLE:
programs run. final (data_type) variable_name [= assigned value];
SINGLE-LINE COMMENTS – comments that begin with final int age = 14;
// and can be placed anywhere in the line.
TYPECASTING
MULTIPLE-LINE COMMENTS – these are enclosed
between /* and */ TYPE CASTING - refers to converting a value from a
specific type to a variable of another type (booleans
IMPORT STATEMENT
cannot be converted to numeric types).
PACKAGES – collections of related classes that have WIDENING CONVERSION (IMPLICIT CASTING) – the
been grouped together into a folder. conversion of the lower precision data type to a
IMPORT – this is a reserved keyword in Java used to value of a higher precision data type.
access the classes in a package.

- Importing a package member – import a NARROWING CONVERSION (EXPLICIT CASTING) – the


specific class into the current file. conversion of a higher precision data type into a
EXAMPLE: import java.util.(class name); value of a lower precision data type. The conversion
- Importing an entire package – to import all could run through the use of a CAST OPERATOR.
the classes contained in a particular
package.
EXAMPLE: import java.util.*;
EXAMPLE:
PRIMITIVE DATA TYPES

DATA TYPE - used to specify the set of values and their


operations.

PRIMITIVE DATA TYPES - these are pre-defined data OPERATORS


types by the language and named by a keyword.
OPERATORS - these are specific symbols in a
programming language.

OPERAND - a value used on either side of an operator

ARITHMETIC OPERATORS - used to perform basic


mathematical operations on numerical values.

CONSTANTS AND VARIABLES

VARIABLE - an identifier, a name for a memory


location that stores a specific value, such as
The operators (/) and (%) have special
numbers and letters.
considerations.
EXAMPLE:
FLOATING-POINT DIVISION - occurs when either or
(data_type) variable_name [= assigned value]; int both of the operands are floating-point values and
age = 14; the result is a floating-point value.

CONSTANT - a memory location whose value cannot EXAMPLE:


be changed during program execution.
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

int x = 11; double y = 3; ORDER OF PRECEDENCE


System.out.println(x / y); // the result is 3.66 ORDER OF PRECEDENCE - a collection of rules that
INTEGER DIVISION - occurs when both of the specifies which operations need to be performed in
operands are integers. Any factorial part of the result an expression.
can be lost.

EXAMPLE:

int x = 11; int y = 3;

System.out.println(x / y); // the result is 3

RELATIONAL OPERATORS - used to evaluate the


relation between the operands and generate a
decision on that base, typically return a boolean
value.

EXPRESSIONS

EXPRESSIONS - a construct made up of variables and


operators that evaluates to a single value.

EXAMPLE: EXAMPLES: 11 + 13, x = 12

boolean b = 10 > 2; // statement 10 > 2 is true ARITHMETIC EXPRESSIONS

LOGICAL OPERATORS - return a boolean value based ARTITHMETIC EXPRESSIONS - an expression that
on the boolean result of the given expressions. It is returns a numeric value.
always evaluated from left to right.
INTEGRAL EXPRESSSION - if all operands in an
expression are integers and the expression returns an
integer type value.

EXAMPLE:

x = 11 + 3 * 24 - 5 / 4; //the result is 82 (integer)


EXAMPLE:
FLOATING-POINT EXPRESSION - if all operands in an
( 4 > 2 ) && (10 < 20) expression are floating-point numbers, and the
expression returns a floating-point value.
True && true // therefore, the result is true.
EXAMPLE:
ASSIGNMENT OPERATORS - used to assign values
to a variable. y = 2.8 * 17.5 - 1.40; // the result is 47.6 (floating-
point)

MIXED EXPRESSIONS

MIXED EXPRESSION - an expression that has


operands of different data types.
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

LOGICAL EXPRESSIONS

LOGICAL EXPRESSION - an expression that returns a


boolean value when evaluated, usually consists of
logical and relational operators.

INPUT

SCANNER - a class in java.util package that is used


to read data from the standard input device and
store data into variables.

You can use the methods of Scanner using the


following statements: import java.util.Scanner; //or
import java.util.*;

Then create an object of the Scanner class and


associate it with the standard input device using:

Scanner (identifier) = new Scanner(System.in);

TOKEN - each retrieved value that is a set of


characters separated from the next set of OUTPUT
whitespace.
EXAMPLE OF OUTPUT STATEMENTS:

THE print() METHOD - displays an output, and the


insertion point stays in the current line.

THE println() METHOD - moves the insertion point


to the following line after the output is displayed.

EXAMPLE:

THE printf() METHOD - used to create an output in a


specific format. This method is used to add
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

formatting instructions before displaying it on the ONE WAY SELECTION


display screen.
In Java, one-way selections are incorporated using
EXAMPLE: the if statement. The if statement tells the program to
execute a certain section of a statement only if a
particular condition evaluates to true.

EXAMPLE:

TWO WAY SELECTION

The two-way selections are used to choose between


two (2) alternatives. The if…else statement is used to
implement two-way selections in Java. else is a
reserved word, which executes when the given
expression of if statement evaluates to false. In a
two-way selection, if the given expression evaluates
EXAMPLE: to true, then statement 1 executes. If the expression
evaluates to false, statement 2 executes.

EXAMPLE:

COMPOUND STATEMENTS – statements grouped


together within curly braces.

EXAMPLE:

SELECTION STRUCTURE

CONTROL STRUCTURE – is a statement that is used to MULTIPLE SELECTIONS (NESTED IF) – when one (1)
control the flow of a program. There are two (2) control statement, either selection or repetition, is
control structures: located within another, it is said to be nested. The if
- In a SELECTION STRUCTURE, the program statement can be nested inside another if statement
executes particular statements depending or an if…else statement nested within the if
on the given condition. This alters the flow of statement. A nested if statement allows a program
program execution by making a selection or to perform multiple selections.
choice. EXAMPLE:
- In a REPETITION STRUCTURE, the program
repeats particular statements a certain
number of times, depending on the given
condition. This alters the flow of program
execution by the repetition of one (1) or
more statements.
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

statement. (Note: The break statement can


only terminate the process of an enclosing
switch statement or any repetition structures
in which it appears.)
- Not every case needs to contain a break. If
no break appears, the flow of control will fall
through to subsequent cases until a break is
reached.
- A switch statement may or may not have the
default statement, which must appear at the
end of the switch. The default statement is
used for performing a task when none of the
cases is true. No break is needed in the
default statement.

SHORT-CIRCUIT EVALUATION - – is a process in which EXAMPLE:


the computer evaluates a logical expression from
left to right and stops as soon as the value of the
expression is determined. The logical AND (&&) and
OR (||) are short-circuit operators.

EXAMPLES:

SWITCH STATEMENT - allows a single variable to be


REPETITION STRUCTURE
tested for equality against a list of values and,
depending on its value, executes a certain block of WHILE LOOP – repeats a block of statements while a
statements. given condition evaluates to true.
In Java, switch, case, break, and default are all
reserved words. The following rules are applied to a
switch statement:
- The expression is a loop condition enclosed in
- The variable used in a switch statement can parentheses
only be type: int, byte, short, char, and String. - The statements enclosed in curly braces is
- There can be any number of case called the loop body
statements within a switch. Each case is - A loop that continues to execute endlessly is
followed by the value to be compared to called an infinite loop
and a colon.
- The value in the case statement must be the EXAMPLE:
same data type as the variable in the switch
statement.
- When the variable being switched on is
equal to a case, then the statements
following that case will execute until a break - In the example, the variable, num is a loop
statement is reached. control variable, a variable whose value
- When a break statement is reached, the determines whether loop execution
switch terminates, and the flow control jumps continues. Within the loop body, the
to the next line following the switch statement num++; is used to update the loop
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

control variable until the loop condition CONTINUE STATEMENT – causes the loop to skip the
evaluates to false. remainder of its body and immediately reevaluate
its condition, and proceeds with the next iteration of
FOR LOOP – executes a sequence of statements
the loop.
multiple times and abbreviates the code that
manages the loop variable. - In a for loop, the continue statement causes
the control of the loop to immediately jump
to the update statement, and then the loop
condition is evaluated.
- The initialization indicates the starting value - In a while or do…while loop, using the
for the loop control variable, the condition is continue statement makes the control of the
the loop condition that controls the loop loop immediately jumps to the loop
entry, and the update is the expression that condition.
alters the loop control variable.
EXAMPLE:
EXAMPLE:

DO… WHILE LOOP – executes the loop body first


before evaluating the expression.

- The expression appears at the end of the NESTED CONTROL STRUCTURES


loop, and the statements in the loop are
executed once before the expression is NESTED CONTROL STRUCTURES – are control
evaluated. If the expression evaluates to statements that are placed within another.
true, the control jumps back up to do
EXAMPLE:
statement, and the loop body executes
again. This process repeats until the
expression evaluates to false.

EXAMPLE:

BREAK AND CONTINUE STATEMENTS

BREAK STATEMENT – terminates the loop or switch


statement and transfers the flow of the program to
the statements following the loop or switch.

EXAMPLE:

STRINGS
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

STRINGS – class from the java.lang package to


create and manipulate strings.

String is a sequence of characters. Every character


in a string has a specific position in the string, and the
position of the first character starts at index 0. The
length of a string is the number of characters in it.

CREATING STRINGS

A String can be constructed by either of the


following:

- directly assigning a string literal to a String


object; or
- using the new keyword and String
constructor to create a String object.

ARRAYS

ARRAY – a collection or sequence of a fixed number


of variables called elements, wherein all the
elements are of the same data type.
STRING METHODS
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

ONE-DIMENTIONAL ARRAY – an array in which all the


elements are arranged in a list form. The general
form to declare a one-dimensional array is:
dataType[] arrayName = new dataType[arraySize];.

FOREACH LOOP – used because all the elements in


an array are of the same type, and the size of the
array is known.

EXAMPLE:
MULTIDIMENTIONAL ARRAY - arrays where the
elements are arrays themselves.

TWO-DIMENTIONAL ARRAY - a collection of a fixed


number of elements arranged in rows and columns,
wherein all the elements are of the same data type.

EXAMPLE:

ARRAY CLASS - several methods for performing array


manipulations in the Arrays class from the java.util
package. This class contains a large number of static
help methods for sorting and searching arrays,
comparing arrays, and filling array elements.

PRE-DEFINED FUNCTIONS

FUNCTIONS – a block of statements that are grouped


together to perform an operation.
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

PRE-DEFINED FUNCTIONS – functions that are already


written and provided by the programming
language.

USER-DEFINED FUNCTIONS – functions that are


created by programmers.

CLASS LIBRARIES – pre-defined functions are


organized as a collection of classes.

USER-DEFINED FUNCTIONS

TWO CATEGORIES:

- VALUE-RETURNING FUNCTIONS – functions


that return a value of a specific data type
using the return statement. These functions
are used to save the value for further
calculations.
- VOID FUNCTIONS – functions that do not
return a value and not using the return
statement.

PARAMETER PASSING
COMPUTER PROGRAMMING 1
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

RECURSIVE DEFINITION

RECURSION – the process of solving a problem by


reducing it to successively smaller versions of itself.
Java uses this technique, in which a method or
function calls itself to solve some problem.

RECURSION VERSUS ITERATION

ITERATIVE METHOD – a method that implements


iteration.

PROBLEM SOLVING USING RECURSION

You might also like