Title: COMP 14 Introduction to Programming
1COMP 14Introduction to Programming
- Basic Elements of Java, Part 1
- Jason Jerald
- Monday June 26, 2006
2Announcements
- Pledge
- If you have not handed in your pledge please do
so ASAP - New office hours
- Tues/Thurs 200-300
- Mon/Wed/Friday 1130-1230
- By appointment
- Assignment 1
- Some of you have not handed in yet
- This time I will only deduct 10 per day
- Assignment 2?
- Questions?
3Review Exercise
- Execution of c2ab in a computer
- Take the value of a
- Multiply it by 2
- Take the value of b
- Add b to the previous result
- Put final result into c
4Reading Check-Up
- The ________ rules of a language determine which
instructions are valid. - True or False? Hello! is an example of a legal
Java identifier. _______ - If an operator has an integer and a
floating-point operand, the result of the
operation is a ___________ number. - The expression (int) (9.2) evaluates to ___.
5Lecture Objectives
- Overview of Java
- Discover what a compiler is and what it does
- Examine how a Java program is processed
- Examine a Java program
- Java Syntax and Semantics
- Become familiar with the basic components of a
Java program, including methods, special symbols,
and identifiers. - Explore primitive data types.
- Discover how to use arithmetic operators.
- Examine how a program evaluates arithmetic
expressions. - Explore how mixed expressions are evaluated.
- Learn about type casting.
- Introduction to the String class
6Programming Languages
- Programming language
- a set of rules, symbols, and special words
- Yesterday we talked about
- machine language
- assembly language
- High-level languages make programming easier
- Closer to spoken languages
- Examples
- Basic
- FORTRAN
- COBOL
- C/C
- Java
7Java and Machine Language
- To run a Java program
- Java instructions need to be translated into an
intermediate language called bytecode. - The bytecode is interpreted into a particular
machine language.
8Compiler
- Compiler
- A program that translates a program written in a
high-level language into the equivalent machine
language. - In the case of Java, this machine language is the
bytecode. - Java Virtual Machine (JVM)
- A hypothetical computer developed to make Java
programs machine independent.
9Processing a Java Program
- Program
- written in Java using the Editor and compiled to
Bytecode using the Compiler. - Loader
- transfers the compiled code (bytecode) into main
memory and loads the necessary Libraries. - Interpreter
- reads and translates each bytecode instruction
into machine language and then executes it.
10A Java Program
- 1 public class DebuggerExample
- 2
- 3 public static void main(String args)
- 4
- 5 int sumOfTwoAndThree, sumOfSevenAndEight
- 6
- 7 System.out.println("A simple Java
program.") - 8
- 9 sumOfTwoAndThree 23
- 10 System.out.println("The sum of 2 and 3
" sumOfTwoAndThree) - 11
- 12 sumOfSevenAndEight 7 8
- 13 System.out.println("7 8 "
sumOfSevenAndEight) - 14
- 15
- Sample Run
- My first Java program.
- The sum of 2 and 3 5
11Debugger / Breakpoints
12Programming Languages
- Programming languages have rules of grammar just
as English does - syntax rules - which statements are legal and
which are not - semantic rules - determine the meaning of the
instructions - token - smallest individual unit of a program
- special symbols
- word symbols
- identifiers
13The Java Programming Language
- Java program A collection of classes
- There is a main method in every Java application
program - Token The smallest individual unit of a program
14Basic Java Syntax and Semantics
- Java Basics
- Tokens
- special symbols
- word symbols
- Identifiers
- data types
- boolean
- integer
- float-point
- Arithmetic operators and expressions
- , -, , /,
15Special Symbols
- / . ? , lt ! gt
16Word Symbolsaka reserved words, or keywords
- void
- public
- static
- throws
- return
- reserved words are always all lowercase
- each word symbol is considered to be a single
symbol - cannot be used for anything other than their
intended - purpose in a program
- shown in blue typewriter font in textbook
- full table in Appendix A
17Identifiers
- Names of things (variables, constants, methods)
in your programs - Can be composed of any combination of letters,
digits, underscore (_), and dollar sign () - Cannot begin with a digit
- May be any length
- Java is case-sensitive
- Total, total, and TOTAL are different identifiers
18Illegal Identifiers
19Questions
- Classify the following as legal or illegal
identifiers - My First Program ____
- my1stProgram ____
- 1stProgram ____
- money ____
- an_identifier ____
- Jane'sProgram ____
20Primitive Data TypesWhats A Data Type?
- A set of values and the operations that can be
performed on those values - Primitive data are fundamental values such as
numbers and characters - Operations are performed on primitive types using
built-in operators
21Primitive Data Types
- 8 primitive data types in Java
- 4 represent integers
- byte, short, int, long
- 2 represent floating point numbers
- float, double
- 1 represents characters
- char
- 1 represents boolean values
- boolean
22Primitive Data TypesBooleans
- Only two valid values
- true or false
- uses 1 bit for storage
- Represent any situation that has 2 states
- on - off
- true - false
- true and false are reserved words
23Primitive Data TypesNumeric Types
- The difference between the various numeric
primitive types is their size, and therefore the
values they can store
24Integers
- Examples -6728, -67, 0, 78, 36782
- Positive integers do not require a '' sign in
front of them (but they can) - No commas are used in an integer
- commas in Java are used to separate items in a
list
25Primitive Data TypesCharacters
- A char stores a single character from the Unicode
character set - an ordered list of characters, and each character
corresponds to a unique number - uses 16 bits per character, allowing for 65,536
unique characters - Character literals are delimited by single
quotes - 'a' 'X' '7' ' ' '' ',' '\n'
newline character (we'll discuss later)
26Floating-Point Data Types
- Two types of floating-point data types
- float
- double
- A double is more precise than a float but
- A double takes up more space
- A double calculation takes longer to computer
27Break
- Do something here to keep people from falling
asleep
28Arithmetic Expressions
- Expression - a combination of one or more
operands and their operators - Arithmetic expressions compute numeric results
and make use of the arithmetic operators - If either or both operands associated with an
arithmetic operator are floating point, the
result is a floating point
Addition Subtraction - Multiplication Div
ision / Modulus (Remainder)
29Division and Remainder
- If both operands to the division operator (/) are
integers, the result is an integer (the
fractional part is discarded) - The modulus, or remainder, operator () returns
the remainder after dividing the second operand
into the first (only works with integer types)
0
8 / 12 equals?
14 / 3 equals?
4
8
8 12 equals?
14 3 equals?
2
30Unary vs. Binary Operators
- Unary operators
- has only one operand
- example - (negative, not subtraction)
- -5
- Binary operators
- has two operands
- example - (subtraction)
- 5 - 3
31Operator Precedence
- Order in in which operators are evaluated
- multiplication, division, and remainder
- addition, subtraction
- arithmetic operators with the same precedence are
evaluated from left to right - Parentheses can be used to force the evaluation
order (just like in math)
32Operator Precedence
- What is the order of evaluation in the following
expressions?
a b c d e
a b c - d / e
1
4
3
2
3
2
4
1
a / (b c) - d e
2
3
4
1
a / (b (c (d - e)))
4
1
2
3
33Integral Expressions
- All operands are integers
- Result is an integer
- Examples
- 2 3 5
- 3 x y / 7
- x 2 (y z) 18
34Floating-point Expressions
- All operands are floating-point numbers
- Result is a floating-point
- Examples
- 12.8 17.5 34.50
- x 10.5 y - 16.2
- 7.0 / 3.5
35Mixed Expressions
- Integer operands yield an integer result
- Floating-point operands yield a floating-point
result - If both types of operands are present, the result
is a floating-point number - implicit type coercion
- Precedence rules are followed
- Examples
- 2 3.5 ________
- 6 / 4 3.9 ________________
2.0 3.5
1 3.9 1.0 3.9
36Type Conversion (Casting)
- Used to avoid implicit type coercion
- Syntax
- (dataTypeName) expression
- Expression evaluated first, then type converted
to dataTypeName - Examples
- (int) (7.9) (int)(6.7) 13
- (int) (7.9 6.7) 14
37QuestionsEvaluate These Expressions
9 6 3
- (5 4) 6
- (5 6) 3.5
- (double) (13) / 2
- (double) (13 / 2)
11 3.5 not possible
13.0 / 2 6.5
(double) (6) 6.0
38The class String
- The class String is used to manipulate strings
- String
- sequence of zero or more characters
- enclosed in double quotation marks
- null or empty strings have no characters
- numeric strings consist of integers or decimal
numbers - length is the number of characters in a string
- Examples
- "Hello World"
- "1234"
- "45.67"
- ""
39Strings
- Every character has a position in the string
(starting with 0) - "Hello World"
- The length of the string is the number of
characters in it - what's the length of "Hello World"?
0123456789...
11 (count the space)
40Parsing Numeric Strings
- In Java, input from the user often comes in the
form of a string - we need to know how to get the number values out
of the string - Numeric String
- a string with only integers or decimal numbers
- "6723", "-823", "345.76"
41Parsing Numeric Strings
- String to int
- Integer.parseInt(strExpression)
- Integer.parseInt("6723") 6723
- String to float
- Float.parseFloat(strExpression)
- Float.parseFloat("345.76") 345.76
- String to double
- Double.parseDouble(strExpression)
- Double.parseDouble("1234.56") 1234.56
42Summary
- Identifiers
- can be composed of any combination of letters,
digits, underscore (_), and dollar sign () - cannot begin with a digit
- Java is case-sensitive
- Data Types
- main integer type int
- main floating-point type double
- others char, boolean
43Summary
- Arithmetic Operators
- If one of the operands is floating-point, the
result is a floating-point - Can only use with two integer operands
- Casting
- (int) (54.9) - truncates the floating-point
number - (double) (23) - adds a .0 to the integer
addition subtraction - multiplication div
ision / remainder (mod)
44To do
- Homework 2 due Wednesday night
- Finish reading ch. 2 (pp. 64-99)
- Quiz tomorrow
- Covering chapters 1 and 2
- Write in your own words an overview (1/2 page)
of chapter 2 - Write a simple algorithm
- Basic Java syntax
- Other basic questions
- Questions?