Introduction to programming in the Java programming language.

Slides:



Advertisements
Similar presentations
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Advertisements

The Web Warrior Guide to Web Design Technologies
IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Lecture 2 Introduction to C Programming
Your First Java Program: HelloWorld.java
Chapter 1: Introduction
Week 2 Recap CSE 115 – Spring Object Oriented Program System of objects that communicate with one another to solve some problem.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Outline Java program structure Basic program elements
01 Introduction1June Introduction CE : Fundamental Programming Techniques.
 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.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Introduction to C Programming
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Shorthand operators.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
A First Program Using C#
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Java Programming Language
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
Working with arrays (we will use an array of double as example)
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
CPS120: Introduction to Computer Science Introduction to C++
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CS 177 Recitation Week 1 – Intro to Java. Questions?
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
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.
Computer Programming Your First Java Program: HelloWorld.java.
Working with Java.
CSC201: Computer Programming
Chapter 1 Introduction to Computers, Programs, and Java
Chapter 3 GC 101 Java Fundamentals.
Chapter 2 - Introduction to C Programming
Introduction to.
CompSci 230 Software Construction
Chapter 2 - Introduction to C Programming
Intro to Java.
String Output ICS 111: Introduction to Computer Science I
Chapter 1: Computer Systems
Java Intro.
Anatomy of a Java Program
Presentation transcript:

Introduction to programming in the Java programming language

Similarity between a computer program and a book The structure of a computer program resembles the structure of a text document, like a book You all know what a book look like and I will use the structure of a book to illustrate the structure of a (Java) program

Similarity between a computer program and a book (cont.) Quick review of what a book look like: A book consists of a number of chapters Each chapter consists of a number of paragraphs Each paragraph consists of a number of sentences Each sentence must obey the syntax rules in the English language

Similarity between a computer program and a book (cont.) Quick overview of what a Java program look like: A Java program consists of a number of classes Each class consists of a number of methods (and some variables) Each method consists of a number of (program) statements Each statement must obey the syntax rules in the Java programming language

What a Java program look like The following picture shows what a Java program look like in its entirety: Each classes is stored in a separate (UNIX) file with the extension.java

What a Java program look like (cont.) The execution of a Java program begins with the method with the name main() (So: one of the methods in the Java program must be named main()...)

The first Java program The simplest Java program consists of 1 class and the class consists of 1 method called main. Example:

The first Java program (cont.) Remember that: Because you only have 1 method... so the method must be named main --- (in this case, you have no choice...) You must have the method main in every Java program

The first Java program (cont.) This Java program is written as follows: We will use this simple program to illustrate some concepts of the Java programming language

The first Java program (cont.) Example Program: (Demo above code) –Prog file: o.java How to run the program: Right click on link and save in a scratch directory To compile: javac Hello.java To run: java Hello

Statement: the smallest unit of execution in a Java program Statement: (You cannot execute a portion of a statement) A statement is like a sentence: it is the smallest unit that you write in a book. Statement = the smallest (unit) of execution in a Java program

Statement: the smallest unit of execution in a Java program (cont.) Examples of a statement: System.out.println("Hello Class");

Statement: the smallest unit of execution in a Java program (cont.) Effect of a statement: A statement tells the computer to do something Example: When the statement System.out.println("Hello Class"); is executed, the computer will print the text "Hello Class" to the terminal

Types of statements There are different kinds (types) of statements in Java We will discuss the details of each statement in Java later in the course

Syntax of statements Each type of statement follows a specific syntax We will discuss the details of the syntax of each type of statement in Java later in the course This is similar to the English language Each type of sentence in English has its own syntax Example: An ordinary sentence follows the syntax: subject verb A question (sentence) follows syntax: verb subject

Method: a container for (many) statements that perform a complex task Important facts: One statement can only perform a simple operation One statement is insufficient to solve a complex problem A complex task can be performed by many statements

Method: a container for (many) statements that perform a complex task (cont.) Method: For convenience, you can put a number of statement into a unit called a method The method is given a unique name to identify the group of statements.

Method: a container for (many) statements that perform a complex task (cont.) Example:

Defining a method Terminology: You must use a specify syntax to define a new method We will discuss a simplified syntax on how to define a method here and discuss the details on how to define methods later Define a method = constructing a method

Defining a method Simplified syntax used to define a method: MethodProperties METHOD_NAME(... ) { statements contained in the method }

Defining a method (cont.) Explanation: The MethodProperties describes the properties of the new method The METHOD_NAME is the name of the method (used for identification) The brackets (... ) contains parameters for the method

Defining a method (cont.) You have seen parameters before if you have used a TI-83 calculator: The X in the formula Y1 = 4sin(X) is a parameter BTW, sin(..) is a method !!!

Defining a method (cont.) Parameters provide information to the method Finally, the braces {..... } encloses the statements Example:

Method invocation: executing a method The computer can execute a method Computer jargon: When a method is invoked (executed), then: A method is a larger execution unit than a (single) statement Invoke a method = execute a method all statements contained between the braces {.... } are executed one statement at a time

Method invocation: executing a method (cont.) Example: Java program:

Method invocation: executing a method (cont.) When the Hello.java is run using the command: the computer will invoke the main() method java Hello

Method invocation: executing a method (cont.) The execution of the main() method will execute the statements contained in the main() method one at a time: System.out.println("Hello Class"); System.out.println(" How is... ");

Header and body of a method Computer Science jargon: Body of a method = the part of the method definition that is enclosed between the braces {... } Header of a method = the part of the method definition before the body of the method

Header and body of a method (cont.) Graphically explained:

Header and body of a method (cont.) Note When a method is executed, the statements in its body are executed

Keywords (or reserved words) Each programming language has reserved a number of words for some specific purpose Keyword: Keyword = a reserved word in a programming language

Keywords (or reserved words) (cont.) Examples: public, static and void

Keywords (or reserved words) (cont.) Keywords have special meaning in a programming language: A keyword must be used for that specified purpose

Keywords (or reserved words) (cont.) Example: A public method is a method that has the highest level of accessibility (Java allows you to define methods with more limited accessibility with the keyword private. You will learn about this topic much later - in another course)

Identifiers Rules to form the name of an identifier in Java: There are some rules in Java that you must follow to form the identifier name Identifier = a name chosen by the programmer to identify something defined inside a program

Identifiers (cont.) Identifier: An identifier consists of a number of characters There is no limit on the number of characters in the identifier (but do not try using identifiers that are too long because you will have to type it yourself...) The first character of an identifier must be one of the following: A letter (a, b,..., z, A, B,..., Z), or The underscore character

Identifiers (cont.) The subsequent characters of an identifier must be one of the following: Identifiers are case-sensitive ! You cannot use a keyword as identifier (keywords are reserved for a specific purpose !) A letter (a, b,..., z, A, B,..., Z), or The underscore character, or A digit (0, 1,..., 9)

Identifiers (cont.) Examples of correct identifiers: age Age (is different from age) greaterCommonDivisor R2D2 radius_of_the_circle

Identifiers (cont.) Examples of illegal identifiers: 3cpo (cannot start with a digit) radius-of-the-circle (cannot have minus sign in an identifier) public (cannot use a keyword !)

Class: container for methods Important facts: One method is used to perform one complex task In order to solve one problem, you may need to perform multiple complex tasks For each complex task, you will have to write one method to perform the task. The different methods have a common purpose It makes sense to collect methods that serve similar purpose together.

Class: container for methods (cont.) Class: For organizational purpose, you can put a number of methods into a unit called a class The class is given a unique name

Defining (constructing) a class You must use a specify syntax to define a new class Syntax to define a class: ClassProperties class CLASSNAME { methodDefinitions }

Defining (constructing) a class (cont.) Explanation: The ClassProperties describes the properties of the new class A word class is a keyword The use of this keyword tells Java that you want to define a new class The CLASSNAME is an identified used as the name of the new class The CLASSNAME must be unique (chosen by the programmer) The name of the class must be the same as the name of the file that contain the class

Defining (constructing) a class (cont.) Example

Block: grouping unit in Java Block: Example: Because this block is part of a class definition, it is called a class block Block = a pair of "{" and "}" braces that groups components in a Java program together

Block: grouping unit in Java (cont.) Blocks can be nested: one block can be placed inside another block Example: The outer block is a class block The inner block is the body of a method and it is called a method block

Block: grouping unit in Java (cont.) Notes: An opening brace "{" must be matched with a closing brace "}“ Programming tip: Whenever you type an opening brace "{", immediately type a closing brace "}" on the next line Go back and insert the rest of the program.

Java is case-sensitive Every letter in a Java program is case-sensitive Meaning: A lower-case letter and its corresponding upper-case letter are counted as different Example: public and Public are not the same. (You can use Public as an identifier, but not public !)

Comments Comment: Comment = text inside a Java program that is ignored by the Java compiler Comments are used to annotate the program to help humans understand the operation of the Java program

Comments (cont.) Syntax for a comment in Java: Single line comment syntax: The text on the line following the symbol // will be ignored //... single line comment

Comments (cont.) Multiple lines comment syntax: All text between the comment brackets /*..... */ will be ignored /* comment line 1 comment line */

Comments (cont.) Example:

Comments (cont.) Example Program: (Demo above code) The Hello prog file with comments: abus/03/Progs/Hello2.java How to run the program: Right click on link and save in a scratch directory To compile: javac Hello2.java To run: java Hello2

Java programs are format-free The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant All that the Java compiler cares about is syntactical correctness

Java programs are format-free (cont.) Consequence: You can make a Java program look very ugly and it may still be syntactically correct to the Java compiler

Java programs are format-free (cont.) Example: public class Hello2 { public static void main(String[] args) { System.out.println("Hello Class"); System.out.println(" How is everyone doing so far ?"); }

Java programs are format-free (cont.) Same program with many insignificant white spaces: public class Hello3 { public static void main (String[] args) { System.out.println ( "Hello Class"); System.out.println( " How is everyone doing so far ?"); }}

Java programs are format-free (cont.) (But... I had to call it Hello3 because the name Hello2 is already used)

Java programs are format-free (cont.) Example Program: (Try it out ! It will run and print the same thing) –Prog file: llo3.java How to run the program: Right click on link and save in a scratch directory To compile: javac Hello3.java To run: java Hello3

Java programs are format-free (cont.) Moral of this lesson: Do yourself a favor Don't abuse the format-free feature of Java Indent your programs properly so the structure of the algorithm is easily visible It will help you understand what the program is doing and find errors

Summary What a Java program look like abstractly:

Summary (cont.) What a Java program look like more concretely:

Summary (cont.) Keyword = a (English) word that is reserved for a special purpose Identifier = an (artificial) name made up by the programmer Each keyword has a special meaning in Java The (artificial) name is used to identify things defined in a java program

Summary (cont.) Statement = the smallest unit of execution in Java Method = contains multiple statements and identified by a method name which is an identifier Class = contains methods that serve a similar purpose Each class is identified by a class name which is an identifier When a method is invoked, all statements contained in the method are executed

Summary (cont.) Syntax to define a public class: public class CLASSNAME { (method definitions) }

Summary (cont.) Syntax to define the main method in Java: public static void main(String[] args) { (statements) }