Download - CSCI 51 Introduction to Programming
![Page 1: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/1.jpg)
CSCI 51Introduction to Programming
Dr. Joshua StoughJanuary 27, 2009
![Page 2: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/2.jpg)
Announcements
•Problem Set 1 due Monday.•Building access request
![Page 3: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/3.jpg)
PS 1
• It may well serve you better to type the code yourself, even if it’s just copying code from another program.– That way, you make mistakes that
you know how to fix, and hopefully fewer mistakes later.
![Page 4: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/4.jpg)
Last Time in CSCI 51
• Parts of a Computer• Binary Numbers• Algorithms
• PS 1 assigned– due Monday, Feb 2
![Page 5: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/5.jpg)
Review, Hardware vs. Software
A computer is made up of hardware and software
Hardware Software• CPU
- ex: 1.8 GHz Core 2 Duo• input/output
- keyboard- 15.4” wide screen LCD- network card
• main memory- ex: 2 GB RAM
• secondary memory- ex: 160 GB hard drive
• operating systems- Windows XP- Mac OS X
• applications- games- Microsoft Word- Mozilla Firefox
![Page 6: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/6.jpg)
ReviewHardware Organization
motherboard
CPU
memory
hard drive
![Page 7: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/7.jpg)
ReviewMain Memory (with 100 cells)
Each memory cell has a numeric address, which uniquely identifies it
![Page 8: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/8.jpg)
QuestionsBinary Numbers
• What’s the maximum value a 6-bit number can represent?
• What’s the decimal representation of 111010?
• What’s the binary representation of 35?
63
58 = 32+16+8+2
100011
![Page 9: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/9.jpg)
KB 210 = 1024
MB 220 (over 1 million)
GB 230 (over 1 billion)
TB 240 (over 1 trillion)
Unit Symbol Number of Bytes
kilobyte
megabyte
gigabyte
terabyte
Storage Capacity
• Every memory device has a storage capacity, indicating the number of bytes (8 bits) it can hold
• Various units:
![Page 10: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/10.jpg)
Questions
1. - computer components including the CPU, main memory, I/O devices, and secondary storage
2. - the brain of the computer, containing the PC, IR, ALU, and ACC
3. - computer instructions to solve a problem
4. The digits 0 and 1 are calledor the shortened term
![Page 11: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/11.jpg)
From Java to Machine Language• Computers understand only 0 and 1 (machine
language)• Compiler translates source code into machine
code
• Java compiler translates source code (file ending in .java) into bytecode (file ending in .class)– bytecode is portable (not machine-specific)
• Java interpreter reads and executes bytecode– different Java interpreters for different types of CPUs
and operating systems (OS)• Intel/Windows, Motorola/Mac OS X, Intel/Linux
![Page 12: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/12.jpg)
Problem Solving• The purpose of writing a program is to have the
computer solve a problem
• But:– “Computers do not solve problems; they
implement solutions.” – Laurent Gasser
• The general steps in problem solving are:– understand the problem– dissect the problem into manageable pieces– design a solution– consider alternatives to the solution and refine it– implement the solution– test the solution and fix any problems that exist
![Page 13: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/13.jpg)
Algorithm
• Sequence of instructions used to carry out a task or solve a problem
• May be written in either English or pseudocode– outline of a program that could be
translated into actual code
• May need refinement as you work
Always write/think out your algorithm before you begin programming
![Page 14: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/14.jpg)
Problem-Analysis-Coding-Execution
most important stepwithoutcomputer
withcomputer
![Page 15: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/15.jpg)
Algorithm Design Example
Problem: Convert change in cents to number of half-dollars, quarters, dimes, nickels, and pennies to be returned.
Example: – given 646 cents– number of half-dollars: divide 646 by 50
• quotient is 12 (number of half-dollars)• remainder is 46 (change left over)
– number of quarters: divide 46 by 25• quotient is 1 (number of quarters)• remainder is 21 (change left over)
– number of dimes, nickels, pennies– result: 12 half-dollars, 1 quarter, 2 dimes, 0
nickels, 1 penny
![Page 16: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/16.jpg)
Resulting Algorithm
1. Get the change in cents2. Find the number of half-dollars3. Calculate the remaining change4. Find the number of quarters5. Calculate the remaining change6. Find the number of dimes7. Calculate the remaining change8. Find the number of nickels9. Calculate the remaining change10.The remaining change is the number of
pennies.
![Page 17: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/17.jpg)
ReviewBinary Numbers
• N bits to represent 2N values• N bits represent values 0 to 2N-1• Example: 5 bits
– 32 unique values (0-31)– 00000 = 0– 11111 = 31
24 23 22 21 20
16 + 8 + 4 + 2 + 1
![Page 18: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/18.jpg)
ReviewProblem Solving• Steps:
– understand the problem• how would you solve the problem (as a human,
without a computer)
– dissect the problem into manageable pieces– design a solution– consider alternatives to the solution and
refine it– implement the solution– test the solution and fix any problems that
exist
![Page 19: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/19.jpg)
Review Questions
1. What is the maximum decimal value that a 4-bit binary number can represent?
2. What is the binary number for 17?
3. Before the CPU can execute instructions, they must first be loaded from
4. What is the first and most important step in writing a program?
15
10001
main memory
analysis, or understanding the problem
![Page 20: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/20.jpg)
Today in CSCI 51
• Basic elements of a Java program – special symbols– identifiers– data types– operators– expressions– Strings
• Textbook Ref: Ch 2 and 3
![Page 21: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/21.jpg)
Reading Check-Up
1. The rules of a language determine which instructions are valid.
2. True or False? Hello! is an example of a legal Java identifier.
3. If an operator has an integer and a floating-point operand, the result of the operation is a number.
4. The expression (int) (9.2) evaluates to
syntax
False
floating-point
9
![Page 22: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/22.jpg)
Introduction
• Computer program: a sequence of statements to accomplish a task
• Programming: process of planning and creating a program
• Programming language: a set of rules, symbols, and special words
![Page 23: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/23.jpg)
Sample Java Program
public class Hello {public static void main (String[] args){
System.out.println ("Hi There!");}
}
Upon execution, this program displaysHi There!
![Page 24: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/24.jpg)
Programming 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
![Page 25: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/25.jpg)
Special Symbols
+ - * /
. ; ? ,
<= != == >=
![Page 26: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/26.jpg)
Word Symbolsaka reserved words, or keywords• int• float• double• char
• 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 intendedpurpose in a program
![Page 27: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/27.jpg)
Identifiers
• 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
![Page 28: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/28.jpg)
Illegal Identifiers
![Page 29: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/29.jpg)
Questions
Classify the following as legal or illegal identifiers:
1. My First Program2. my1stProgram3. 1stProgram4. $money5. an_identifier6. Jane'sProgram
illegal
legal
illegal
legal
legal
illegal
![Page 30: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/30.jpg)
Primitive Data TypesWhat’s 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
![Page 31: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/31.jpg)
Primitive 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
![Page 32: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/32.jpg)
Primitive Data TypesNumeric Types
• The difference between the various numeric primitive types is their size, and therefore the values they can store:
Typebyteshortintlong
floatdouble
Storage8 bits16 bits32 bits64 bits
32 bits64 bits
Min Value-128-32,768-2,147,483,648< -9 x 1018
+/- 3.4 x 1038 with 7 significant digits+/- 1.7 x 10308 with 15 significant digits
Max Value12732,7672,147,483,647> 9 x 1018
![Page 33: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/33.jpg)
Integers
• Examples: -6728, -67, 0, 78, 36782
• Positive integers do not have 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
![Page 34: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/34.jpg)
Primitive 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)
![Page 35: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/35.jpg)
Primitive 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
![Page 36: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/36.jpg)
• 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 *Division /Remainder %
Arithmetic Expressions
![Page 37: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/37.jpg)
• If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded)
• The remainder, or modulus, operator (%) returns the remainder after dividing the second operand into the first (only works with integer types)
14 / 3 equals?
8 / 12 equals?
4
0
14 % 3 equals?
8 % 12 equals?
2
8
Division and Remainder
![Page 38: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/38.jpg)
Unary vs. Binary Operators• Unary operators
– has only one operand– example: - (negative, not subtraction)-5
• Binary operators– has two operands– example: - (subtraction)5 - 3
![Page 39: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/39.jpg)
Operator Precedence
• Determines the order in which operators are evaluated:1. multiplication, division, and remainder 2. addition, subtraction, and string
concatenation3. 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)
![Page 40: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/40.jpg)
Operator Precedence
• What is the order of evaluation in the following expressions?
a + b + c + d + e1 432
a + b * c - d / e3 241
a / (b + c) - d % e2 341
a / (b * (c + (d - e)))4 123
![Page 41: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/41.jpg)
Integral Expressions
• All operands are integers• Result is an integer
• Examples:2 + 3 * 53 + x – y / 7x + 2 * (y – z) + 18
![Page 42: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/42.jpg)
Floating-point Expressions• All operands are floating-point numbers• Result is a floating-point
• Examples:12.8 * 17.5 – 34.50x * 10.5 + y - 16.2
7.0 / 3.5
![Page 43: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/43.jpg)
Mixed Expressions
• Operands of different types• Examples:
2 + 3.56 / 4 + 3.9
• 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
2.0 + 3.5
1 + 3.9 1.0 + 3.9
![Page 44: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/44.jpg)
Type Conversion (Casting)• Used to avoid implicit type
coercion• Syntax(dataTypeName) expression
• Expression evaluated first, then type converted to dataTypeName
• Examples:(int) (7.9 + 6.7) = 14(int) (7.9) + (int)(6.7) = 13
![Page 45: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/45.jpg)
QuestionsEvaluate These Expressions
1. (5 + 4) % 6
2. (5 + 6) % 3.5
3. (double) (13) / 2
4. (double) (13 / 2)
9 % 63
11 % 3.5not possible
(double) (6)6.0
13.0 / 26.5
![Page 46: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/46.jpg)
The class String• 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
• The class String is used to manipulate strings• Examples:
– "Hello World"– "1234"– "45.67"– ""
![Page 47: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/47.jpg)
Strings
• 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)
![Page 48: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/48.jpg)
Parsing Numeric Strings• In Java, input from the user 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"
![Page 49: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/49.jpg)
Parsing Numeric Strings• We will use Scanner and nextInt, next,
nextDouble, etc.• String to intInteger.parseInt(strExpression)Integer.parseInt("6723") 6723
• String to floatFloat.parseFloat(strExpression)Float.parseFloat("345.76") 345.76
• String to doubleDouble.parseDouble(strExpression)Double.parseDouble("1234.56") 1234.56
![Page 50: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/50.jpg)
Summary
• 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
![Page 51: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/51.jpg)
• 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 *division /remainder (mod) %
Summary
![Page 52: CSCI 51 Introduction to Programming](https://reader035.vdocuments.us/reader035/viewer/2022081513/56813116550346895d976ee8/html5/thumbnails/52.jpg)
Next Time in CSCI 51
• Wednesday– input, variables,
increment/decrement, Strings and '+' operator
– Remember, you’re responsible for Ch. 2,3