By: A.Abbasi Omid Reza Nejati Marvdasht Islamic Azad University.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 09 – Programming with Java Datatypes and Flow Control.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
CMT Programming Software Applications
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
UNIT II Decision Making And Branching Decision Making And Looping
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introducing Java.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
DAT602 Database Application Development Lecture 5 JAVA Review.
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Introduction to Java Java Translation Program Structure
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
By Mr. Muhammad Pervez Akhtar
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
IDENTIFIERS CSC 111.
Starting JavaProgramming
LRobot Game.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Introduction to Java Applications
Unit 3: Variables in Java
Presentation transcript:

By: A.Abbasi Omid Reza Nejati Marvdasht Islamic Azad University

Install the necessary Java components and files: Java Virtual Machine which is also known as Java Runtime Environment (JRE) is necessary to run any Java code. Java Software Development Kit is required to write your code and test it out. NetBeans as one of the most popular IDEs (Interface Development Environments). NetBeans handles all the editing, creating, compiling and running for you.

How Things Work in Java (cont’d)

To start a new project, click on File > New Project and do as the following:

The word "main" is the main entry point for executing your programs. Whenever a Java program starts, it looks for this name to execute any code within its curly brackets.

Add the following line to your main method as your first simple program: Now click File > Save, or File > Save All. Or click the Save icon on the NetBeans toolbar.

There are various ways to run your program in NetBeans: Press F6 on your Keyboard. Select Run Main Project from the Run menu. click the green arrow on the NetBeans toolbar.

You can send your programs to other people so that they can run them. To do that, you need to create a JAR file (Java ARchive). From the Run menu of NetBeans, select Clean and Build Main Project. When you do, NetBeans saves your work and then creates all the necessary files. It will create a folder called dist and place all the files in there.

Comments Variables Operators Expressions, Statements and Blocks Control Flow Statements Arrays Basic Input/Output

Comments are used to explain a special part of code, give some information about the program goal, the programmer, the code version, and etc. When a program is compiled, all the comments are ignored. Java supports single-line comments: //This is a single line comment as well as comments in multiple lines. The syntax is as the following: /* This is a comment spreading over two lines or more */

Programs work by manipulating data placed in memory. The data can be numbers, text, characters, and more besides. The data is given a name, so that it can be re- called whenever it is need. The name, and its value, is known as a Variable. Java is statically-typed, which means that all variables must first be declared before they can be used. Java has the following syntax for declaring a variable: Data-type Variable-name;

byte: an 8-bit signed two's complement integer ( ) short: a 16-bit signed two's complement integer ( ) int: a 32-bit signed two's complement integer ( ) long: a 64-bit two's complement integer ( ) float: a single-precision 32-bit IEEE 754 floating point double: a double-precision 64-bit IEEE 754 floating point boolean: true or false value char: a single 16-bit Unicode character ( ) String: Java supports character strings via java.lang.String

Variable names are case-sensitive. A variable's name can be any legal identifier: an unlimited- length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". int: a 32-bit signed two's complement integer ( ) long: a 64-bit two's complement integer ( ) float: a single-precision 32-bit IEEE 754 floating point double: a double-precision 64-bit IEEE 754 floating point boolean: true or false value char: a single 16-bit Unicode character ( ) String: Java supports character strings via java.lang.String

A literal is the source code representation of a fixed value. Integer literals: An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. Integer literals can be expressed by these number systems: - Decimal (Base 10): e.g Hexadecimal (Base 16): e.g. 0x1a - Binary (Base 2; available in Java SE 7 and later): e.g. 0b11010

Floating-Point Literals: A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d. The floating point types can also be expressed using E or e for scientific notation.

All Java components require names. Names used for classes, variables and methods are called identifiers. In Java, there are several points to remember about identifiers. They are as follows:  All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).  After the first character, identifiers can have any combination of characters.  A keyword cannot be used as an identifier.  Most importantly identifiers are case sensitive.  Examples of legal identifiers : age, $salary, _value, __1_value  Examples of illegal identifiers: 123abc, -salary

The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names.

abstractassertbooleanbreak bytecasecatchchar classconstcontinuedefault dodoubleelseenum extendsfinalfinallyfloat forgotoimplementsif importinstanceofinterfaceint longnativepackagenew privateprotectedpublicreturn shortstaticstrictfpsuper switchthissynchronizedthrow throwstransientvoidtry volatilewhile

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as: variable x =(expression)? value iftrue: value iffalse Following is the example: This would produce the following result: Value of b is:30 Value of b is:20

Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while Loop  for Loop

A while loop is a control structure that allows you to repeat a task a certain number of times. The syntax of a while loop is: while(Boolean_expression) { //Statements } When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true. Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Do { //Statements } while(Boolean_expression); A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested. If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false.

This would produce the following result:

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when you know how many times a task is to be repeated. The syntax of a for loop is: for(initialization ; Boolean_expression ; update) { //Statements }

There are two types of decision making statements in Java. They are:  if statements  switch statements

if(Boolean_expression) { //Statements will execute if the Boolean expression is true } An if statement consists of a Boolean expression followed by one or more statements. The syntax of an if statement is: If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If not, the first set of code after the end of the if statement(after the closing curly brace) will be executed.

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false The syntax of an if...else is: if (Boolean_expression){ //Executes when the Boolean expression is true } else{ //Executes when the Boolean expression is false } if(Boolean_expression1){ //Executes when the Boolean expression 1 is true } else if(Boolean_expression2){ //Executes when the Boolean expression 2 is true } else{ //Executes when the none of the above condition is true. } An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement. The syntax of an if...else if...else Statement

This would produce the following result:

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case The syntax of enhanced for loop is: switch(expression) { case value : //Statements break;//optional case value : //Statements break;//optional //You can have any number of case statements. default://Optional //Statements }

Creating Arrays: You can create an array by using the new operator with the following syntax: dataType [] arrayRefVar = new dataType [arraySize]; dataType [] arrayRefVar = {value0, value1,..., value n}; For example: double[] myList =new double[10]; Int [] my List= {5,6,4,8,9,3,12,45,10,20}; Or Data type [][][]…[] arrayRefVar= new dataType [arraySize] [arraySize]… [arraySize]; For example: Int [] [] x; X= new int [10][100];

To import data You must import the java.util.Scanner class to declare and create instances of the Scanner class. import java.util.Scanner; public class test { public static void main ( String [] args ) { scanner s= new Scanner( System.in ); For example: import java.util.Scanner; public class test { public static void main ( String [] args ) { Int n; scanner s= new Scanner( System.in ); n=s.nextint(); n=*2; System.out.println(“n*2=“+n);

We have used System.out.print(...) and System.out.println(...) statements for displaying simple text messages to the user. int x = 3; double rate = 5.5; boolean playing = true; String phrase = "The winner is "; System.out.print( "x = " + x); System.out.println( rate ); System.out.println( "playing = " + playing ); System.out.println( phrase + "Deb" ); they can all be printed using print or println as follows: For example, if the following variables are defined,

1.Write a program that receives two numbers and prints their sum does import java.util.Scanner; public class test{ public static void main(String[] args) { int first; int second; int sum; Scanner s= new Scanner( System.in ); System.out.print("Enter first number: "); first = s.nextInt(); System.out.print("Enter second number: "); second = s.nextInt(); sum=first+second; System.out.print(“sum= “+sum);

2.Write a program that receives two numbers and determine the maximum and minimum import java.util.Scanner; public class test{ public static void main(String[] args) { int m; int n; Scanner s= new Scanner( System.in ); System.out.print("Enter first number: "); m= s.nextInt(); System.out.print("Enter second number: "); n= s.nextInt(); If(m>n) System.out.print(“max=“+m,”min=“+n); else System.out.print(“max=“+n,”min=“+m);

3.Write a program to print even numbers between two numbers int x; int y; Scanner s= new Scanner( System.in ); System.out.print("Enter first number: "); x= s.nextInt(); System.out.print("Enter second number: "); y= s.nextInt(); int max,min; if(x>y){ max=x; min=y; } else{ max=y; min=x; } If(min%2==0){ for(int i=min;min<=max;i+=2){ System.out.println(i);} } else{ for(int i=min+1;min<=max;i+=2){ System.out.println(i);}

4.Write a program to calculate the perimeter and area of ​​ a circle int x; int y; Scanner s= new Scanner( System.in ); System.out.print("Enter radius of the circle: "); n= s.nextInt(); Final double p = 3.14; System.out.println(“area of circle=“+p*(n*n); System.out.println(“perimeter of circle=“+p*(n*n); Note: If you use the word final The variable remains constant and does not change during program

5. Write a program that will get a ten numbered and write the sum of number. int sum=0; int [] array; array = new int [10]; Scanner s= new Scanner( System.in ); for (i=1; i<=10 ; i++) { System.out.print(“enter number:”); array=s.nextInt(); sum=sum+array; } System.out.println(“sum=”+sum); Note: to input a list of number you must use loop