1 / 18

Computer Programming2 Level 4

Computer Programming2 Level 4. Lecture 1 Basic Elements of Java Maria Abdalla Beshir. Approaches of Programming Methodologies. Q: Mention the approaches to programming design: structured approach. object-oriented approach. Q: Define each

sahirah
Télécharger la présentation

Computer Programming2 Level 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming2 Level 4 Lecture 1 Basic Elements of Java Maria AbdallaBeshir

  2. Approaches of Programming Methodologies Q: Mention the approaches to programming design: structured approach. object-oriented approach. Q: Define each Structured Programming[Top-Down Design] In structured design, a problem is divided into smaller subproblems. Each subproblem is solved, and the subproblem solutions are Integrated.

  3. Object-Oriented Design[OOD] The programmer identifies components called objects, which form the basis of the solution, and determines how these Objects interact with one another. In OOD, a program is a collection of interacting objects. • object consists of data and the operations on those data. Q: What is an object-Oriented Programming[OOP] A programming language that implements OOD

  4. Review: Basic Elements of Java • Comments • Single-line comments begin with // • Multiple-line comments are enclosed between /* and */. • The compiler ignores anything that appears between /* and */.or // • Special Symbols The Following are some of the special symbols: • + - * / [Mathematical symbols] • . ; ? , [Punctuation marks] • <= != == >= [Comparisons]

  5. Reserved Words • Reserved words are also called keywords. • The letters in a reserved word are always lowercase. • cannot be used for anything other than their intended use. Ex: int, float, double, char, void, public, static, throws, return • Identifiers • Are names of things, such as variables, constants, and methods, that appear in programs. • Identifier: A Java identifier consists of letters, digits, the underscore character (_), and the dollar sign ($). • must begin with a letter, underscore, or the dollar sign. Java is case sensitive.

  6. Data Types • Primitive Data Types Are the fundamental data types in Java There are of three categories : • Integral deals with integers, or numbers without a decimal part. They are of many types [char, byte, short, int, and long] • Floating –Point Data type that deals with decimal numbers. The default type of floating-point numbers is double. • Boolean Boolean, which is a data type that deals with logical values

  7. Char • The char data type can represent any key on your keyboard. • When using the char data type, you enclose each character represented within single quotation marks. EX: 'A', 'a', '0', '*', '+', '$', '&', ' ‘ • The class String • Is used to manipulate strings. • A string is a sequence of zero or more characters. • Strings in Java are enclosed in double quotation marks.

  8. A string containing no characters is called a null or empty string. • The operator + can be used to concatenate two strings Operators in Java • The arithmetic operators in Java are addition (+), subtraction (-), multiplication (*), division (/), and mod (%). • The mod operator, %, gives the remainder upon division.

  9. Variables • memory locations whose content may change during program execution • The syntax for declaring one variable or multiple variables is: • Q: Write the general syntax for: • Declaring a variable Datatype name; EX: int num1, num2; ASSIGNMENT STATEMENT • Use to put data in variables. • the value of the expression should match the data type of the variable. EX: • num1 = 4; • num2 = 4 * 5 - 11;

  10. The syntax for assignment • A variable is said to be initialized the first time a value is placed in the variable. Common ways to place data into a variable are: 1. Use an assignment statement. 2. Use input (read) statements Declaring and Initialize Variables initialize variables while they are being declared. • EX: int first = 13;

  11. Named Constants in Java • Named constant: A memory location whose content is not allowed to change during program execution. Q: Write the syntax to declare a named constant In syntax, the shading indicates the part of the definition that is optional. EX: final double CENTIMETERS_PER_INCH = 2.54;

  12. Creating a Java Application Program Q: Write a syntax of a class to create java application Program classMembers consists of the data members and methods (such as the method main).

  13. Q: Write a typical syntax of the method mainto create java application Program

  14. Output Statements • The standard output device is usually the monitor. Output syntax System. Out = standard output object Print , println = Methods EX: System.out.println(29 / 4); 7 2 System.out.println("Hello there."); Hello there. 3 System.out.println(12); 12 System.out.print("My name is James."

  15. Quick Review • A referencevariable is a variable that stores the address of a memory space. 2. In Java, all variables declared using a classare reference variables. 3. A reference variable does not directly store data in its memory space. It stores the address of the memory space where the actual data is stored. 4. Class objects are instances of that class. 5. Using the operator new to create a class object is called instantiating an object of that class.

  16. 6. To use a predefined method in a program, you need to know the name of the class containing the method (unless the class, such as the class String, is automatically imported) and the name of the package containing the class, and then you need to import the class into the program. In addition, you need to know the name of the method, the number of parameters the method takes, and the type of each parameter. You must also be aware of the method’s return type or, loosely speaking, what the method produces.

  17. 7. In Java, the dot (.) is called the member access operator. The dot separates the class name from the member, or method, name. Dot notation is also used when a reference variable of a class type accesses a member of that class. 8. The class String is used to process strings. 9. The assignment operator is defined for the class String. 10. The method substring of the class String returns a substring from another string. 11. The class String contains many other useful methods, such as: charAt,indexOf, concat, length, replace, toLowerCase, and toUpperCase. 12. You can use the method printf to format the output in a specific manner.

  18. 13. The method printf is available in Java 5.0 and its higher versions. 14. In a format specifier, using the option width you can also specify the number of columns to be used to output the value of an expression. The (default) output is right justified. 15. To force the output to be left justified, you use the format specifier flag. If the flag is set to '-', then the output of the result is left justified. 16. A numeric string consists of an integer or a decimal number with an optional minus sign.

More Related