 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.

Slides:



Advertisements
Similar presentations
IT151: Introduction to Programming
Advertisements

Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Lecture 2 Introduction to C Programming
University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.
Introduction to C Programming
Chapter 1 Introduction to JAVA. Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.
Chapter 1: Introduction
Chapter 2 - Introduction to Java Applications
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Excerpts from Introduction to Java Programming, 4E Author: Y. Daniel Liang (Copyright by Prentice Hall)
Introduction to Java Programming, 4E
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2 - Introduction to Java Applications
Introduction to C Programming
Java Applications & Program Design
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.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Introduction to Java Programming with Forte Y. Daniel Liang.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction To JAVA By Ihtesham Ul Haq. Course Objectives Upon completing the course, you will understand Upon completing the course, you will understand.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
8/31: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Introduction to Java Programming. 2 Chapter 1 Introduction to Java and Forte F What Is Java? F Getting Started With Java Programming –Create, Compile.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Introduction to Java Programming, 4E Y. Daniel Liang.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 Introduction to Java Applications
Chapter 2 Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Chapter 1 Introduction to Computers, Programs, and Java
Fundamentals of Java Programs, Input/Output, Variables, and Arithmetic
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Anatomy of a Java Program
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Computer Programming-1 CSC 111
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA Section 1 Professor: Sana` Odeh Class Website: Office Hours: Mon. 3:30 pm - 4:30 pm in room 418 in WWH

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 2 Announcement Lab Session Wed. 1/12 Class will be held at the ITS Lab at 14 Washington Place. We will meet in class first, then will go to the lab together. Make sure you are on time!

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 3 Introduction Introductions to Java Programming basics –Introduce a basic Java program –We will use Jcreator –to create/edit/compile/ debug and execute/run our Java programs

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class A First Program in Java: Printing a Line of Text Application –Program that executes using the java interpreter Sample program –Show program, then analyze each line

 2003 Prentice Hall, Inc. All rights reserved. Outline 5 Welcome1.java Program Output 1 // first Java program called Welcome1.java 2 // Text-printing program. 3 4 public class Welcome1 { 5 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.println( "Welcome to Java Programming!" ); } // end method main } // end class Welcome1 Welcome to Java Programming!

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 6 Lets look at the first and second line of the previous program T his is a comment –Comments start with: // Comments ignored during program execution Improves code readability Document and describe code –Traditional comments: /*... */ /* This is a traditional comment. It can be split over many lines */ –Another line of comments –Note: line numbers not part of program, added for reference 1 // first Java program called Welcome1.java 2 // Text-printing program.

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 7 –Blank line Makes program more readable Blank lines, spaces, and tabs are white-space characters –Ignored by compiler –Begins class declaration for class Welcome1 – Public & class are reserved key words and have to be included in every program Reserved Keyword: words reserved for use by Java. –class keyword followed by class name ( in this case it is Welcome1) is referred to as an identifier. –An identifier is user defined –Welcome1 is the name of the program and should be same as the file name Welcome1.java Naming classes: capitalize every word –SampleClassName 3 4 public class Welcome1 { Lets look at the following lines of the previous program

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 8 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. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in Example 1 are public, static, and void. Their use will be introduced later in the book.

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 9 Lets look at the following lines of the previous program –Name of class called identifier ( in this case its Welcome1 ) Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces Examples: Welcome1, $ value, _value, button7 –7button is invalid Java is case sensitive (capitalization matters) –a1 and A1 are different –For chapters 2 to 7, use public keyword Certain details not important now Mimic certain features, discussions later 4 public class Welcome1 {

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class A Simple Program: Printing a Line of Text –Saving files File name must be class name with.java extension Welcome1.java –Left brace { Begins body of every class Right brace ends declarations (line 13) –Part of every Java application Applications begin executing at main –Parenthesis indicate main is a method (ch. 6) –Java applications contain one or more methods 4 public class Welcome1 { 7 public static void main( String args[] )

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 11 main Method The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { Statements; }

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 12 Blocks A pair of braces in a program forms a block that groups components of a program.

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 13 Statements A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!"); in the program in Example 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;).

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 14 Classes The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. The mystery of the class will continue to be unveiled throughout this book. For now, though, understand that a program is defined by using one or more classes.

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class A Simple Program: Printing a Line of Text Exactly one method must be called main –Methods can perform tasks and return information void means main returns no information For now, mimic main 's first line –Left brace begins body of method declaration Ended by right brace } (line 11) 7 public static void main( String args[] ) 8 {

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class A Simple Program: Printing a Line of Text –Instructs computer to perform an action Prints string of characters –String - series characters inside double quotes White-spaces in strings are not ignored by compiler –System.out Standard output object Print to command window (i.e., MS-DOS prompt) –Method System.out.println Displays line of text Argument inside parenthesis –This line known as a statement Statements must end with semicolon ; 9 System.out.println( "Welcome to Java Programming!" );

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class A Simple Program: Printing a Line of Text –Ends method declaration –Ends class declaration –Can add comments to keep track of ending braces –Lines 8 and 9 could be rewritten as: –Remember, compiler ignores comments –Comments can start on same line after code 11 } // end method main 13 } // end class Welcome1

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 18 Compiling a Java program Compiling a program –OpenJCreator See website for instructions 001/software.htmhttp:// 001/software.htm – Create a new project, then a new java file/class and will save it. –Type the program and compile it –If no errors, Welcome1.class created Has bytecodes that represent application Bytecodes passed to Java interpreter –If no errors, we will run or execute our program to get the output.

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 19 Executing program.. Lets take a look how to do this using JCreator Executing a program –Type java Welcome1 Interpreter loads.class file for class Welcome1.class extension omitted from command –Interpreter calls method main

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 20 Lets Modify Our First Java Program Modify previous example to print same contents using different code

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 21 Modifying Our First Java Program Modifying programs –Welcome2.java produces same output as Welcome1.java (Fig. 2.1) –Using different code –Line 9 displays “Welcome to ” with cursor remaining on printed line –Line 10 displays “Java Programming! ” on same line with cursor on next line 9 System.out.print( "Welcome to " ); 10 System.out.println( "Java Programming!" );

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 22 Methods What is System.out.println() ? – It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message.

 2003 Prentice Hall, Inc. All rights reserved. Outline 23 Welcome2.java 1. Comments 2. Blank line 3. Begin class Welcome2 3.1 Method main 4. Method System.out.prin t 4.1 Method System.out.prin tln 5. end main, Welcome2 Welcome to Java Programming! 1 // Fig. 2.3: Welcome2.java 2 // Printing a line of text with multiple statements. 3 4 public class Welcome2 { 5 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.print( "Welcome to " ); 10 System.out.println( "Java Programming!" ); } // end method main } // end class Welcome2 System.out.print keeps the cursor on the same line, so System.out.println continues on the same line.

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class Modifying Our First Java Program Newline characters ( \n ) –Interpreted as “special characters” by methods System.out.print and System.out.println –Indicates cursor should be on next line –Welcome3.java (Fig. 2.4) –Line breaks at \n Usage –Can use in System.out.println or System.out.print to create new lines System.out.println( "Welcome\nto\nJava\nProgramming!" ); 9 System.out.println( "Welcome\nto\nJava\nProgramming!" );

 2003 Prentice Hall, Inc. All rights reserved. Outline 25 Welcome3.java 1. main 2. System.out.prin tln (uses \n for new line) Program Output 1 // Fig. 2.4: Welcome3.java 2 // Printing multiple lines of text with a single statement. 3 4 public class Welcome3 { 5 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.println( "Welcome\nto\nJava\nProgramming!" ); } // end method main } // end class Welcome3 Welcome to Java Programming! Notice how a new line is output for each \n escape sequence.

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class Modifying Our First Java Program Escape characters –Backslash ( \ ) –Indicates special characters be output

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 27 Find the Error Sample 1 1 Printing multiple lines of text with a single statement public class Welcome3 { 5 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.println( "Welcome\nto\nJava\nProgramming!" ); } // end method main } // end class Welcome3

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 28 Find the Error Sample 2 1 //Printing multiple lines of text with a single statement public Welcome3 { 5 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.println( "Welcome\nto\nJava\nProgramming!" ); } // end method main } // end class Welcome3

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 29 Find the Errors (7) 1 //Printing multiple lines of text with a single statement Public class 1firstprogram { 5 6 // main method begins execution of Java application 7 public static void main( String args[] 8 { 9 System.out.print( "Welcome\nto\nJava\nProgramming! ) } // end method main // end class Welcome3

 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 30 Anatomy of a Java Program Comments Reserved words Statements Blocks Classes Methods The main method