1 / 79

SD1420 Unit 1 – Chapter 1 Introduction to Computers, Programs, and Java

SD1420 Unit 1 – Chapter 1 Introduction to Computers, Programs, and Java. Chapter 2 Elementary Programming. Objectives. Upon completion of this unit, you should be able to do the following: Describe the relationship between Java and the World Wide Web

hang
Download Presentation

SD1420 Unit 1 – Chapter 1 Introduction to Computers, Programs, and Java

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. SD1420Unit 1 – Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Elementary Programming

  2. Objectives Upon completion of this unit, you should be able to do the following: • Describe the relationship between Java and the World Wide Web • Understand the meaning of Java Language Specification, API, JDK, and IDE • Create, compile, and run Java programs • Explain the anatomy of Java programs • Be familiar with Java programming style and documentation • Use identifiers to name variables, constants, methods, and classes • Explore Java numeric primitive data types: byte, short, int, long, float, and double • Write and evaluate numeric expressions • Represent characters using the char type • Represent a string using the String type • Obtain input using the JOptionPane input dialog boxes

  3. Programming Languages Machine language is a set of primitive instructions built into every computer. The instructions are in binary code, so you have to enter binary codes for various instructions. Programming with native machine language is a tedious process. The programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010

  4. Programming Languages Assembly languages were developed to make programming easy. Because the computer cannot understand assembly language, a program called assembler converts assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3 Assembly Source File Machine Code File … ADDF3 R1, R2, R3 … … 1101101010011010 … ASSEMBLER

  5. Programming Languages The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1415;

  6. Interpreting Source Code An interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, and then executes it right away, as shown in the following figure. Note that a statement from the source code may be translated into several machine instructions. Output Assembly Source File INTERPRETER area = 5 * 5 * 3.1415;

  7. Compiling Source Code A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed, as shown in the following figure. Output High-level Source File Machine Code File COMPILER … 1101101010011010 1111101001100010 … EXECUTOR area = 5 * 5 * 3.1415;

  8. Java, Web, and Beyond • Java can be used to develop Web applications. • Java Applets • Java Web Applications • Java can also be used to develop applications for hand-held devices such as Palm and cell phones

  9. Java’s History • James Gosling and Sun Microsystems • Oak • Java, May 20, 1995, Sun World • HotJava • The first Java-enabled Web browser

  10. JDK Versions • JDK 1.02 (1995) • JDK 1.1 (1996) • JDK 1.2 (1998) • JDK 1.3 (2000) • JDK 1.4 (2002) • JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 • JDK 1.6 (2006) a. k. a. JDK 6 or Java 6 • JDK 1.7 (2011) a. k. a. JDK 7 or Java 7 • JDK 1.8 (2014) a. k. a. JDK 8 or Java 8

  11. JDK Editions • Java Standard Edition (J2SE) • J2SE can be used to develop client-side standalone applications or applets. • Java Enterprise Edition (J2EE) • J2EE can be used to develop server-side applications such as Java servlets, Java ServerPages, and Java ServerFaces. • Java Micro Edition (J2ME). • J2ME can be used to develop applications for mobile devices such as cell phones.

  12. Software Development Process

  13. A Simple Java Program Listing 1.1 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  14. Creating and Editing Using Notepad To use NotePad: Type notepad Welcome.java from the DOS prompt. 1 2

  15. Creating and Editing Using WordPad To use WordPad, type writeWelcome.java from the DOS prompt. 1 2

  16. Creating and Editing Using Eclipse Example of “Hello World” created in Eclipse with output shown.

  17. Creating, Compiling, and Running Programs Create/Modify Source Code Saved on the disk Source code (developed by the programmer) Source Code public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Compile Source Code i.e. java Welcome java If compilation errors Stored on the disk Byte code (generated by the compiler for JVM to read and interpret, not for you to understand) Bytecode … Method Welcome() 0 aload_0 … Method void main(java.lang.String[]) 0 getstatic #2 … 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 … 8 return Run Bytecode i.e. java Welcome Result If runtime errors or incorrect result

  18. Compiling and Running Java from the Command Window • Set path to JDK bin directory • set path=c:\Program Files\java\jdk1.6.0\bin • Set classpath to include the current directory • set classpath=. • Compile • javac Welcome.java • Run • java Welcome

  19. Anatomy of a Java Program • Class name • Main method • Statements • Statement terminator • Reserved words • Comments • Blocks

  20. Class Name //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  21. Main Method //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  22. Statement //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  23. Statement Terminator //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  24. Reserved words //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  25. Blocks

  26. Special Symbols

  27. The showMessageDialog Method JOptionPane.showMessageDialog(null, 1. "Welcome to Java!", 2. "Display Message", 3. JOptionPane.INFORMATION_MESSAGE);

  28. The showMessageDialog Method JOptionPane.showMessageDialog(null, 1. "Welcome to Java!", 2. "Display Message", 3. JOptionPane.INFORMATION_MESSAGE); 1

  29. The showMessageDialog Method JOptionPane.showMessageDialog(null, 1. "Welcome to Java!", 2. "Display Message", 3. JOptionPane.INFORMATION_MESSAGE); 2 1

  30. The showMessageDialog Method JOptionPane.showMessageDialog(null, 1. "Welcome to Java!", 2. "Display Message", 3. JOptionPane.INFORMATION_MESSAGE); 2 1 3

  31. The showMessageDialog Method JOptionPane.showMessageDialog(null, 1. "Welcome to Java!", 2. "Display Message", 3. JOptionPane.INFORMATION_MESSAGE); 2 1 3

  32. Two Ways to Invoke the Method 1. Use a statement as shown in the example: JOptionPane.showMessageDialog(null, x, y, JOptionPane.INFORMATION_MESSAGE); where x is a string for the text to be displayed and y is a string for the title of the message dialog box. 2. Use a statement like this: JOptionPane.showMessageDialog(null, x); where x is a string for the text to be displayed.

  33. Programming Errors • Syntax Errors • Detected by the compiler • Runtime Errors • Causes the program to abort • Logic Errors • Produces incorrect result

  34. Debugging Debugger is a program that facilitates debugging. You can use a debugger to: • Execute a single statement at a time • Trace into or stepping over a method • Set breakpoints • Display variables • Display call stack • Modify variables

  35. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } Allocate memory for radius radius no value

  36. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } memory radius no value area no value Allocate memory for area

  37. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } Assign 20 for radius radius 20 area no value

  38. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } memory radius 20 area 1256.636 Compute area and assign it to variable area

  39. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } memory radius 20 area 1256.636 Print a message to the console

  40. Identifiers • An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). • An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. • An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). • An identifier cannot betrue, false, ornull. • An identifier can be any length.

  41. Variables // Compute the first area radius = 1.0; area = radius * radius * 3.14159; System.out.println("The area is “ + area + " for radius "+radius); // Compute the second area radius = 2.0; area = radius * radius * 3.14159; System.out.println("The area is “ + area + " for radius "+radius);

  42. Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a;

  43. Declaring and Initializing in One Step • int x = 1; • double d = 1.4;

  44. Named Constants final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3;

  45. Naming Conventions • Choose meaningful and descriptive names. • Variables and method names: • Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea use this convention.

  46. Numeric Operators

  47. Integer Division +, -, *, /, and % 5 / 2 yields an integer 2 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division)

  48. Remainder Operator

  49. Exponent Operations System.out.println(Math.pow(2, 3)); // Displays 8.0 System.out.println(Math.pow(4, 0.5)); // Displays 2.0 System.out.println(Math.pow(2.5, 2)); // Displays 6.25 System.out.println(Math.pow(2.5, -2)); // Displays 0.16

  50. Number Literals A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements: int i = 34; long x = 1000000; double d = 5.0;

More Related