1 / 40

Lecture 1: Introduction to Java Programming

Lecture 1: Introduction to Java Programming. Dr. Kyung Eun Park Summer 2017. Computer and Me?. How to communicate with computer?. How does computer listen to? How does computer talk? How does computer think?. Our Communication. Hi Jack!. Hi Joy!. ~~~ Analog Sound Wave ~~~.

rosalyn
Télécharger la présentation

Lecture 1: Introduction to 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. Lecture 1: Introduction to Java Programming Dr. Kyung Eun Park Summer 2017

  2. Computer and Me?

  3. How to communicate with computer? • How does computer listen to? • How does computer talk? • How does computer think?

  4. Our Communication Hi Jack! Hi Joy! ~~~ Analog Sound Wave ~~~

  5. Computer with us? Hi Com! Hi Java! I got it! Hello Boys!

  6. Computer Knows Only 0s and 1s • In computer systems, data is stored and represented in binary digits, called bits. • A bit says two states: • On or Off • Open or Close • True or False • 0 or 1 • Black or White 0 or 1 • More Colors for Red and Blue? 00: Black 01: White 10: Red 11: Blue Two Bulbs 0 or 1 0 or 1

  7. Bit and Byte • Bit • Binary digit • 0 or 1 • 21 (=2) states cf) 22 (=4), 23 (=8) • Byte • 8-bit sequence • 28 (=256) states • 256 different colors • 256 different letters • 256 different cards • 1KB, 1MB, 1GB, 1TB, 1PB • 210 , 220 , 230 , 240 , 250 ,

  8. Our Number System: • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 : One blank  • 9 + 1? : Two blanks  10, 11, ~, 99 • Three blanks  100, 101, ~, 999 Decimal Number System using 10 Digits!

  9. Computer’s Number System: • 0, 1: One blank (bit)  • 2, 3? : No digit 2, 3 for a bit  Two bits • 4, 5, 6, 7  Three bits? 0000, 0001, 0010, 0011 0100, 0101, 0110, 0111 1000, 1001, 1010, 1011 1100, 1101, 1110, 1111 0 1 000, 001 010, 011 100, 101 110, 111 00 01 10 11 Binary Number System using Two (0 and 1) Digits!

  10. Base-10 Decimal Number • The decimal number 5872 is interpreted as follows. 5 0 0 0 8 0 0 7 0 + 2 5 8 7 2 x 102+ = x 103+ x 101 + x 100 5 8 7 2

  11. Base-2 Binary Number • Used in machine language (language that computers understand) • The binary notation 1011 is interpreted as follows 1 0 1 1 = 1 x 23 + 0 1 x 21 + 1 x 20 x 22 + = 1 x 8 + 0 1 x 2 + 1 x 1 x 4 + = 8 + 0 2 + 1 + = 11 (eleven, in decimal notation)

  12. Binary Number System in Computer • Sequence of 0s and 1s

  13. Bits for Text Symbols

  14. Sound Processing with Computer Audio files: mp3, wav

  15. Analog Sound Wave to Digital Sound • Sampling and Quantizing

  16. Digitizing Software

  17. How to Store Alpha-Numeric Data? • Convert Data to Digital Format • Data Categorization: • Integer • Character • Real number • String

  18. Java’s Types • Primitive Types: • Java supports 8 simple types for numbers, text, etc. • byte, short, int, long, float, double, char, & Boolean • Special (Object) Type: String

  19. Real Number Decomposition • Sign, Mantissa, and Expoent 5 10 x 1. 6 3 7 Shift point 5 places to the right Start with 1.637 163700

  20. Real Number Representation in Computer • Mantissa and Exponent 5 0.10100111 2 x Shift point 5 places to the right Start with 0.10100111 20 and ½+ ¼+⅛ (=⅞) 10100.111 20. 875

  21. Java’s Operators • Operators: • Combines multiple values • Simple Expression:Left_Operand Operator Right_Operand • + addition • - subtraction (or negation) • * multiplication • / division • % modulus (a.k.a. remainder) • Expression evaluation 1+1 evaluates 2 3*4 evaluates 12 (1+5)*5 evaluates 30 9/2 evaluates 4 156%10 evaluates 6

  22. Your First HelloWorld Program public class HelloWorld { 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"); } } • Save it as HelloWorld.java (case sensitive)

  23. output source code byte code compile run Java Programming • Writing code • Compiling code (Interpreting it into machine code, bytecode) • Running code on Java Virtual Machine

  24. class: a program statement: a command to be executed method: a named groupof statements Structure of a Java Program public class name { public static void main(String[] args) { statement; statement; ... statement; } } • Every executable Java program consists of a class, • that contains a method named main, • that contains the statements (commands) to be executed.

  25. System.out.println() • A statement that prints a line of output on the console. • pronounced "print-linn“ • Prints the given text message System.out.println("text"); • Prints a blank line of output System.out.println(); • Console: Text box into which the program's output is printed.

  26. Naming Conventions • User Defined Names: Identifiers • Program (Class) Name • Methods • Variables • Constants • Identifiers • Must start with a letter or _ (underscore) or $ • Construct using letters [A-Za-z], digits [0-9], and ‘_’ and ‘$’ symbols

  27. Keywords (Reserverd Words) • keyword: An identifier that you cannot use because it already has a reserved meaning in Java. abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface staticvoid char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

  28. Programming Errors • Syntax Error • Found at compile time by the compiler • Java language grammar • Every Java statement ends with a semicolon, ; • Contents of a class or a method enclosed between { and } * Program organizing tip: Use indentationfor better readability and debugging • Semantic Error (Logic Error) • Found at program run-time by the virtual machine • Results in program failure

  29. Comment for Your Program • A Good Programmer explains his/her program with comments as much as possible • Comments are neither compiled nor executed. • Syntax //comment text, on one lineor,/*comment text; may span multiple lines*/ • Example // This is a one-line comment. /* This is a very long * multi-line comment * generally for program header */

  30. Structured Programming w/ Static Methods • First design the logic solving your problem • Start writing a program class • Include a static main() method as the program entry • Declare static methods if necessary  modular programming rather than one single method program • Organize the main() method with multiple method invocations

  31. class method A • statement • statement • statement method B • statement • statement method C • statement • statement • statement Static methods • Static method: A named group of statements. • denotes the structure of a program • eliminates redundancy by code reuse • Procedural decomposition: • dividing a problem into methods • Writing a static method is likeadding a new command to Java.

  32. Algorithm Design for Baking Cookies Problem // This program displays a delicious recipe for baking cookies. public class BakeCookies1 { public static void main(String[] args) { // Step 1: Make the cake batter. System.out.println("Mix the dry ingredients."); System.out.println("Cream the butter and sugar."); System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients."); // Step 2a: Bake cookies (first batch). System.out.println("Set the oven temperature."); System.out.println("Set the timer."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); // Step 2b: Bake cookies (second batch). System.out.println("Set the oven temperature."); System.out.println("Set the timer."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); // Step 3: Decorate the cookies. System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); } }

  33. Declaring a Static Method • Syntax: public static void name() {statement;statement;...statement;} • Example: public static void printStop() {System.out.println(" ______");System.out.println(" / \\"); System.out.println("/ \\"); System.out.println("| STOP |"); System.out.println("\\ /"); System.out.println(" \\______/"); System.out.println(); }

  34. Calling a Method • Syntax: method_name(); • Example: printStop();

  35. Final Cookie Program // This program displays a delicious recipe for baking cookies. public class BakeCookies2 { public static void main(String[] args) { makeBatter(); bake(); // 1st batch bake(); // 2nd batch decorate(); } // Step 1: Make the cake batter. public static void makeBatter() { System.out.println("Mix the dry ingredients."); System.out.println("Cream the butter and sugar."); System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients."); } // Step 2: Bake a batch of cookies. public static void bake() { System.out.println("Set the oven temperature."); System.out.println("Set the timer."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); } // Step 3: Decorate the cookies. public static void decorate() { System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); } }

  36. String • String: A sequence of characters • Starts and ends with a " character " … " • Examples: "hello""This is a string. It's very long!" • Restrictions: • May not span multiple lines."This is nota legal String." • May not contain a " character."This is not a "legal" String either." • Use Escape Sequence!

  37. Escape Sequences • Escape sequence: A special sequence of characters used to represent certain special characters in a string \t tab character \n new line character \" quotation mark character \\ backslash character • Example: System.out.println("\\hello\nhow\tare \"you\"?\\\\"); • Output: \hellohow are "you"?\\

  38. Questions • What is the output of the following println statements? System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the downward spiral"); • Write a println statement to produce this output: / \ // \\ /// \\\

  39. Answers • Output of each println statement: a b c \\ ' """ C: in he downward spiral • println statement to produce the line of output: System.out.println("/ \\ // \\\\ /// \\\\\\");

  40. Setting Programming Environment • Install Java Language Library at Oracle.com • http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html • Windows x64 Offline • Mac OS X • Dr. Java (Simple Java Development Environment) • http://www.drjava.org/ • Download Windows App • Download Mac OS X App • Eclipse • Windows x64 : https://www.eclipse.org/downloads/download.php?file=/oomph/epp/neon/R3/eclipse-inst-win64.exe • Mac OS X: • NetBeans • https://netbeans.org/downloads/

More Related