مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 2 Elementary Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Introduction to Java Programming, 4E Y. Daniel Liang.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Elementary Programming 1. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Chapter 2 Elementary Programming. 2 Objectives  To write Java programs to perform simple calculations  To obtain input from the console using the.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Liang, Introduction to Java Programming1 Primitive Data Types and Operations Gang Qian Department of Computer Science University of Central Oklahoma.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
CS 201 Lecture 2: Elementary Programming Tarik Booker CS 201 California State University, Los Angeles.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Chapter 2 Elementary Programming
Topic 2 Elementary Programming
Unit 2 Elementary Programming
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
IDENTIFIERS CSC 111.
Chapter 2 Elementary Programming
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Chapter 2: Basic Elements of Java
Fundamentals 2.
Chapter 2: Java Fundamentals
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Chapter 2: Java Fundamentals
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 2 Primitive Data Types and Operations
Chapter 2 Elementary Programming
Chapter 2: Beginning to Program
Presentation transcript:

مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015 المحاضرة #4: الفصل الثاني «البرمجة الابتدائية» من كتاب Intro to Java Programming, Comprehensive Version, 10E م. محمد بسام أبوبكر

برنامج لحساب مساحة الدائرة allocate memory for radius public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } radius no value

برنامج لحساب مساحة الدائرة memory public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } radius no value area no value allocate memory for area

برنامج لحساب مساحة الدائرة assign 20 to radius public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } radius 20 area no value

برنامج لحساب مساحة الدائرة memory public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } radius 20 area 1256.636 compute area and assign it to variable area

برنامج لحساب مساحة الدائرة memory public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } radius 20 area 1256.636 print a message to the console

Identifiers المعرفات: الاسماء التي تظهر بالبرنامج 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. (See Appendix A, “Java Keywords,” for a list of reserved words). An identifier cannot be true, false, or null. An identifier can be of any length. استخدام المعرفات التالية صحيح: $2, ComputeArea, area, radius, and number أما استخدام المعرفات التالية فهو غير صحيح: 2N, d+4 تعتبر لغة جافا (case sensitive) أي انها حساسة لاستخدام الحروف الصغيرة والكبيرة فمثلا: المعرفات التالية تعتبر مختلفة: area, Area, AREA

Variables المتغيرات // Compute the first area radius = 1.0; area = radius * radius * 3.14159; System.out.println("The area is “ + area + " for radius "+radius); // Compute the second area radius = 2.0;

Declaring Variables تعريف المتغيرات متغير اسمه x ونوعه عدد صحيح int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; متغير اسمه radius ونوعه عدد عشري متغير اسمه a ونوعه رمز باللون الازرق نوع المتغير باللون الاحمر اسم المتغير

Assignment Statements اعطاء قيمة x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; int double char

Declaring and Initializing in One Step تعريف واعطاء قيمة في خطوة واحدة int x = 1; double d = 1.4;

Named Constants الثوابت final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3; PI له قيمة ثابتة لا تتغير في البرنامج SIZE له قيمة ثابتة لا تتغير في البرنامج القيم الثابتة في البرنامج يتم استخدام كلمة final قبل نوع المتغير

Naming Conventions التسميات المتعارف عليها Choose meaningful and descriptive names. Variables and method names: Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea. 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 constant PI and MAX_VALUE

Numerical Data Types أنواع البيانات الرقمية

Numeric Operators

Integer Division +, -, *, /, and % 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division)

Integer Literals An integer literal can be assigned to an integer variable as long as it can fit into the variable. A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable of the byte type. An integer literal is assumed to be of the int type, whose value is between -231 (-2147483648) to 231–1 (2147483647). To denote an integer literal of the long type, append it with the letter L or l. L is preferred because l (lowercase L) can easily be confused with 1 (the digit one).

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. For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number.

double vs. float double float The double type values are more accurate than the float type values. For example, System.out.println("1.0 / 3.0 is " + 1.0 / 3.0); double System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F); float

Arithmetic Expressions العمليات أو التعبيرات الحسابية وكيفية كتابتها في برنامج جافا is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

How to Evaluate an Expression ترتيب تنفيذ العمليات الحسابية

Augmented Assignment Operators

Increment and Decrement Operators

Increment and Decrement Operators, cont.

1. Create a Scanner object Reading Input from the Console ادخال قيم المتغيرات عن طريق لوحة المفاتيح 1. Create a Scanner object Scanner input = new Scanner(System.in); 2. Use the method nextDouble() to obtain to a double value. For example, System.out.print("Enter a double value: "); double d = input.nextDouble();

Reading Numbers from the Keyboard Scanner input = new Scanner(System.in); int value = input.nextInt();

ملاحظات

END