Chap1 2
Chap1 2
• Programming is the process of encoding your algorithm into a programming language, so that
it can then be executed by a computer.
• A Computer is not intelligent ( cannot analyze a problem and come up with a solution ) .
•Test \ اﻻﺧﺗﺑﺎر
◦ Have the computer follow the instructions.( ) ﻧطﻠب ﻣن اﻟﻛﻣﺑﯾوﺗر اﺗﺑﺎع اﻟﻣﻌﻠوﻣﺎت
◦ Then manually check the results. ( ) ﺛم ﻧﺗﺣﻘق ﯾدوﯾًﺎ ﻣن اﻟﻧﺗﺎﺋﺞ
◦ If you find errors, analyze the program and the algorithm to determine the source of the
errors, and then make corrections.
( ) اذا وﺟد أﺧطﺎء ﻓﻘم ﺑﺗﺣﻠﯾل اﻟﺑرﻧﺎﻣﺞ واﻟﺧوارزﻣﯾﺔ ﻟﺗﺣدﯾد ﻣﺻدر اﻷﺧطﺎء ﺛم ﻗم ﺑﺗﺻﺣﯾﺢ اﻷﺧطﺎء
• Maintenance اﻟﺻﯾﺎﻧﺔ/
requires modification of the program to meet changing requirements or to correct any
errors that show up while using it.
( ) اﻟﺗﻌدﯾل ﻋﻠﻰ اﻟﺑرﻧﺎﻣﺞ ﻟﺗﻠﺑﯾﺔ اﻟﻣﺗطﻠﺑﺎت اﻟﻣﺗﻐﯾرة أو ﻟﺗﺻﺣﯾﺢ أي أﺧطﺎء ﺗظﮭر اﺛﻧﺎء اﻻﺳﺗﺧدام
Steps in Program Development
1. Define the problem into three separate components:( ﻣﻛوﻧﺎت ﻣﻧﻔﺻﻠﺔ٣ ) ﻧﻌرف اﻟﻣﺷﻛﻠﺔ إﻟﻰ
• Inputsاﻟﻣدﺧﻼت
• Processing steps to produce required outputs. ﺧطوات اﻟﻣﻌﺎﻟﺟﺔ اﻟﺗﻲ ﺗﻌطﻲ اﻟﻧﺗﯾﺟﺔ اﻟﻣطﻠوﺑﺔ
• Outputsاﻟﻣﺧرﺟﺎت
3. Develop the outline into an algorithm.( ) ﻧﺣول ﻣﺧطط اﻟﺣل إﻟﻰ ﺧوارزﻣﯾﺔ
What is an algorithm?
◦ A step-by-step series of instructions in order to perform a specific
task.
An algorithm must:
▪ Be lucid (clear), precise and unambiguous.
▪ Give the correct solution in all cases, and eventually end.
• Flowchart A graphical representation of the sequence of operations in an
information system or program.
Program flowcharts
• show the sequence of instructions in a single program or subroutine.
• shows logic of an algorithm
• emphasizes individual steps and their
interconnection
Programming Languages
The high-level languages are English-like and easy to learn and program.
* For example, the following is a high-level language statement that computes the area of a circle with radius 5:
area = 5 * 5 * 3.1415;
• A program written in a high-level language is called a source program or source code.
• a program tool called an interpreter or a compiler is used to convert a source program
into machine code.
An interpreter reads one statement from A compiler translates the entire source code into
the source code, translates it to the a machine-code file, and the machine-code file is
machine code or virtual machine code, . then executed
.and then executes it right away ) ﯾﺗرﺟم اﻟﻣﺗرﺟم ﻛود اﻟﻠﻐﺔ ﻋﺎﻟﯾﺔ اﻟﻣﺳﺗوى ﺑﺎﻟﻛﺎﻣل إﻟﻰ ﻣﻠف ﻛود
ً
ﺟﻢ ﺳﻄﺮ او ﺟﻤﻠﺔ واﺣﺪا ﻓﻘﻂ ﻣﻦ اﻟﻠﻐﺔBC) ;ﻘﺮأ اﻟﻤ ( ﺛم ﯾﺗم ﺗﻧﻔﯾذ ﻣﻠف ﻛود اﻵﻟﺔ، ﻟﻐﺔ اﻵﻟﺔ
f ﺛﻢ ﻳﻨﻔﺬە ﻋ،ﺟﻤﻪ إ] ﻟﻐﺔ اﻻﻟﻪBCZﺔ اﻟﻤﺴﺘﻮى وUﻋﺎﻟ
( اﻟﻔﻮر
JAVA
* Java enables users to develop and deploy applications on the Internet for servers, desktop
computers, and small hand-held devices.
ﻢH) ﺗﻢ ﺗﺼﻤJava ﻃﺮازOﺎ ﻋHً ﺟﺰﺋC(ghﻞ ﻛﺒcﺸa ﻄﻬﺎ وﺗﺤﺴ_ﻨﻬﺎHﺴZﻦ ﺗﻢ ﺗX وﻟ، ++
Some people refer to Java as "C++--" because it is like C++ but with more functionality and fewer
negative aspects.
3ﺸ(' )ﻌﺾ اﻷﺷﺨﺎص إ% )Java" )ﺎﺳﻢC " ﻷﻧﻬﺎ ;ﺸ<ﻪ- ++ C( ﺔ أﻗﻞO' وﺟﻮاﻧﺐ ﺳﻠﺒIH وﻟ@ﻦ ﻣﻊ وﻇﺎﺋﻒ أ++
2 - Java Is Object-Oriented
• Object-oriented programming (OOP) is a popular programming approach that is replacing
traditional procedural programming techniques.
• Java was designed from the start to be object-oriented.
• Object- oriented programming provides great flexibility, modularity, clarity, and reusability
through encapsulation, inheritance, and polymorphism.
3 - Java Is Distributed
• Distributed computing involves several computers working together on a network.
• Java is designed to make distributed computing easy.
• Since networking capability is inherently integrated into Java, writing network programs is like
sending and receiving data to and from a file.
4 - Java Is Interpreted
• The programs are compiled into the Java Virtual Machine code called bytecode.
• The bytecode is machine- independent and can run on any machine that has a Java interpreter,
which is part of the Java Virtual Machine (JVM).
5 - Java Is Robust
• Java compilers can detect many problems that would first show up at execution time in other
languages.
• Java has eliminated certain types of error- prone programming constructs found in other
languages.
• Java has a runtime exception-handling feature to provide programming support for robustness.
6 - Java Is Secure
• Java implements several security mechanisms to protect your system against harm caused by
stray programs.
7 - Java Is Architecture-Neutral
• Write once, run anywhere
• With a Java Virtual Machine (JVM), you can write one program that will run on any platform.
8 - Java Is Portable
• They can be run on any platform without being recompiled.
9 - Java's Performance
• They can be run on any platform without being recompiled.
10 - Java Is Multithreaded
11 - Java Is Dynamic
• Java was designed to adapt to an evolving environment.
JDK Editions \ ( ) ﺣزم ﺗطوﯾر ااﻟﺟﺎﻓﺎ
Appropriate Comments
1 - Include a summary at the beginning of the program to explain what the program does, its
key features, its supporting data structures, and any unique techniques it uses.
وأي ﺗﻘﻧﯾﺎت، وھﯾﺎﻛل اﻟﺑﯾﺎﻧﺎت اﻟداﻋﻣﺔ، وﻣﯾزاﺗﮫ اﻟرﺋﯾﺳﯾﺔ، ) ﻗم ﺑﺗﺿﻣﯾن ﻣﻠﺧص ﻓﻲ ﺑداﯾﺔ اﻟﺑرﻧﺎﻣﺞ ﻟﺷرح ﻣﺎ ﯾﻔﻌﻠﮫ اﻟﺑرﻧﺎﻣﺞ
( ﻓرﯾدة ﯾﺳﺗﺧدﻣﮭﺎ
2 - Include your name, class section, instructor, date, and a brief description at the beginning of
the program.
Naming Conventions
1 - Choose meaningful and descriptive names.
2 - Class names:
– Capitalize the first letter of each word in the name.
For example, the class name ComputeExpression.
Special Symbols
Programming Errors
• Syntax Error
• Runtime Errors
– Causes the program to terminate in an
abnormal way
– Known as “crashing” or “my program crashed”
– Often caused by input mistakes, where the user enters a value the program cannot handle
– Another example: divide by zero
! Logic Errors
– Produces incorrect result
– Program does not run the way we intended
– Usually the result of logical mistakes
– In fact, the program “works”, but the output is wrong due to our logical mistake.
– These errors are harder to detect.
! Common Errors
– Missing a semicolon
– Missing a closing brace, missing a semicolon, missing quotation marks for strings, and
misspelling names are common errors for new programmers.
– Misspelling Names:
– – main is misspelled as Main
– String is misspelled as string
! Identifiers are the names that identify elements of your program, such as classes, methods, and
variables.
! An identifier is a sequence of characters that consist of letters, digits, underscores (_), and
dollar signs ($). ( ($)( و-) ) اﻟﻣﻌرف ﻣﻣﻛن ﯾﺣﺗوي ﻋﻠﻰ ﺣروف وأرﻗﺎم و
! An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with
a digit. ( ( وﻻ ﯾﻣﻛن ان ﯾﺑدا ﺑرﻗم$) ( او-) ) ﻻزم ﯾﺑدا ﺑﺣرف او
An identifier cannot be a reserved word ( ) ﻻﯾﻣﻛن ان ﯾﺣﺗوي اﻟﻣﻌرف ﻋﻠﻰ ﻛﻠﻣﺎت ﻣﺣﺟوزةand cannot be
true, false, or null.
! An identifier can be of any length.
! Examples of legal identifiers:
area, radius, ComputeArea, $2, average
! Examples of illegal identifiers( syntax error):
2A and d+4
! Note: Java is case sensitive
area, Area, and AREA all are different identifiers
7 7
byte –2 to 2 – 1 (-128 to 127) 8-bit signed
15 15
short –2 to 2 – 1 (-32768 to 32767) 16-bit signed
Positive range:
4.9E-324 to 1.7976931348623157E+308
! Examples
1 -Integer Literals
! An integer literal is assumed to be of the int type.
! To denote an integer literal of the long type, append it with the letter L or l.
ﯾﻔﺿل اﺳﺗﺧدام ﺣرف ال) ( اﻟﻛﺎﺑﯾﺗل ﻋﺷﺎن ﻣﺎن ﻧﻠﺧﺑط ﺑﯾن ال ) ( اﻟﺳﻣول ورﻗم واﺣد
2- Floating-Point Literals
! Floating-point literals are written with a decimal point.
! By default, a floating-point literal is treated as a double type value
For example, 5.0 is considered a double value, not a float value.
! You can make a number a float by appending the letter f or F, and make a number a double by
appending the letter d or D.
The double type values are more accurate than the float type values.
Declaring Variables
! Variables are used to represent values that may be changed in the program.
! If variables are of the dame data type, they can be declared together:
datatype var1, var2, var3,…, varn;
Example:
int i, j, k;
! You can also use shorthand form to declare and initialize variables of the same type together:
Example:
int i = 62, j = 78, k = 35;
! You can also use the same variable on both sides of the assignment statement
! Example:
ھﺬاx = x + 1; ﻧﻔﺲ ھﺬاa++; او++a;
a+=1; ﻧﻔﺲ ذي
! The variable’s data type is the kind of data that you can store in that particular variable.
1 public class ComputeArea {
2 public static void main(String[] args) {
3 double radius;
4 double area;
5
6 // Assign a radius
7 radius = 20;
8
9 // Compute area
10 area = radius * radius * 3.14159;
11
12 // Display results
13 System.out.println("The area for the circle of radius " +
14 radius + " is " + area);
15 }
16 }
-The following table shows the value in memory for the variables area and radius
as the program is executed.
• To fix the error, break the string into separate substrings and use
concatenation (+) to combine:
Method Description
! Before you can use the Scanner class, you must import it!
! The Scanner class is in the java.util package.
! Notice that we do this import before we start coding our actual class.
– There are two types of import statements:
" Specific import: specifies a single class that should be imported
– Example:
import java.util.Scanner;
" Wildcard import: imports all the classes in a package by using the asterisk as the wildcard.
– Example:
import java.util.*;
Naming Conventions
! Class names:
– Capitalize the first letter of each word in the name.
– For example, the class name ComputeArea.
! Constants:
– Capitalize all letters in constants, and use underscores to connect words.
– For example, the constants PI and MAX_VALUE
Numeric Operators
Name Meaning Example Result
+ Addition 34 + 1 35
% Remainder 20 % 3 2
Remainder Operator
The % operator is known as the remainder operator, or also as the modulo operator
" 7%3=1
" 12 % 4 = 0
" 20 % 13 = 7
" an even number % 2 is always 0
" An odd number % 2 is always 1
Example:
" If today is Saturday, it will be Saturday again in 7 days. Suppose you and your friends
will meet in 10 days. What day is it in 10 days?
" Let us assume Sunday is the 1st day of the week.
" We can find that in 10 days, the day will be Tuesday by using the following equation:
Saturday is the 7th day in a week
A week has 7 days
(7 + 10) % 7 is 3
The 3rd day in a week is Tuesday
After 10 days
! Calculations involving floating-point numbers are approximated because these numbers are not stored
with complete accuracy.
! For example:
System.out.println(1.0-0.1-0.1-0.1-0.1-0.1);
Exponent Operations
System.out.println(Math.pow(2, 3));
// Displays 8.0 اﻟﻨﺎﺗﺞ ﯾﻜﻮن رﻗﻢ دﺑﻞ
Arithmetic Expressions
! Java expressions are written the same way as normal arithmetic expressions.
! Example:
3 + 4 x 10( y - 5)(a + b + c) 4 9+ x
- + 9( + )
5 x x y
is translated into
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
Augmented Assignment Operat
! we use the current value of a variable, we modify it, and then save it back to the same
variable.
Example:
count = count + 1;
" Java allows you to combine this addition and assignment into one operator, which is called the
augmented assignment operator.
Example:
count += 1;
! The augmented assignment operator is performed last after all the other operators in the
expression are evaluate
Example:
x /= 4 + 5.5 * 1.5;
is same as
x = x / (4 + 5.5 * 1.5);
§ Examples:
int i = 3, j = 3;
i++; // i becomes 4
j--; // j becomes 2
§ Examples:
int i = 3, j = 3;
++i; // i becomes 4
--j; // j becomes 2
If the statement is ONLY doing increment or decrement, the effect of j++ and ++j is the same.
! Another example:
– Consider the following code:
double x = 1.0;
double y = 5.0;
double z = x-- + (++y);
– What is the value of x, y, and z after are three lines are executed?
" x becomes 0.0
" y becomes 6.0
" z becomes 7.0
Avoid using these operators in expressions that modify multiple variables, or the same variable
for multiple times such as this:
int k = ++i + i;
Numeric Type Conversions
" If you add an integer with a floating-point number, Java automatically coverts the int to a
floating point value.
Example:
" 3 * 4.5 is the same as
" 3.0 * 4.5
Example:
" You can save an int into a double, because the double is much wider (larger) than the int
int x = 4;
double y;
y = x; //y =4,0
" This is allowed, because x can easily “fit” into y.
you cannot assign a value to a variable of a data type with a smaller range of values.
§ Unless you use type casting
Casting is an operation that coverts a value of one data type into a value of another data type
" Casting a type with a small range to a type with a larger range
is known as “widening a type”
" Casting a type with a large range to a type from a smaller range
is known as “narrowing a type”
Java will automatically widen a type, but you must request a narrowing explicitly
! Casting:
– Example:
System.out.println((double)1/2);
" 0.5 gets printed.
" Why? Because 1 is cast into 1.0. Then 1.0 is divided by 2.
– Example:
System.out.println(1 / 2);
" Be careful!
" Here, 0 (zero) gets printed. Why? 1 and 2 are both inters and the result should be an integer.
int sum = 0;
sum += 4.5; // sum becomes 4 after this statement