Más contenido relacionado


java 1 new.pdf

  1. Sulaimani Polytechnic University TechnicalCollegeof Informatics ComputerNetworksDepartment By : Msc. Fariaa Abdulmajed
  2. Introduction:- Programming and programminglanguage Programming: is the technique of converting the idea of solving a specified problem into a logical related sequence of statements using a programming language to obtain a required goal. Programming language is a set of rules and symbols used to construct a computer program
  3. Programming Languages Programming languages can be classified according to two criteria: ▪ Purpose of the Languages: This means, the objective that this language is produced to. ▪ Level of the Language: This mean, the association level of the language from the machine (microprocessor) or from the user (Human).
  4. LanguagePurpose
  5. Language Level
  6. Compiler and Interpreter • A program written in a high-level language is called a source program or source code. • A computer cannot execute 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.
  7. Structureof Java programs public class <name> { public static void main(String[] args) { <statement(s)>; } } • Every executable Java program consists of a class... • that contains a method named main... • that contains the statements to be executed
  8. Simple First Program: "Hello World" public class Hello { public static void main(String[] args) { // Program execution begins here System.out.println("Hello world."); } }
  9. 9 public // Accessible outside this file. "public" is a reserved word. class // "class" is a reservedword. Hello// The name of the class - same name as filename. { // Braces instead of begin/end. public // This function must be accessible. "public" is reserved. static // "static"is reserved. void // The return value - nothing. "void" is reserved. main // To start execution, a class must have a "main" function. (String[] args) System.out.println ("Hello World!"); // useful function called println, which takes in a string and prints it out on a new line. SimpleFirstProgram:"HelloWorld"
  10. Important Notes ▪Filenames must end with .java in lowercase letters. ▪Filenames must match their class names (more info on this soon). ▪Filenames cannot contain spaces. ▪Filenames are case sensitive: vs. vs. ▪Filenames consist only of letters (A-Z, a-z), digits (0-9), underscores (_), dashes (-) and periods (.).
  11. System.out.println • Java programs use a statement called System.out.println to instruct the computer to print a line of output on the console • Two ways to use System.out.println : 1. System.out.println("<Message>"); • System.out.println (“I love JAVA");. 2. System.out.println(mathematical computations); • System.out.println ((3*4)+7-8);
  12. AnotherJavaprogram The code in this program instructs the computer to print four messages on the screen. public class Hello2 { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } }
  13. 13 Program Development Process Text editor Source code (.java) Saves Java statements Java compiler Byte code (.class) Produces Java Virtual Machine Program Execution Results in
  14. • All Java source files must end with the .java extension • Those source files are then compiled into .class files by the javac compiler. • it instead contains bytecodes — the machine language of the Java Virtual Machine (Java VM). • The java launcher tool then runs your application with an instance of the Java Virtual Machine. An overview of the software development process
  15. • the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows,Linux, or Mac OS. Through the Java VM, the same application is capable of running on multiple platforms
  16. syntaxerrors & Runtime errors • syntax error or compiler error: A problem in the structureof a program that causes the compiler to fail. • If you type yourJava program incorrectly, you may violate Java's syntax and see a syntax error. public class Hello { pooblic static void main(String[] args) { System.owt.println("Hello, world!")_ } } <identifier> expected pooblic static void main(String[] args) { ^ ';' expected } ^ 2 errors Tool completed with exit code 1 compiler output:
  17. Runtime errors are errors that cause a program to terminate unusually. WHEN we get Runtime errors!!! 1- if the program is running on the environment detects an operation that is impossible to carry out. 2- If Input mistakes typically cause runtime errors Ex : you have to read in a number, but you input string, this causes data-type errors to occur in the program. 3- division by zero System.out.println (12/0);
  18. Comments •Comments in Java can be one of two styles: –Single line •starts at //anywhere on a line •ends at the end of that line –Multi-line •starts with character sequence /*anywhere •ends with character sequence */anywhere after that •can span multiple lines
  19. Escapesequences • Escape sequences,or escape characters, begin with a slash and are immediately followed by another character. • This two-character sequence,inside ""allows you to control your output (n, t, b)
  20. lab work
  21. 1- write a program to execute “Hello, World! ” using TextPad Step 1: Launch TextPad editor as demonstrated as follows:
  22. Step 2: Write any sample Java program in the editor tool as demonstrated as follows: public class Hello { public static void main(String [] arg) { // Program execution begins here System.out.println("Hello world."); } }
  23. Step 3: Select ‘TextPad tools compile java’ choice from the Tools Menu – > External Tools Sub Menu (or you can likewise use Ctrl+1 alternate way key
  24. Step 4: Ensure that the ‘Sava As’ dialog is shown. In ‘Save As’ dialog, guarantee that ‘Save as Type’ is chosen as All Files, type ‘’ into the ‘File Name’ field and tap on ‘Save’ button as demonstrated as follows: Hello Hello
  25. Step 5: Repeat Step 3 to compile the saved Java document Step 6: Select ‘Run Java Application’ alternative from the Tools Menu – > External Tools Sub Menu (or you can likewise use Ctrl+2 easy shortcut key) Step 7: Observe that the outcome of the above composed Java program is shown in the command window
  26. 2- what is the output of the following program and why public class Ex1 { public static void main ( String[] args ) { System.out.println("HellontWorldn!" ); } }
  27. 3) Write a java program to print your name . For example you get this output 4) Write a java program to output the following message.
  28. 5- what is the output of the following program public class Ex1 { public static void main ( String[] args ) { System.out.println(12/4*(6+12)-8); } }