Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

JAVA PROGRAMMING

Uploaded by

Isaac
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

JAVA PROGRAMMING

Uploaded by

Isaac
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

INTRODUCTION TO

PROGRAMMING WITH
PYTHON
• Computer programming is the process of designing and building an
executable computer program to accomplish a specific computing result or
to perform a specific task.

• A programming language is a computer language programmers use to


develop software programs, scripts, or other sets of instructions for
computers to execute.

• Examples of programming languages: Python, JavaScript, Java, C#, C, C++,


Go, R, Swift, PHP, Java etc.
JAVA
• Java is a popular programming language, created in 1995.
• It is owned by Oracle, and more than 3 billion devices run
Java.
• It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
Algorithm
• Algorithms describe a set of steps to solve a problem.

Why programmers use algorithms


1. To plan their approache to the problem and check if it makes sense
before they begin.
2. Choose the right way to solve a problem.
3. Describe their solution to others.
4. Keep a record of the logic of their solution.
Flowchart
• A flowchart is simply a graphical representation of steps to solve a
problem. It shows steps in sequential order and is widely used in
presenting the flow of algorithms, workflow or processes.

• Some Flowchart Symbols


Pseudocode
• PSEUDOCODE is a simple method of showing an algorithm, using
English-like words and mathematical operators that are set out to
look like a program.
.
Key Words
• Test data: is used to test the accuracy of an algorithm.

• Verification: is checking that data has been accurately copied onto the
computer or transferred from one part of a computer system to another.

• Validation: is the automated checking by a program that data is reasonable


before it is accepted into a computer system.
Common Data Types
• Integer (int) : It is the most common numeric data type used to store numbers without a
fractional component (-707, 0, 707).
• Floating Point (float): It is also a numeric data type used to store numbers that may have a
fractional component, like monetary values do (707.07, 0.7, 707.00).
• Please note that number is often used as a data type that includes both int and float types.
• Character (char): It is used to store a single letter, digit, punctuation mark, symbol, or blank
space.
• String (str or text): It is a sequence of characters and the most commonly used data type to
store text. Additionally, a string can also include digits and symbols, however, it is always
treated as text.
• A phone number is usually stored as a string (+1-999-666-3333) but can also be stored as an
integer (9996663333) .
• Boolean (bool): It represents the values true and false. When working with the boolean data
type, it is helpful to keep in mind that sometimes a boolean value is also represented as 0 (for
false) and 1 (for true).
• A LIBRARY ROUTINE is a set of programming instructions for a
given task that is already available for use. For example, the task
‘get time’ in the checking-for-the-alarmtime algorithm would
probably be readily available as a library routine.

• A SUB-ROUTINE is a set of programming instructions for a given


task that forms a subsystem, not the whole system. Sub-routines
written in high-level programming languages are called
‘procedures’ or ‘functions’ depending on how they are used.
Java Quick-start
• In Java, every application
begins with a class name,
and that class must match
the filename.
• Let's create our first Java
file, called Main.java,
which can be done in any
text editor (like Notepad).
• The file should contain a
"Hello World" message,
which is written with the
following code:
SYNTAX AND SEMANTICS
• Java Syntax is a basic of the language, all the main rules,
commands, constructions to write programs that the compiler and
computer “understands”. Every programming language has its
syntax as well as human language.
CONT.
• Semantics describes the processes a computer follows
when executing a program in that specific language. This can be
shown by describing the relationship between the input and output of
a program, or an explanation of how the program will be executed on
a certain platform, hence creating a model of computation.
Java Output
• Print Text
You learned from the previous chapter that you can use
the println() method to output values or print text in
Java:
CONT.
• Print Numbers

• System.out.println(3 + 7);
• System.out.println(3 * 7);
Java Comments
• Comments can be used to explain Java code, and to make it
more readable. It can also be used to prevent execution
when testing alternative code.
• Single-line comments start with two forward slashes (//).

• Multi-line comments start with /* and ends with */.


Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
•String - stores text, such as "Hello". String values are surrounded by
double quotes
•int - stores integers (whole numbers), without decimals, such as 123 or
-123
•float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
•char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
•boolean - stores values with two states: true or false
Declaring (Creating) Variables
• To create a variable, you must specify the type and
assign it a value:
CONT.
CONT.
Data Types
CONT.
• Primitive Data Types
Primitive data types specify the size and type of variable values.
They are the building blocks of data manipulation and cannot be
further divided into simpler data types.

• Non-Primitive Data Types


Non-primitive data types or reference data types refer to instances
or objects. They cannot store the value of a variable directly in
memory. They store a memory address of the variable. Unlike
primitive data types, which are defined by Java, non-primitive data
types are user-defined. They are created by programmers and can
be assigned with null. All non-primitive data types are of equal size.
CONT.
CONT.
Numbers
Boolean Data Types
Very often in programming, you will need a data type that can only have
one of two values, like:

•YES / NO
•ON / OFF
•TRUE / FALSE

For this, Java has a boolean data type, which can only take the
values true or false:
CONT.
Characters
The char data type is
used to store
a single character. The
character must be
surrounded by single
quotes, like 'A' or 'c':

Strings
The String data type is used
to store a sequence of
characters (text). String
values must be surrounded
by double quotes:
Type Casting
Type casting is when you assign a value of one primitive
data type to another type.
In Java, there are two types of casting:
•Widening Casting (automatically) - converting a smaller
type to a larger type size
byte -> short -> char -> int -> long -> float -> double

•Narrowing Casting (manually) - converting a larger type


to a smaller size type
double -> float -> long -> int -> char -> short -> byte
CONT.
CONT.
• Narrowing casting must be done manually by placing
the type in parentheses in front of the value:
Operators
CONT.

int sum1 = 100;


int sum2 = 50;
int sum2 = sum1 + sum2;
int sum3 = sum1 * sum2;
int sum3 = sum1 / sum2;
Assignment Operators
Comparison Operators
Logical Operators
Strings
A String variable contains a collection of characters surrounded by double
quotes:

String Length
String Methods

Finding a Character in a String


The indexOf() method returns the index (the position) of the first
occurrence of a specified text in a string (including whitespace):
String Concatenation
The + operator can be used between strings to combine
them. This is called concatenation:
Math
• The Java Math class has many methods that allows you
to perform mathematical tasks on numbers.
If ... Else (CONDITION STATEMENTS)
Java supports the usual logical conditions from mathematics:
•Less than: a < b
•Less than or equal to: a <= b
•Greater than: a > b
•Greater than or equal to: a >= b
•Equal to a == b
•Not Equal to: a != b

You can use these conditions to perform different actions for different
decisions.
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
The if Statement
The else
Statement
The else if
Statement
Switch
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:
CONT.
The default Keyword
• The default keyword specifies some code to run if there is
no case match:
Loops
• Loops can execute a block of code as long as a specified
condition is reached.
• Loops are handy because they save time, reduce errors,
and they make code more readable.
Types of Loops
While Loop
Cont.
The while loop loops through a block of code as
long as a specified condition is true:
Do/While Loop

The do/while loop is a


variant of the while loop.
This loop will execute the
code block once, before
checking if the condition
is true, then it will repeat
the loop as long as the
condition is true
Cont.
For Loop

When you know exactly how


many times you: want to
loop through a block of
code, use the for loop
instead of a while loop
Cont.
Cont.
Nested Loops

• It is also possible to place


a loop inside another
loop. This is called
a nested loop.
• The "inner loop" will be
executed one time for
each iteration of the
"outer loop":
Cont.
For Each Loop • There is also a "for-each" loop,
which is used exclusively to loop
through elements in an array:
Java Break
• You have already seen
the break statement used in
an earlier chapter of this
tutorial. It was used to "jump
out" of a switch statement.

• The break statement can


also be used to jump out of
a loop.

• This example stops the loop


when i is equal to 4:
Java Continue
• The continue statement
breaks one iteration (in the
loop), if a specified condition
occurs, and continues with
the next iteration in the
loop.

• This example skips the value


of 4:
Break and Continue in While Loop
• public is an access modifier. A variable, method, or class marked with the
public modifier can be accessed from anywhere in the program. There are four of
them in Java: public, private, protected and default (empty). We talk about them a
little bit later. For the first step it is better to make all your methods public.
• void is the return type of the method. Void means that it doesn't return any value.
• main represents the starting point of the program. This is the name of the
method.
• String[] args is a main method argument. For now it is enough to know that
almost every Java program has the main method, it starts the program and it
declares such as public static void main(String[] args)
• Static methods are ones to work with the class. Methods that use the static
keyword in their declaration can only work directly with local and static variables.
• https://www.computerhope.com/jargon/p/programming-language.ht
m
• https://en.wikipedia.org/wiki/Computer_programming

You might also like