Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Programming Languages

Similar presentations


Presentation on theme: "An Introduction to Programming Languages"— Presentation transcript:

1 An Introduction to Programming Languages
M105 - Week 1 An Introduction to Programming Languages Prepared by: M105 Team - AOU - SAB

2 Prepared by: M105 Team - AOU - SAB
Objectives Computer Architecture State the difference between the low-level and high- level programming languages. Java - Versions and Editions The Java Software Development Kit How the Java compiler works Software Engineering Identify the basic components of Java programs Use Standard Classes Prepared by: M105 Team - AOU - SAB

3 1- Computer Architecture
Output Devices RAM CPU Commu- nication Devices Storage Devices Input Devices Chapter 0 0.2 Computer Architecture A typical computer today has five basic components: RAM, CPU, storage devices, I/O (input/output) devices, and communication devices. Random access memory (RAM) is a repository for both program instructions and data manipulated by the program during execution. RAM is divided into cells with each cell having a unique address. The central processing unit (CPU) is the brain of a computer. The CPU is the component that executes program instructions by fetching an instruction (stored in RAM), executing it, fetching the next instruction, executing it, and so on until it encounters an instruction to stop. Input/output (I/O) devices allow communication between the user and the CPU. Input devices such as keyboards and mice are used to enter data, programs, and commands in the CPU. Output devices such as monitors and printers are used to display or print information. Storage devices such as disk and tape drives are used to store data and programs. Secondary storage devices are called nonvolatile memory, while RAM is called volatile memory. A communication device connects the personal computer to a network. The most common communication device for computers at home and small offices is the modem. A communication device for connecting a computer to a LAN is a network interface card (NIC). Prepared by: M105 Team - AOU - SAB

4 2- Programming Languages
Three levels of programming languages: Machine Languages Machine language instructions are binary coded and very low level. Assembly Languages Assembly language allows symbolic programming. Requires an assembler to translate assembly programs into machine programs. High-level Languages High-level language provides a very high conceptual model of computing. Requires a compiler to translate high-level pograms into assembly programs. Chapter 0 0.3 Programming Languages Examples for the tree levels of programming languages Machine code: Assembly code: MV 0, SUM MV NUM, AC ADD SUM, AC STO SUM, TOT High-level code: X = ( Y + Z ) / 2 Prepared by: M105 Team - AOU - SAB

5 3- Java - Versions and Editions
Java is a high-level object-oriented language developed by Sun Microsystems. Java’s clean design and wide availability make it an ideal language for teaching the fundamentals of computer programming. The first publicly available version of the language was Java 1.0. Java 1.2 being a standard and stable version Sun changed the name of Java 1.2 to Java 2 version 1.2 Java 2 is the standard language, and there have been a number of further versions (Java 2 version 1.3, Java 2 version 1.4, Java 2 version 1.5 and so on) Chapter 0 0.4 Java Versions and editions of Java Java has undergone some significant changes since its early versions, so it is important to have some appreciation of the way these versions are distinguished. The first publicly available version of the language was Java 1.0. Although it was widely promoted as a serious language for the internet, it actually had a number of significant deficiencies. Many of these were remedied in later versions, with Java 1.2 being a standard and stable version for quite some time. To emphasize the change, and perhaps also as a marketing tool, Sun changed the name of Java 1.2 to Java 2 version 1.2. Subsequent versions to date have followed this naming convention. At the time of writing, Java 2 is the standard language, and there have been a number of further versions (Java 2 version 1.3, Java 2 version 1.4, Java 2 version 1.5 and so on), each introducing relatively minor improvements. In 1999, Sun recognized that the concept that one standard Java language package could apply to all sorts of platforms was not really feasible. So the concept of Java editions was introduced, to cater for the different needs of, say, large international business systems running on extensive networks with many servers as opposed to software running on mobile phones with very limited hardware resources. We will mostly be interested in the Java 2 Standard Edition (J2SE). Large-scale systems are catered for by the Java 2 Enterprise Edition (J2EE) Java 2 Micro Edition (J2ME), designed for use on smaller systems with limited resources. Prepared by: M105 Team - AOU - SAB

6 3- Java - Versions and Editions
In 1999 the concept of Java editions was introduced , to cater for the different needs of, say, large international business systems running on extensive networks with many servers as opposed to software running on mobile phones with very limited hardware resources. We will mostly be interested in the Java 2 Standard Edition (J2SE). Large-scale systems are catered for by the Java 2 Enterprise Edition (J2EE) Java 2 Micro Edition (J2ME), designed for use on smaller systems with limited resources. Chapter 0 0.4 Java Versions and editions of Java In 1999, Sun recognized that the concept that one standard Java language package could apply to all sorts of platforms was not really feasible. So the concept of Java editions was introduced, to cater for the different needs of, say, large international business systems running on extensive networks with many servers as opposed to software running on mobile phones with very limited hardware resources. We will mostly be interested in the Java 2 Standard Edition (J2SE). Large-scale systems are catered for by the Java 2 Enterprise Edition (J2EE) Java 2 Micro Edition (J2ME), designed for use on smaller systems with limited resources. Prepared by: M105 Team - AOU - SAB

7 4- The Java Software Development Kit
The Java Software Development Kit The basic tools for compiling and running Java programs on a variety of platforms are freely available from Sun Microsystems. This collection of tools is known as the Java Software Development Kit, often abbreviated to the Java SDK or the JDK. This kit includes a number of important components, listed below The Java compiler (javac) translates Java source into bytecode. The Java interpreter (java) translates and executes Java bytecode. Java There are more but not cover in this course Chapter 0 0.4 Java The Java Software Development Kit The basic tools for compiling and running Java programs on a variety of platforms are freely available from Sun Microsystems. This collection of tools is known as the Java Software Development Kit, often abbreviated to the Java SDK or the JDK. This kit includes a number of important components, listed below - The Java compiler (javac) translates Java source into bytecode. - The Java interpreter (java) translates and executes Java bytecode. Java Prepared by: M105 Team - AOU - SAB

8 5- How the Java compiler works
The programmer develops a program expressed in what is known as source code. the compiler translates the source code into an intermediate form, known as bytecode. This is a language that is capable of being executed by any computer with a suitable interpreter. The interpreter is a program that translates bytecode into the native code of the computer it runs on, so that it can be executed. Chapter 2 2.3 Edit-Compile-Run Cycle The programmer develops a program expressed in what is known as source code. In the case of Java, the compiler translates the source code into an intermediate form, known as bytecode. This is a language that is capable of being executed by any computer with a suitable interpreter. The interpreter is a program that translates bytecode into the native code of the computer it runs on, so that it can be executed. Prepared by: M105 Team - AOU - SAB

9 6- Software Engineering & Software Engineering
Much like building a skyscraper, we need a disciplined approach in developing complex software applications. Software engineering is the application of a systematic and disciplined approach to the development, testing, and maintenance of a program. In this class, we will learn how to apply sound software engineering principles when we develop sample programs. Chapter 1 1.5 Software Engineering and Software Life Cycle When a construction firm builds a house, it will follow a strict construction sequence that is based on sound engineering principles. Although software is not a physical object, it is just as complex as a building. And we also need to follow a strict software construction sequence if we expect to build an error-free software. We need a software construction sequence that is based on sound software engineering principles. Prepared by: M105 Team - AOU - SAB

10 Prepared by: M105 Team - AOU - SAB
6- Software Life Cycle The sequence of stages from conception to operation of a program is called software life cycle. Five stages are Analysis Design Coding Testing Operation and Maintenance Chapter 1 1.5 Software Engineering and Software Life Cycle In the analysis phase, we perform a feasibility study. We analyze the problem and determine whether a solution is possible. If a solution is possible, the result of this phase is a requirements specification which describes the features of a program. In the design phase, we turn a requirements specification into a detailed design of the program. For an object-oriented design, the output from this phase will be a set of classes/objects that fulfill the requirements. In the coding phase, we implement the design into an actual program, in our case, a Java program. In the testing phase, we run the program using different sets of data to verify that the program runs according to the specification. Two types of testing are possible for object-oriented programs: unit testing and integration testing. With unit testing, we test classes individually. With integration testing, we test that the classes work together correctly. Activity to eliminate programming error is called debugging. An error could be a result of faulty implementation or design. When there's an error, we need to backtrack to earlier phases to eliminate the error. Finally, after the testing is successfully concluded, we enter the operation phase in which the program will be put into actual use. The most important and time-consuming activity during the operation phase is software maintenance. Prepared by: M105 Team - AOU - SAB

11 Prepared by: M105 Team - AOU - SAB
6- Software Life Cycle In the analysis phase, we perform a feasibility study. We analyze the problem and determine whether a solution is possible. If a solution is possible, the result of this phase is a requirements specification which describes the features of a program. In the design phase, we turn a requirements specification into a detailed design of the program. For an object-oriented design, the output from this phase will be a set of classes/objects that fulfill the requirements. Chapter 1 1.5 Software Engineering and Software Life Cycle In the analysis phase, we perform a feasibility study. We analyze the problem and determine whether a solution is possible. If a solution is possible, the result of this phase is a requirements specification which describes the features of a program. In the design phase, we turn a requirements specification into a detailed design of the program. For an object-oriented design, the output from this phase will be a set of classes/objects that fulfill the requirements. In the coding phase, we implement the design into an actual program, in our case, a Java program. In the testing phase, we run the program using different sets of data to verify that the program runs according to the specification. Two types of testing are possible for object-oriented programs: unit testing and integration testing. With unit testing, we test classes individually. With integration testing, we test that the classes work together correctly. Activity to eliminate programming error is called debugging. An error could be a result of faulty implementation or design. When there's an error, we need to backtrack to earlier phases to eliminate the error. Finally, after the testing is successfully concluded, we enter the operation phase in which the program will be put into actual use. The most important and time-consuming activity during the operation phase is software maintenance. Prepared by: M105 Team - AOU - SAB

12 Prepared by: M105 Team - AOU - SAB
6- Software Life Cycle In the coding phase, we implement the design into an actual program, in our case, a Java program. In the testing phase, we run the program using different sets of data to verify that the program runs according to the specification. Two types of testing are possible for object-oriented programs: unit testing and integration testing. With unit testing, we test classes individually. With integration testing, we test that the classes work together correctly. Chapter 1 1.5 Software Engineering and Software Life Cycle In the analysis phase, we perform a feasibility study. We analyze the problem and determine whether a solution is possible. If a solution is possible, the result of this phase is a requirements specification which describes the features of a program. In the design phase, we turn a requirements specification into a detailed design of the program. For an object-oriented design, the output from this phase will be a set of classes/objects that fulfill the requirements. In the coding phase, we implement the design into an actual program, in our case, a Java program. In the testing phase, we run the program using different sets of data to verify that the program runs according to the specification. Two types of testing are possible for object-oriented programs: unit testing and integration testing. With unit testing, we test classes individually. With integration testing, we test that the classes work together correctly. Activity to eliminate programming error is called debugging. An error could be a result of faulty implementation or design. When there's an error, we need to backtrack to earlier phases to eliminate the error. Finally, after the testing is successfully concluded, we enter the operation phase in which the program will be put into actual use. The most important and time-consuming activity during the operation phase is software maintenance. Prepared by: M105 Team - AOU - SAB

13 Prepared by: M105 Team - AOU - SAB
6- Software Life Cycle Activity to eliminate programming error is called debugging. An error could be a result of faulty implementation or design. When there's an error, we need to backtrack to earlier phases to eliminate the error. Finally, after the testing is successfully concluded, we enter the operation phase in which the program will be put into actual use. The most important and time-consuming activity during the operation phase is software maintenance. Chapter 1 1.5 Software Engineering and Software Life Cycle In the analysis phase, we perform a feasibility study. We analyze the problem and determine whether a solution is possible. If a solution is possible, the result of this phase is a requirements specification which describes the features of a program. In the design phase, we turn a requirements specification into a detailed design of the program. For an object-oriented design, the output from this phase will be a set of classes/objects that fulfill the requirements. In the coding phase, we implement the design into an actual program, in our case, a Java program. In the testing phase, we run the program using different sets of data to verify that the program runs according to the specification. Two types of testing are possible for object-oriented programs: unit testing and integration testing. With unit testing, we test classes individually. With integration testing, we test that the classes work together correctly. Activity to eliminate programming error is called debugging. An error could be a result of faulty implementation or design. When there's an error, we need to backtrack to earlier phases to eliminate the error. Finally, after the testing is successfully concluded, we enter the operation phase in which the program will be put into actual use. The most important and time-consuming activity during the operation phase is software maintenance. Prepared by: M105 Team - AOU - SAB

14 Prepared by: M105 Team - AOU - SAB
7- Program Components A Java program is composed of comments, import statements, and class declarations. Chapter 2 2.2 Program Components Three key elements are comments, import statements, and class declarations. Strictly speaking, you can write a Java program that does not include comments or import statements, but such program is either very trivial or not well designed. Prepared by: M105 Team - AOU - SAB

15 Template for Simple Java Programs
/* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import javax.swing.*; public class Ch2Sample { public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Comment Import Statements Class Name Method Body Chapter 2 2.2 Program Components Comment: Use a comment to describe the program Import Statements: Include a sequence of import statements. Class declarations: 1- Class Name: Give a descriptive name to the main class. 2- Method Body: Include a sequence of instructions. Prepared by: M105 Team - AOU - SAB

16 Prepared by: M105 Team - AOU - SAB
1- Comment Comment /* Meeting 1Sample Program: Displaying a Window File: First.java */ import javax.swing.*; public class First { public static void main(String[ ] args) { JOptionPane.showMessageDialog(null, “hello”); } Chapter 2 2.2 Program Components A comment is any sequence of text that begins with the marker /* and terminates with another marker */. Although not required to run the program, comments are necessary in writing easy-to-understand code. /** (Mrs. Malak El-Amir) * M105 - Meeting1 * First.java * this program displays hello using dialog box */ import javax.swing.*; public class First { public static void main (String []args) JOptionPane.showMessageDialog(null, "hello"); }// main method }// class Prepared by: M105 Team - AOU - SAB

17 Matching Comment Markers
/* This is a comment on one line */ /* Comment number 1 */ Comment number 2 This is a comment Error: No matching beginning marker. These are part of the comment. Chapter 2 2.2 Program Components The beginning and ending markers are matched in pairs; that is, every beginning marker must have a matching ending marker. A beginning marker is matched with the next ending marker that appears. Prepared by: M105 Team - AOU - SAB

18 Three Types of Comments
/* This is a comment with three lines of text. */ Multiline Comment Single line Comments // This is a comment // This is another comment // This is a third comment /** * This class provides basic clock functions. In addition * to reading the current time and today’s date, you can * use this class for stopwatch functions. */ javadoc Comments Chapter 2 2.2 Program Components The sample comments shown in previous slides are called multiline comments. There are two additional types of comments used in Java. The second type of comment is called a single-line comment. A single-line comment starts with double slashes //. Any text between the double-slash marker and the end of a line is a comment. The third type of comment is called a javadoc comment. It is a specialized comment that can appear before the class declaration and other program elements yet to be described in the book. Javadoc comments begin with the /** marker and end with the */ marker. We won’t worry about javadoc comments now. At this point, you are only required just to be aware of it. If you are curious about javadoc comments, you can find information on javadoc and samle Web documentation pages that are generated from javadoc comments at techdocs/api/ Prepared by: M105 Team - AOU - SAB

19 Prepared by: M105 Team - AOU - SAB
2- Import Statement /* Meeting 1Sample Program: Displaying a Window File: First.java */ import javax.swing.*; public class First { public static void main(String[ ] args) { JOptionPane.showMessageDialog(null, “hello”); } Import Statement Chapter 2 2.2 Program Components The import statement allows the program to use classes and their instances defined in the designated package. We develop object-oriented programs by using predefined classes whenever possible and defining our own classes when no suitable predefined classes are available. In Java, classes are grouped into packages. The Java compiler comes with over 2000 standard classes organized into several hundred packages. Prepared by: M105 Team - AOU - SAB

20 Import Statement Syntax and Semantics
Package Name Name of the package that contains the classes we want to use. Class Name The name of the class we want to import. Use asterisks to import all classes. import <package name> <class name> ; e.g. import java . util . * ; More Examples Chapter 2 2.2 Program Components To use the Resident class in the dorm package, we write java.util; which we read as “java dot util.” This notation is called dot notation. Notice that the import statement is terminated by a semicolon. A package can include subpackages, forming a hierarchy of packages. The notation javax.swing.JFrame means the JFrame class is located in the package swing, which itself is contained in the top-level javax package. If you need to import more than one class from the same package, then instead of using an import statement for every class, you can import them all using asterisk notation: <package name> . * ; For example, when we write import java.util.*; then we are importing all class from the java.util package. import javax.swing.JFrame; import java.util.*; import com.drcaffeine.simplegui.*; Prepared by: M105 Team - AOU - SAB

21 Prepared by: M105 Team - AOU - SAB
3- Class Declaration /* Meeting 1Sample Program: Displaying a Window File: First.java */ import javax.swing.*; public class First { public static void main(String[ ] args) { JOptionPane.showMessageDialog(null, “hello”); } Class Declaration Chapter 2 2.2 Program Components A Java program is composed of one or more classes, some of them are predefined classes, while others are defined by ourselves. In this sample program, First class is the class we define ourselves. The syntax for class declaration is <modifiers> class <class name> { <method > } Where <modifiers> is a sequence of terms designating different kinds of class, The word class is a reserved word used to mark the beginning of a class declaration. We can use any valid identifier to name the class. <class name> is the name of a class, <method > is a sequence of statements. One of the classes in a program must be designated as the main class. The main class of the sample program is First. The main class must include a method called main, because when a Java program is executed, the main method of a main class is executed first. In other words, it is the entry point of program execution. Prepared by: M105 Team - AOU - SAB

22 Prepared by: M105 Team - AOU - SAB
Method Declaration /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ import javax.swing.*; class First { public static void main(String[ ] args) { JOptionPane.showMessageDialog(null, “hello”); } Method Declaration Chapter 2 2.2 Program Components Here’s the main method of the class. We can include any valid statements in the main method, but the structure is fixed and cannot be changed. Prepared by: M105 Team - AOU - SAB

23 Method Declaration Elements
Modifier Return Type Method Name Parameter public static void main ( String[ ] args ){ JOptionPane.showMessageDialog(null, “hello”); } Method Body Chapter 2 2.2 Program Components The syntax for method declaration is <modifiers> <return type> <method name> ( <parameters> ) { <method body> } Where <modifiers> is a sequence of terms designating different kinds of methods, <return type> is the type of data value returned by a method, <method name> is the name of a method, <parameters> is a sequence of values passed to a method, and <method body> is a sequence of statements. We will explain the meanings of modifies, return types, and parameters in detail gradually as we progress through the course. Prepared by: M105 Team - AOU - SAB

24 Prepared by: M105 Team - AOU - SAB
8- Use Standard Classes Learning how to use standard Java classes is the first step toward mastering OOP. Before we can learn how to define our own classes, we need to learn how to use existing classes We will introduce one of the standard classes here: JOptionPane And use it methods showMessageDialog showInputDialog Chapter 2 2.4 Sample Java Standard Classes Java comes with over 2000 standard classes. Developing complex Java programs involves defining our own classes and using existing standard classes. Whenever possible, we should use existing classes and avoid reinventing the wheel. Also, being able to use existing classes is a necessary foundation for us to learn how define our own classes. In this lesson, we will introduce one of the standard classes. Prepared by: M105 Team - AOU - SAB

25 Prepared by: M105 Team - AOU - SAB
8- Use Standard Classes The documentation of the standard classes is called Java API documentation, where API stands for Application Programming Interface. In a Graphical User Interface GUI environment there are two types of windows: a frame window and a dialog window. We will know about frame window later in this course. We use import statement to import the package javax and the sub-package swing and all classes inside it. Chapter 2 2.4 Sample Java Standard Classes The documentation of the standard classes is called Java API documentation, where API stands for Application Programming Interface. In a Graphical User Interface GUI environment there are two types of windows: a frame window and a dialog window. We will know about frame window later in this course. We use import statement to import the package javax and the sub-package swing and all classes inside it. Prepared by: M105 Team - AOU - SAB

26 JOptionPane for output
Using showMessageDialog of the JOptionPane class is a simple way to display a result of a computation to the user. JOptionPane.showMessageDialog(null,“ILove Java”); Chapter 2 2.4 Sample Java Standard Classes 2.4.1 JOptionPane for output JOptionPane class is a class inside a swing package. It provides a method for displaying shot messages. It is showMessageDialog. In this example, we are displaying a message I Love Java. The dialog with the specified message appears on the center of the screen. The argument null indicates that there is no containing frame. This indication results in a dialog appearing at the center of the screen. /** (Mrs. Malak El-Amir) * M105 - Meeting1 * Prog1.java * this program displays I Love Java using dialog box */ import javax.swing.*; public class Prog1 { public static void main (String []args) JOptionPane.showMessageDialog(null, "I Love Java"); }// main method }// class This dialog will appear at the center of the screen. Prepared by: M105 Team - AOU - SAB

27 Displaying Multiple Lines of Text
We can display multiple lines of text by separating lines with a new line marker \n. JOptionPane.showMessageDialog(null, “one\ntwo\nthree”); Chapter 2 2.4 Sample Java Standard Classes 2.4.1 JOptionPane for outputWe can only pass a single line text to the showMessageDialog method, but it is possible to display it as multiple lines of text. The key is to embed a nonprintable new line marker \n. Notice that in the actual display, the new line markers are not shown. This new line and other nonprintable markers are called control characters because they are not for display but for controlling the test is displayed. /** (Ms. Malak El-Amir) * M105 - Meeting1 * Prog2.java * this program displays multiple lines of text using dialog box */ import javax.swing.*; public class Prog2 { public static void main (String []args) JOptionPane.showMessageDialog(null, "one \ntwo \nthree"); }// main method }// class Prepared by: M105 Team - AOU - SAB

28 Prepared by: M105 Team - AOU - SAB
JOptionPane for Input Using showInputDialog of the JOptionPane class is a simple way to input a string. String name; name = JOptionPane.showInputDialog (null, “What is your name?”); This dialog will appear at the center of the screen ready to accept an input. Chapter 2 2.4 Sample Java Standard Classes 2.4.1 JOptionPane for input The showInputDialog method allows a program to accept a String data/input from the end-user. The method returns the data entered by the end-user as a String object, so we can assign it to a variable. /** (Mrs. Malak El-Amir) * M105 - Meeting1 * Prog3.java * this program asks the user to enter his name and displays Welcome text using dialog box */ import javax.swing.*; public class Prog3 { public static void main (String []args) String name; name = JOptionPane.showInputDialog(null, "Enter your name"); JOptionPane.showMessageDialog(null, "Welcome " + name); }// main method }// class Prepared by: M105 Team - AOU - SAB

29 JOptionPane for output
Using showMessageDialog of the JOptionPane class is a simple way to display a result of a computation to the user. JOptionPane.showMessageDialog(null, "Welcome " + name); Chapter 2 2.4 Sample Java Standard Classes 2.4.1 JOptionPane for input The showInputDialog method allows a program to accept a String data/input from the end-user. The method returns the data entered by the end-user as a String object, so we can assign it to a variable. /** (Mrs. Malak El-Amir) * M105 - Meeting1 * Prog3.java * this program asks the user to enter his name and displays Welcome text using dialog box */ import javax.swing.*; public class Prog3 { public static void main (String []args) String name; name = JOptionPane.showInputDialog(null, "Enter your name"); JOptionPane.showMessageDialog(null, "Welcome " + name); }// main method }// class Welcome Ahmed This dialog will appear at the center of the screen. Prepared by: M105 Team - AOU - SAB


Download ppt "An Introduction to Programming Languages"

Similar presentations


Ads by Google