1 / 44

Java Programming

Java Programming. By: A.Abbasi Omid Reza Nejati Omid.nejati.73@live.com Marvdasht Islamic Azad University. Java Environment. Getting Started. Install the necessary Java components and files:

tass
Download Presentation

Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming By: A.Abbasi Omid Reza Nejati Omid.nejati.73@live.com Marvdasht Islamic Azad University

  2. Java Environment

  3. Getting Started • 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. • NetBeansas one of the most popular IDEs (Interface Development Environments). NetBeans handles all the editing, creating, compiling and running for you.

  4. How Things Work in Java (cont’d)

  5. Starting a New Project in NetBeans • To start a new project, click on File > New Projectand do as the following:

  6. Main Function • 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.

  7. Write and Save Your Code • 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.

  8. Running Java Programs in NetBeans • There are various ways to run your program in NetBeans: • Press F6on your Keyboard. • Select RunMain Project from the Runmenu. • click the green arrow on the NetBeanstoolbar.

  9. Sharing Java Programs • 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 Runmenu 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 distand place all the files in there.

  10. Java Basics • Comments • Variables • Operators • Expressions, Statements and Blocks • Control Flow Statements • Arrays • Basic Input/Output

  11. Comments • 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 */

  12. Variables • 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 mustfirst be declared before they can be used. Java has the following syntax for declaring a variable: • Data-type Variable-name;

  13. Primitive Data Types • byte: an 8-bit signed two's complement integer (-128 .. +127) • short: a 16-bit signed two's complement integer (-32768 .. +32767) • int: a 32-bit signed two's complement integer (-231 .. 231-1) • long: a 64-bit two's complement integer (-263.. 263-1) • 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 (0 .. 65535) • String: Java supports character strings via java.lang.String

  14. Variable Names • 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 (-231 .. 231-1) • long: a 64-bit two's complement integer (-263.. 263-1) • 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 (0 .. 65535) • String: Java supports character strings via java.lang.String

  15. Literals • 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. 26 • - Hexadecimal (Base 16): e.g. 0x1a • - Binary (Base 2; available in Java SE 7 and later): e.g. 0b11010

  16. Literals (cont’d) • 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 efor scientific notation.

  17. Java Identifiers 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

  18. Java keywords 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.

  19. Java keywords

  20. Java Basic Operators

  21. Java Relational Operators

  22. Java Relational Operators

  23. Java Bitwise Operators

  24. Java Logical Operators

  25. 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: Java Conditional Operator (?:) public static void main(String args[]){ Inta,b; a =10; b =(a ==1) ? 20 : 30; System.out.println("Value of b is : "+ b ); b =(a ==10) ? 20 : 30; System.out.println("Value of b is : "+ b ); } } Value of b is:30 Value of b is:20

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

  27. The while 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.

  28. The do...while Loop A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time. Do { //Statements } while(Boolean_expression); 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.

  29. Example for do...while Loop This would produce the following result:

  30. The for loop 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 }

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

  32. The if Statement An if statement consists of a Boolean expression followed by one or more statements. The syntax of an if statement is: if(Boolean_expression) { //Statements will execute if the Boolean expression is true } 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.

  33. 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: The if...else Statement if (Boolean_expression){ //Executes when the Boolean expression is true } else{ //Executes when the Boolean expression is false } 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 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. }

  34. Example for if Loop This would produce the following result:

  35. The switch Statement 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 }

  36. Java Arrays 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];

  37. To import data You must import the java.util.Scanner class to declare and create instances of the Scanner class. Java input and output (I/O) 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);

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

  39. Program example 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; intsecond; 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);

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

  41. 3.Write a program to print even numbers between two numbers intx; inty; Scanner s= new Scanner( System.in ); System.out.print("Enter first number: "); x= s.nextInt(); System.out.print("Enter second number: "); y= s.nextInt(); intmax,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(inti=min+1;min<=max;i+=2){ System.out.println(i);}

  42. 4.Write a program to calculate the perimeter and area of ​​a circle intx; inty; 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

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

  44. The End

More Related