Introduction to Programming

Slides:



Advertisements
Similar presentations
What is a Computer Program? For a computer to be able to do anything (multiply, play a song, run a word processor), it must be given the instructions.
Advertisements

L1-2:CSC ©Dr. Basheer M. Nasef Lecture #1 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Introduction to Java Programming, 4E
For more Lectures and Notes Visit
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
L EC. 01: J AVA FUNDAMENTALS Fall Java Programming.
LESSON 1 INTRODUCTION Compiled By: Edwin O. Okech [Tutor, Amoud University] JAVA PROGRAMMING.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
BASIC JAVA PROGRAMMING TUTORIAL. History  James Gosling and Sun Microsystems  Oak  Java, May 20, 1995, Sun World  Hot Java –The first Java-enabled.
Chapter 1 Introduction to Computers, Programs, and Java
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
Chapter 1 Introduction to Computers, Programs, and Java
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Chapter 1 Introduction to Computers, Programs, and Java 1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Programming Languages Machine.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
CPCS  Problem Solving in Everyday Life  Types of Problems  Problem Solving with Computers  Difficulties with Problem Solving 0-3.
1 Chapter 1 Introduction to Computers, Programs, and Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Programs,
1.  At the end of this slide, student can:  Explore tools, features, properties and interface of the Textpad.  Creating a new project.  Open and run.
1 Chapter 3 Programs and Java. 2 Objectives F To review computer programs, and operating systems. F To represent numbers in binary, decimal, and hexadecimal.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Chapter 1 Introduction to Computers, Programs, and Java 1.
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Programs.
Introduction to Java Programming with Forte Y. Daniel Liang.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved What is a Computer? A computer.
Object Oriented Rabie A. Ramadan, PhD Slides are exerted from different sources.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Application Architecture Using Java Hong Li. Introduction Developed by a team led by James Gosling at Sun Microsystem. Originally called Oak, designed.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Introduction to Java Programming. 2 Chapter 1 Introduction to Java and Forte F What Is Java? F Getting Started With Java Programming –Create, Compile.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1: Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
GC101 Introduction to computer and program
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Java programming lecture one
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1: Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Introduction to Java.
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
CPCS 202 – Programming I Computer Science Department College of Computing and Information Technology King Abdul Aziz University.
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 1 Introduction to Computers, Programs, and Java
Outcome of the Lecture Upon completion of this lecture you will be able to understand Fundamentals and Characteristics of Java Language Basic Terminology.
Introduction to Computers, Programs, and Java
Presentation transcript:

Introduction to Programming Java Language

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.

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:  1101101010011010

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

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 * 3.1415;

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.

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.

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

Popular High-Level Languages

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).

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++.

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

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, with @WebServlet annotation. 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.

Popular Java IDEs NetBeans Eclipse https://netbeans.org/ http://www.eclipse.org/

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

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

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

Creating, Compiling, and Running Programs

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.

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!"); }

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!"); }

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

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

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!"); }

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!"); }

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!"); }

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!"); }

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!"); }

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

Special Symbols

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

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

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

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

Lab

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 www.cs.armstrong.edu/liang/intro8e

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

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

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

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

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!"); }

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

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); }

Two more simple exercises WelcomeWithThreeMessages Run ComputeExpression Run