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 Joy! Hi Jack!
~~~ Analog Sound Wave ~~~
Computer with us?
Hi Java! Hi Com!
I got it!
Hello Boys!
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 0 or 1
Black or White
More Colors for Red and Blue ? 00: Black
01: White
Two Bulbs 10: Red
11: Blue
0 or 1 0 or 1
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 ,
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!
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?
0 00 000, 001 0000, 0001, 0010, 0011
1 01 010, 011 0100, 0101, 0110, 0111
10 100, 101 1000, 1001, 1010, 1011
11 110, 111 1100, 1101, 1110, 1111
Binary Number System using Two (0 and 1) Digits!
Base-10 Decimal Number
The decimal number 5872 is interpreted as follows.
5000
800
70
+ 2
5872
= 5 x 103 + 8 x 102 + 7 x 101 + 2 x 100
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 x 22 + 1 x 21 + 1 x 20
= 1 x 8 + 0 x 4 + 1 x 2 +1 x 1
= 8 +0 +2 +1
= 11 (eleven, in decimal notation)
Binary Number System in Computer
Sequence of 0s and 1s
B. N. Bit 3 Bit 2 Bit 1 Bit 0 D. N.
0 0 0 0 0 0
1 0 0 0 1 1
10 0 0 1 0 2
100 0 1 0 0 4
1000 1 0 0 0 8
1011 1 0 1 1 11
1111 1 1 1 1 15
Bits for Text Symbols
Sound Processing with Computer
Audio files: mp3, wav
Analog Sound Wave to Digital Sound
Sampling and Quantizing
Digitizing Software
How to Store Alpha-Numeric Data?
Convert Data to Digital Format
Data Categorization:
Integer
Character
Real number
String
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
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
Real Number Representation in Computer
Mantissa and Exponent
0.10100111 x 2 5
Start with 0.10100111 Shift point 5 places to the right
10100.111 20 and ½+ ¼+⅛ (=⅞) 20. 875
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/2evaluates 4
156%10 evaluates 6
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)
Java Programming
Writing code
Compiling code (Interpreting it into machine code, bytecode)
Running code on Java Virtual Machine
source code byte code output
compile run
Structure of a Java Program
public class name { class: a program
public static void main(String[] args) {
statement;
statement; method: a named group
... of statements
statement;
}
} statement: a command to be executed
Every executable Java program consists of a class,
that contains a method named main,
that contains the statements (commands) to be executed.
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.
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
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
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 indentation for better readability and debugging
Semantic Error (Logic Error)
Found at program run-time by the virtual machine
Results in program failure
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
*/
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
Static methods
• Static method: A named group of statements. class
• denotes the structure of a program method A
• eliminates redundancy by code reuse
statement
statement
statement
• Procedural decomposition: method B
• dividing a problem into methods statement
statement
method C
• Writing a static method is like statement
adding a new command to Java. statement
statement
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.");
}
}
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();
}
Calling a Method
Syntax:
method_name();
Example:
printStop();
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.");
}
}
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!
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"?\\
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:
/ \ // \\ /// \\\
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("/ \\ // \\\\ /// \\\\\\");
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/