Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1: Introduction to Java Programming

Similar presentations


Presentation on theme: "Lecture 1: Introduction to Java Programming"— 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 ~~~ Analog Sound Wave ~~~
Our Communication Hi Joy! Hi Jack! ~~~ 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 Byte 1KB, 1MB, 1GB, 1TB, 1PB 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? 1 00 01 10 11 000, 001 010, 011 100, 101 110, 111 0000, 0001, 0010, 0011 0100, 0101, 0110, 0111 1000, 1001, 1010, 1011 1100, 1101, 1110, 1111 Binary Number System using Two (0 and 1) Digits!

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

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

12 Binary Number System in Computer
Sequence of 0s and 1s B. N. Bit 3 Bit 2 Bit 1 Bit 0 D. N. 1 10 2 100 4 1000 8 1011 11 1111 15

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: Special (Object) Type: String
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 1. 6 3 7 x 10 5 Start with 1.637 Shift point 5 places to the right 163700

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

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 Java Programming Writing code
Compiling code (Interpreting it into machine code, bytecode) Running code on Java Virtual Machine source code compile byte code run output

24 Structure of a Java Program
class: a program public class name { public static void main(String[] args) { statement; ... } Every executable Java program consists of a class, that contains a method named main, that contains the statements (commands) to be executed. method: a named group of statements statement: a command 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 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 static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

28 Programming Errors Syntax Error Semantic Error (Logic 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 indentation for 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 line or, /* 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 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 like adding a new command to Java. class method A statement method B method C

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). // 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 not a 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: \hello how 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 Windows x64 Offline Mac OS X Dr. Java (Simple Java Development Environment) Download Windows App Download Mac OS X App Eclipse Windows x64 : inst-win64.exe Mac OS X: NetBeans


Download ppt "Lecture 1: Introduction to Java Programming"

Similar presentations


Ads by Google