1 / 144

Structure Programming Programming in Java

Structure Programming Programming in Java. Instructed and enhanced by Dr. Safia Abbas Computer Science Department Faculty of Computer and Information Sciences Ain Shams University Prepared By Dr. safia abbas Computer Science Department Faculty of Computer and Information Sciences

damita
Télécharger la présentation

Structure Programming Programming in Java

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. Structure ProgrammingProgramming in Java Instructed and enhanced by Dr. Safia Abbas Computer Science Department Faculty of Computer and Information Sciences Ain Shams University Prepared By Dr. safia abbas Computer Science Department Faculty of Computer and Information Sciences Ain Shams University

  2. The Course • Instructor: Dr. safia abbas • Text Book: • Paul Deitel and Harvey Deitel. “Java-How to Program. 9th edition, Pearson,2013.”

  3. Other (Online) Resources • How to Think Like a Computer Scientist • http://greenteapress.com/thinkapjava/thinkapjava.pdf • Introduction to Programming Using Java, 5th Edition • http://math.hws.edu/javanotes/ • Thinking in Java, 3rd Edition • http://nyll.com/ebooks/index.html • Sun Java Tutorial http://download.oracle.com/javase/tutorial/java/ • The Java API • http://download.oracle.com/javase/1.5.0/docs/api/

  4. Course Contents • Introduction. • Variables, types and expressions. • Functions and procedural abstraction. • Input/ output and streams. • Branch and loop statements. • Arrays and strings. • Pointers. • Recursions.

  5. Lecture 1: Agenda • Introduction - computer hardware & Programming Languages • First Program • Output Using pint • Directives • Comments • Integer Variables • Input with Scanner • Variable Names • Arithmetic Expressions • The arithmetic assignment operators • Character Variables • Floating Point Types • const Qualifier • Type bool • The setw Manipulator • Variable Type Summary • Type Conversion • Increment Operators • Math Library Functions

  6. Main computer components • A computer is a machine that can perform computation. a computation involves the following three components: • Input: The user gives a set of input data. • Processing: The input data is processed by a well-defined and finite sequence of steps. • Output: Some data available from the processing step are output to the user. Usually, computations are carried out to solve some meaningful and useful problems. One supplies some input instances for the problem, which are then analyzed in order to obtain the answers for the instances.

  7. Why digital computers & problems types • Usually, computer perform computations in order to solve some meaningful and useful problems faster and more accurately. One supplies some input instances for the problem, which are then analyzed in order to obtain the answers for the instances. • Types of problems • Functional problems A set of arguments a1,a2,...,are send to Some function, as f(a1,a2,...,an), which in turn calculate the arguments and output to the user. • Decision problems These form a special class of functional problems whose outputs are "yes" and "no" (or "true" and "false", or "1" and "0", etc). • Search problems Given an input object, one tries to locate some particular configuration pertaining to the object and outputs the located configuration, or "failure" if no configuration can be located. • Optimization problems Given an object, a configuration and a criterion for goodness, one finds and reports the configuration pertaining to the object, that is best with respect to the different existing paths.

  8. How does a program run in a computer? • The inputs, the intermediate values and the instructions defining the processing stage reside in the (main) memory. In order to separate data from instructions the memory is divided into two parts: • Data area The data area stores the variables needed for the processing stage. The values stored in the data area can be read, written and modified by the CPU. • Instruction area The instruction area stores a sequence of instructions that define the steps of the program. Under the control of a clock, the computer carries out a fetch-decode-execute cycle in which instructions are fetched one-by-one from the instruction area to the CPU, decoded in the control unit and executed in the ALU. The CPU understands only a specific set of instructions. The instructions stored in memory must conform to this specification.

  9. How does a program run in a computer?

  10. What is programming? • Programming is a process done by programmers to instruct a computer on how to do a task . • It is Planning or scheduling a sequence of steps for a computer to follow to perform a task. • Basically, telling a computer what to do and how to do it. • A program is : • A sequence of steps to be performed by a computer. • Expressed in a computer language.

  11. Programming Techniques Unstructured Programming Procedural Programming Modular & Structural Programming Abstract Data Type Object-Oriented Programming

  12. Unstructured Programming Small and (simple?) programs. Consists only of one main program. Here ``main program'' stands for a sequence of commands or statements which modify data which is global throughout the whole program. Main Program Data

  13. Drawbacks This programming technique can only be used in a very small program. Why? For example, if the same statement sequence is needed at different locations within the program, the sequence must be copied. If an error needed to be modified, every copy needs to be modified.  This has lead to the idea to extract these sequences (procedure), name them and offering a technique to call and return from these procedures.

  14. Programming Techniques Unstructured Programming Procedural Programming Modular & Structural Programming Abstract Data Type Object-Oriented Programming

  15. Procedural Programming With procedural programming, you are able to combine sequences of calling statements into one single place. A procedure call is used to invoke the procedure. After the sequence is processed, flow of control proceeds right after the position where the call was made . Main Program Procedure

  16. Procedures With parameters and sub-procedures (procedures of procedures) , programs can now be written more structured and with less errors. For example, if a procedure is correct, every time it is used it produces correct results. Consequently, in cases of errors you can narrow your search to those places which are not proven to be correct.

  17. Now a program can be viewed as a sequence of procedure calls. The main program is responsible to pass data to the individual calls, the data is processed by the procedures and the resulting data is presented. Thus, the flow of data can be illustrated as a hierarchical graph, a tree. Procedure Program view

  18. Procedure Program view Main Program Data Procedure3 Procedure2 Procedure1

  19. Programming Techniques Unstructured Programming Procedural Programming Modular & Structural Programming Abstract Data Type Object-Oriented Programming Will be discussed next year

  20. Modular Programming Modular programming is subdividing your program into separate subprograms. Procedures of a common functionality are grouped together into separate modules. A program no longer consists of only one single part. It is now divided into several smaller parts which interact through procedure calls. The main program coordinates calls to procedures inseparate modules and hands over appropriate data as parameters.

  21. Main Program (Also a module) Data Module2 + Module1 + Data Data Data1 Data1 Data Data2 Procedure2 Procedure3 Procedure1

  22. Structured Programming A subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. “organizing considering time and space” Certain languages such as Ada, and Pascal were designed with features that encourage or enforce a logical program

  23. Structured Programming Three Types of Structures in a structured program Basic structures for programming? -Statement sequence(s1,s2,…,sn) -Branch(if-then-else) -Loop(for,do, and while loops) - Keep modules!!!

  24. Programming languages classes?

  25. Programming Languages • There are many different programming languages, and many ways to classify them. For example, • "high-level" programming languages are languages whose syntax is relatively close to natural language, • whereas the syntax of "low-level" languages includes many technical references to the (0's and 1's, etc.) • C is in many ways hard to categories. Compared to assemblylanguage it is high-level, but it nevertheless includes many low-level facilities to directly manipulate the computer's memory.

  26. high – level languages • Are designed to be easy to read and write • Use more complicated instructions than the CPU can follow • Must be translated to zeros and ones for the CPU to execute a program

  27. Machine, Assembler and high level language • Machine language is a numeric language specifically understood by a computer’s processor (the CPU). • Assembly language consist of statements written with short mnemonics such as ADD, MOV, ….It has a one-to- one relationship with machine language instructions • High level language ( such as C++ and Java) has a one–to–many relationship with machine/assembly language instructions

  28. How computers understand the instruction? • Programs are written in a programming language, then translated into machine code by a compiler and linker so that the computer can execute it directly or run it line by line (interpreted) by an interpreter program.

  29. Compilers • Translate high-level language to machine language Linkers A Linker combines • The object code for the programs we write And • The object code for the pre-compiled routines Into • The machine language program, the CPU can run (executable program)

  30. Agenda • Introduction - computer hardware & Programming Languages • First Program • Output Using print • Directives • Comments • Integer Variables • Input with scanner • Variable Names • Arithmetic Expressions • The arithmetic assignment operators

  31. The First Java Program Type all carefully and save it to a file named Welcome.java class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } // end method main } // end class welcome

  32. The First Java Program Java program source files (.java) contain definition of classes class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } // end method main } // end class welcome

  33. The First Java Program The class name or identifier class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } // end method main } // end class welcome

  34. Class name or identifier • class names begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). • A class name is an identifier—a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces. • Ex. valid identifiers such as Welcome1, $value,_value, m_inputField1 and button7. • Ex. Invalid identifiers such as 7button -not a valid identifier because start with digit input field - not a valid identifier because it contains a space • identifier that does not begin with a capital letter is not a class name. • Java is case sensitive—uppercase and lowercase letters are distinct—so value and Value are different.

  35. The First Java Program Curly braces pair enclose a block of code, class Welcome here class Welcome{ /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } } Don’t miss me!

  36. The First Java Program Curly braces pair enclose a block of code, method main() here class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } } Don’t miss me!

  37. The First Java Program This is a block of comments, for human, not for computer class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } } It explains to you what happens

  38. The First Java Program /* and */ pair encloses a comment block (over several lines) class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } // end of method main } // end of class Don’t miss me! One line comment

  39. The First Java Program This is a method of the class Welcome, named main() class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

  40. Declaring a method • The parentheses after the identifier main indicate that it’s a program building block called a method. • Java class declarations can contain one or more methods. • one of the methods must be called main and must be defined as shown previously otherwise, the JVM will not execute the application

  41. The First Java Program There MUST be a pair of parentheses following ALL method names class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } } Don’t miss me!

  42. The First Java Program A method may take some input from the caller, formally known as arguments or parameters class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

  43. The First Java Program A method may give some output to the caller too, known as return value void means no return value class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

  44. The First Java Program The static keyword before a method definition indicates this is a class method class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

  45. The First Java Program This method is a public one, others can call me. class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

  46. The First Java Program Standard properties of the main() method class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

  47. The First Java Program A statement (instruction) to display a message “standard output object” class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

  48. Selecting method print Class System, defined in the Standard Package of java Output stream object, it is member of System System . Out . println(“string"); Method member of Out Selects an individual class member of System Selects an individual class member of Out Parameter of Print method

  49. The First Java Program After every statement, there must be a semi-colon! class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

  50. The First Java Program How to ask the computer to act according to the instructions in this program? class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } }

More Related