Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


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

1 Introduction to Programming
Java Language

2 Programs Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Programs are written using programming languages. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them.

3 Programming Languages
Machine Language Assembly Language High-Level Language Machine language is a set of primitive instructions, in the form of binary code. Program with native machine language is a tedious process and it is highly difficult to read and modify. Example, to add two numbers, you might write an instruction in binary like this: 

4 Programming Languages
Machine Language Assembly Language High-Level Language Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, a program called assembler is used to convert assembly language programs into machine code. Example, to add two numbers, you might write an instruction in assembly code like this: ADD R1, R2

5 Programming Languages
Machine Language Assembly Language High-Level Language The high-level languages are English-like and easy to learn and program. Example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * ;

6 Interpreting/Compiling Source Code
A program written in a high-level language is called a source program or source code. Because a computer cannot understand a source program, a source program must be translated into machine code for execution. The translation can be done using another programming tool called an interpreter or a compiler.

7 Interpreting Source Code
An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, and then executes it right away. Note that a statement from the source code may be translated into several machine instructions.

8 Compiling Source Code A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed.

9 Popular High-Level Languages

10 Why Java? One of the most popular programming languages in use.
Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. "write once, run anywhere": code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM).

11 Characteristics of Java
Java is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. was originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation). derives much of its syntax from C and C++.

12 JDK Versions JDK 1.02 (1995) JDK 1.1 (1996) JDK 1.2 (1998)
JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6 JDK 1.7 (2011) a. k. a. JDK 7 or Java 7 JDK 1.8 (2014) a. k. a. JDK 8 or Java 8

13 JDK Editions Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone applications or applets. Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets, Java ServerPages, and Java ServerFaces. Java Micro Edition (J2ME). J2ME can be used to develop applications for mobile devices such as cell phones. JSP (JavaServer Pages) JSP is a Java view technology running on the server machine which allows you to write template text in (the client side languages like HTML, CSS, JavaScript and so on). JSP supports taglibs, which are backed by pieces of Java code that let you control the page flow or output dynamically. A well known taglib is JSTL. JSP also supports Expression Language, which can be used to access backend data (via attributes available in page, request, session and application scopes), mostly in combination with taglibs. When a JSP is requested for the first time or when the webapp starts up, the servlet container will compile it into a class extending HttpServlet and use it during the webapp's lifetime. You can find the generated source code in the server's work directory. In for example Tomcat, it's the /workdirectory. On a JSP request, the servlet container will execute the compiled JSP class and send the generated output (usually just HTML/CSS/JS) through the webserver over network to the client side, which in turn displays it in the web browser. Servlets Servlet is an Java application programming interface (API) running on the server machine, which intercepts requests made by the client and generates/sends a response. A well known example is theHttpServlet which provides methods to hook on HTTP requests using the popular HTTP methodssuch as GET and POST. You can configure HttpServlets to listen on a certain HTTP URL pattern, which is configurable in web.xml, or more recently with Java EE 6, When a Servlet is first requested or during webapp startup, the servlet container will create an instance of it and keep it in memory during the webapp's lifetime. The same instance will be reused for every incoming request whose URL matches the servlet's URL pattern. You can access the request data by HttpServletRequest and handle the response by HttpServletResponse. Both objects are available as method arguments inside any of the overridden methods of HttpServlet, such as doGet() and doPost(). JSF (JavaServer Faces) JSF is a component based MVC framework which is built on top of the Servlet API, and providescomponents via taglibs which can be used in JSP or any other Java based view technology such asFacelets. Facelets is much more suited to JSF than JSP. It namely provides great templating capabilities such as composite components, while JSP basically only offers the <jsp:include> for templating, so that you're forced to create custom components with raw Java code (which is a bit opaque and a lot of tedious work in JSF) when you want to replace a repeated group of components with a single component. Since JSF 2.0, JSP has been deprecated as view technology in favor of Facelets. As being a MVC (Model-View-Controller) framework, JSF provides the FacesServlet as the sole request-response Controller. It takes all the standard and tedious HTTP request/response work from your hands, such as gathering user input, validating/converting them, putting them in model objects, invoking actions and rendering the response. This way you end up with basically a JSP or Facelets (XHTML) page for View and a Javabean class as Model. The JSF components are been used to bind the view with the model (such as your ASP.NET web control does) and the FacesServlet uses theJSF component tree to do all the work.

14 Popular Java IDEs NetBeans Eclipse https://netbeans.org/

15 A Simple Java Program Listing 1.1
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } IMPORTANT NOTE: (1) To enable the buttons, you must download the entire slide file slide.zip and unzip the files into a directory (e.g., c:\slide) . (2) You must have installed JDK and set JDK’s bin directory in your environment path (e.g., c:\Program Files\java\jdk1.7.0\bin in your environment path. (3) If you are using Office 2010, check PowerPoint2010.doc located in the same folder with this ppt file. Welcome Run

16 Creating and Editing Using NotePad
To use NotePad, type notepad Welcome.java from the DOS prompt.

17 Creating and Editing Using WordPad
To use WordPad, type write Welcome.java from the DOS prompt.

18 Creating, Compiling, and Running Programs

19 Compiling Java Source Code
When using another programming language, you can port a source program to any machine with appropriate compilers but the source program must be recompiled, because the object program can only run on a specific machine. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a JVM. JVM is a software that interprets Java bytecode.

20 Trace a Program Execution
animation Trace a Program Execution Enter main method //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

21 Trace a Program Execution
animation Trace a Program Execution Execute statement //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

22 Trace a Program Execution
animation Trace a Program Execution //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } print a message to the console

23 Anatomy of a Java Program
Class name Main method Statements Statement terminator Reserved words Comments Blocks

24 Class Name Every Java program must have at least one class.
Each class has a name. By convention, class names start with an uppercase letter. In the following example, the class name is Welcome: //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

25 Main Method Line 2 defines the main method.
In order to run a class, the class must contain a method named main. The program is executed from the main method. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

26 Statement A statement represents an action or a sequence of actions.
The statement System.out.println("Welcome to Java!") in the program is a statement to display the greeting "Welcome to Java! ". //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

27 Statement Terminator Every statement in Java ends with a semicolon (;). //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

28 Reserved words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. Example, when the compiler sees the word class, it understands that the word after class is the name for the class. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

29 Blocks A pair of braces in a program forms a block that groups components of a program.

30 Special Symbols

31 Programming Errors Syntax Errors Detected by the compiler
Runtime Errors Causes the program to abort Logic Errors Produces incorrect result

32 Syntax Errors public class ShowSyntaxErrors {
public static main(String[] args) { System.out.println("Welcome to Java); }

33 Runtime Errors public class ShowRuntimeErrors {
public static void main(String[] args) { System.out.println(1 / 0); }

34 Logic Errors public class ShowLogicErrors {
public static void main(String[] args) { for (i=1; i<=10; i++) ; {    System.out.println("Number is " + i);  } }

35 Lab

36 Supplements on the Companion Website
See Supplement I.B for installing and configuring JDK See Supplement I.C for compiling and running Java from the command window for details

37 Compiling and Running Java from the Command Window
Companion Website Compiling and Running Java from the Command Window Set path to JDK bin directory set path=c:\Program Files\java\jdk1.6.0\bin Set classpath to include the current directory set classpath=. Compile javac Welcome.java Run java Welcome

38 Compiling and Running Java from TextPad
Companion Website See Supplement II.A on the Website for details

39 Compiling and Running Java from Eclipse
Companion Website Compiling and Running Java from Eclipse See Supplement II.D on the Website for details

40 Compiling and Running Java from NetBeans
Companion Website Compiling and Running Java from NetBeans See Supplement I.D on the Website for details

41 Exercise 1 Learn the NetBeans Environment.
Create a project and write the following program. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }

42 Exercise 2 Test the following program: public class MyClass {
static int i; public static void main(String args[]){ System.out.println(i); } }

43 Exercise 3 Test the following program:
public class MultiplicationTable { public static void main(String[] args) System.out.println("Multiplication Table for the number 2 "); for(int i=0; i<=2; i++) System.out.println("2 * "+i+" = "+ 2*i); }

44 Two more simple exercises
WelcomeWithThreeMessages Run ComputeExpression Run


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google