Lecture2 - Illinois Institute of Technology

Download Report

Transcript Lecture2 - Illinois Institute of Technology

CS 115
OBJECT ORIENTED PROGRAMMING I
LECTURE 2
GEORGE KOUTSOGIANNAKIS
Copyright: Fall 2015 --Illinois Institute of TechnologyGeorge Koutsogiannakis
1
NOTE
• MAKE SURE THAT YOU HAVE YOUR TEXT WITH
YOU IN EVERY LECTURE!!
2
Basic Computer Concepts
• Hardware
– Central Processing Unit
– Memory and Storage Devices
• Operating Systems
• Application Software
• Computer Networks and the Internet
3
Typical Computer Hardware
• CPU
– executes the instructions of the program
• Hard disk and CD-ROM
– store instructions and data so program can be loaded into
memory and executed
• Main memory
– stores the program instructions and data while executing
• Keyboard and mouse
– used for data input
• Monitor
– used to display output from a program
• Other accessories, such as a printer
4
A Typical Design of a Personal
Computer
5
Central Processing Unit (CPU)
• Arithmetic Logic Unit
– performs integer arithmetic and logical operations
• Floating-point Unit
– performs floating-point operations
• Hardware registers
– store data and memory addresses
• Instruction Pointer
– keeps track of next instruction to execute
• Examples of CPUs:
– Intel Core 2 Duo™, AMD Turion™ 64, Sun Microsystems
SPARC, Hewlett-Packard PA-RISC, IBM POWER processor
6
CPU Instructions
•
•
•
•
Move data from one location to another
Perform a calculation
Compare data
Change the sequence of instructions to
execute (the flow of control)
7
Memory or Storage Devices
•
•
•
•
Memory consists of cells that hold one bit.
A bit's value can be 0 or 1
A byte is 8 binary digits (bits)
Storage capacity is expressed as:
– Kilobytes (1,024 bytes)
– Megabytes (1,048,576 bytes)
– Gigabytes (1,073,741,824 bytes
8
Operating System (OS) Software
• OS boots when computer is turned on, and runs
continuously
• Controls the peripheral devices (disks, keyboard, mouse,
etc.)
• Supports multitasking (multiple programs executing
simultaneously)
• Allocates memory to each program
• Prevents one program from damaging another program
• Controls the file system. Hard drive storage/file opening for
writing/ file opening for reading/closing of files/allocating
resources needed for file operations.
• OS Examples: Microsoft Windows, Linux
9
Application Software
• Written to perform specific tasks
• Runs "on top of" the operating system
• Examples: word processor, spreadsheet,
database management system, games,
Internet browser, etc.
10
Data Representation
• Computer data is stored in the form of binary numbers.
• Binary Numbers
– Expressed in base 2 system
• (two values are: 0 and 1)
• Hexadecimal Numbers
– Base-16 system used as shorthand for representing binary
numbers
• The Unicode Character Set
– Used to represent characters. Every character is represented by
a coded number.
– You can find out more about the coded numbers by going to
web site:
http://www.unicode.org/standard/WhatIsUnicode.html
11
Binary Equivalents of Decimal
Numbers
Decimal
0
1
2
3
4
5
6
7
8
Binary Equivalent
0000
0001
0010
0011
0100
0101
0110
0111
1000
12
Powers of 2
Decimal
20
1
21
2
22
4
23
8
24
16
25
32
26
64
27 128
28
29
210
211
212
213
214
215
Decimal
256
512
1,024
2,048
4,096
8,192
16,384
32,768
13
Decimal (base 10) numbers
A decimal number can be represented as the
sum of powers of 10 (the base) with
coefficients in the base 10 digits (0 - 9)
For example:
2485 = 2000 + 400 + 80 + 5
2485 = 2 * 1000 + 4 * 100 + 8 * 10 + 5 * 1
2485 = 2 * 103 + 4 * 102 + 8 * 101+ 5 * 100
14
Converting From Decimal to Binary
•
Just as a decimal number can be represented as a sum of
powers of 10 (the base) with coefficients in the base 10
alphabet (0 to 9) we can change the representation:
•
•
•
•
A number can also be represented as the sum of powers of 2
(the base of the binary system) with coefficients in the base 2
digits (0 and 1).
Since we change the base to 2 instead of 10 we will call that
representation of the number: a binary number
To make the conversion from a base of 10 top a base of 2 we
need an algorithm to do that.
We can also start with a base of 2 representation of a number
(a binary number) and then convert it back to a base of 10
(make it a decimal number).
15
BINARY TO DECIMAL CONVERSION
-Example
• Convert the binary number 100110 to decimal
where the extreme right is the Least
Significant binary number:
• 1*25+0*24+0*23+1*22+1*21+0*20
• 32+0+0+4+2=0=38
• 3*101+8*100
16
DECIMAL TO BINARY CONVERSION
-Example
• To convert 38 back to the binary we keep dividing by two and
keep the remainder:
• 38/2=19 remainder is 0 (LSB: least significant binary)
• 19/2=9 remainder is 1
• 9/2=4 remainder is 1
• 4/2=2 remainder is 0
• 2/2=1 remainder is 0
• ½=0 remainder is 1 (MSB : most significant binary)
17
DECIMAL TO BINARY CONVERSION
-Example
• Therefore the binary number is from MSB to LSM:
100110
• Notice that the representation is left to right where the MSB
is on the left.
1*25+0*24+0*23+1*22+1*21+0*20
18
Hexadecimal Numbers
• Base-16 number system (16 digits)
• Uses digits 0 - 9 and letters A – F for
remainder digits.
• One hexadecimal digit can express values from
0 to 15
– For example: C represents 12
• Thus, one hexadecimal digit can represent 4
bits in a binary system.
19
Hexadecimal - Binary Equivalents
Hex
0
1
2
3
4
5
6
7
Binary
0000
0001
0010
0011
0100
0101
0110
0111
Decimal Hex
0
8
1
9
2
A
3
B
4
C
5
D
6
E
7
F
Binary
1000
1001
1010
1011
1100
1101
1110
1111
Decimal
8
9
10
11
12
13
14
15
20
The Unicode Character Set
• Each character is stored as 16-bits
• Maximum number of characters that can be
represented: 65,536 (216)
• ASCII character set (used by many
programming languages) stores each
character as 7 bits (maximum number of
characters is 128)
• For compatibility, first 128 characters of
Unicode set represent the ASCII characters
21
Some Unicode Characters
Character
*
1
2
A
B
a
b
}
Decimal Value
42
49
50
65
66
97
98
125
Hex Value
002A
0031
0032
0041
0042
0061
0062
007D
22
Unicode system
• Every key on the computer’s keyboard
translates to a Unicode value.
• The computer system understands what you
typed from that translated Unicode value.
• Keep in mind that there are other symbols
that are not necessarily on the keyboard but
they have a Unicode value.
23
Programming Languages
• Machine language
• Assembly language
• High-level languages
24
High-Level Languages
•
•
•
•
Examples: Fortran, Perl, COBOL, C++, Java
Highly symbolic
Portable among CPU architectures
Languages can be designed for specific uses:
– Perl: Internet applications
– Fortran: scientific applications
– COBOL: business applications
25
A Java Program
• We write programming instructions on a text editor using
English words specific to the programming language (called
keywords) and other English words that represent categories
data (called identifiers). This is called the source code file
• The source code (Programming instructions) gets translated
by a compiler to coded instructions (coded binary numberssequences of 1s and 0s). The compiler produces a file called
the bytecodes file.
• The bytecodes file needs to be interpreted by the Java
Interpreter in order for the instructions to be executed by the
machine (the interpreter actually converts the coded java
instrcutions into machine instructions).
26
High-Level Languages
• Compiled- Some languages need only to compile the
source code.
– Compiler converts source code (instructions and data) into
machine language directly, then program is executed
– C language is such a language
• Interpreted- Some languages (like Java) need also the
interpeter.
– Interpreter converts instructions into machine language at
run time as instructions are executed
– Usually executes more slowly than compiled program
– Java needs both compilation and interpretation.
27
Java
• Therefore, Java uses a Combination of compiler and
interpreter
• Compiler converts source code into byte codes (an instruction
set for a virtual, machine-independent processor). The
instructions are intended for something called the JVM (Java
Virtual Machine).
• At run time, the Java Virtual Machine (JVM) interprets the
byte codes and converts them into the machine language for
the platform (CPU type and Operating System type)on which
the program is running.
28
The Java Language
• Created by Sun Microsystems in 1995
• Syntax is based on C++
• Object-oriented. This term will be explained
farther into the course.
• Supports Internet applications
• Provides an extensive library of prewritten classes
• Is portable among platforms because of the JVM
concept. The JVM is , however, specific to the
platform.
• Has built-in networking
29
Programming Basics
• Programming is translating a problem into
ordered steps consisting of operations a
computer can perform:
–
–
–
–
–
Input
Perform calculations
Compare values
Move data
Output
• The order of execution of instructions is called
flow of control
30
Four Types of Flow of Control
• Sequential Processing
– Execute instructions in order
• Method Call
– Jump to code in method, then return
• Selection
– Choose code to execute based on data value
• Looping or Iteration
– Repeat operations for multiple data values
31
Sequential Processing
• The pseudocode for calculating the sum of
two numbers would look like this:
read first number
read second number
set total to (first number +
second number)
output total
32
Parts of a Java Program
• The class (every Java program is inside a class)
• A class can have methods. A method is that
chunk of programming instructions usually
dedicated to a specific task (for example some
specific calculation)
• Instance variables of the class. Those are
specific data that the class needs to use.
33
What is a Class
• A class is an artificial (conceptual) representation of
the overall task that our Java program is supposed to
accomplish.
– We arbitrarily assign a name to that class
i.e.
public class CalculateMyExpenses
Don’t worry right now as to what public means.
The name of the class in the example is CalculateMyExpenses and
obviously it signifies that my program is intended to calculate
expenses.
The name is arbitrarily chosen.
34
Topics to be studied
• The next few slides present the highlights of
the topics that will be discussed in more detail
as the course progresses.
35
What is a Class
• Note: This is a first look at a what a class means to a
Java program. We will come back to that to give
more extensive definitions later on in the course!!!
• Therefore our Java program always starts by defining
a class name!!
• The overall task that the program has to accomplish
can be subdivided into smaller tasks.
36
Methods
• Each of the smaller tasks, within the overall task that our
program (class) has to accomplish , can be represented by a
group of programming instructions that can accomplish that
task. That group of instructions for that task constitutes what
is called a “method” in Java.
• One of the programming instructions within the group of
instructions that a method has, can be an instruction that calls
another method to execute its instructions (perform its task).
– That is called a “method call” type of instruction.
37
Method Call
• Calling the method executes the method
• Methods can take arguments (data to use) and
return values (return data as a result of the method’s
execution)
• Here is pseudocode for calculating the square root of
an integer:
read an integer
call the square root method, passing the
integer and receiving the square root
output the square root
38
Method Signature
• Here is an example of a method signature:
public return_type method_name (type1 name_of_type1, type2 name of type2)
Note: by the term signature we mean the way we have to write the method (the syntax that we
have to use)
– public indicates that the method can be called by any members of any
class.
– Followed that is the data type that the method returns to whoever
called it
i.e. int, String etc.
it could be void also in which case the method does not return
anything to the caller.
– Followed the data type is the name of the method
– Inside the parenthesis we find the arguments that method takes
39
Method Arguments
• Arguments are the data types and their values
that a caller of that method has to pass to the
method.
– Notice that there is no requirement to pass
arguments to a method. It could be empty of
arguments.
• A method can call another method from
within its code.
40
Example
public class FirstProgram
5 {
6
public static void main( String [] args )
7
{
8
System.out.println( "Programming is not "
9
+ " a spectator sport!" );
10
System.exit( 0 );
11
}
12 }
Question:
• Identify the methods their return values their arguments and any method
calls made in this program.
41
Selection
• The pseudo code for determining if a number is
positive or negative is:
read a number
if the number is greater than
or equal to 0
write "Number is positive."
else
write "Number is negative."
42
Looping
• The pseudocode for finding the sum of a set of
numbers is:
set total to 0
read a number
while there was a number to
read,
add number to total
read the next number
write total
43
Identifiers - Symbolic Names
• Identifiers are used to name classes, variables,
and methods
• Identifier Rules:
– Must start with a "Java letter"
• A - Z, a - z, _, $, and Unicode letters
– Can contain essentially any number of Java letters and
digits, but no spaces
– Case sensitive!!
• Number1 and number1 are different
– Cannot be keywords or reserved words
44
Program Building Blocks
The Statement
– Performs some action
– Terminates with a semicolon (;)
– Can span multiple lines
Example:
System.out.println( “Programming is ”
+ “not a spectator sport” );
45
Building Blocks - The Block
The Block
– 0, 1, or more statements
– Begins and ends with curly braces { }
– Can be used anywhere a statement is allowed
Example:
public static void main( String [] args )
{
System.out.println( “Hello” );
}
46
Building Blocks - White Space
• Space, tab, newline are white space characters
• At least one white space character is required
between a keyword and identifier
• Any amount of white space characters are
permitted between identifiers, keywords,
operators, and literals
Examples:
int a
1 + 2
public static void main( String []
args)
47
Data Types, Variables, and Constants
• For all data, assign a name (identifier) and a data
type
• Data type tells compiler:
– How much memory to allocate
– How to store the data
– The types of operations you will perform on the data
• Compiler monitors use of data
– Java is a strongly typed language
• Java eight primitive data types
byte, short, int, long, float, double, char, boolean
48
Examples
Are these identifiers valid?
taxRate
Yes.
char
No. char is a keyword
intNumber
Yes, The keyword int is embedded, but the identifier
is not identical to a keyword.
2008Taxrate
No. The identifier starts with a digit
49
Declaring Variables
• Variables hold one value at a time, but that value
can change
Syntax:
dataType identifier;
or
dataType identifier1, identifier2, …;
• Naming convention for variable names:
– first letter is lowercase
– embedded words begin with uppercase letter
50
Integer Types - Whole Numbers
Type
byte
short
int
long
Size
Minimum Value
in Bytes
1
-128
2
-32,768
4
-2, 147, 483, 648
8 -9,223,372,036,854,775,808
Maximum Value
127
32,767
2, 147, 483, 647
9,223,372,036,854,775,807
Example declarations:
int testGrade;
int numPlayers, highScore, diceRoll;
short xCoordinate, yCoordinate;
byte ageInYears;
long cityPopulation;
51