Download presentation
Presentation is loading. Please wait.
1
OBJECT ORIENTED PROGRAMMING I LECTURE 2 GEORGE KOUTSOGIANNAKIS
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 2 GEORGE KOUTSOGIANNAKIS Copyright: Fall Illinois Institute of Technology- George Koutsogiannakis
2
NOTE MAKE SURE THAT YOU HAVE YOUR TEXT WITH YOU IN EVERY LECTURE!!
3
Basic Computer Concepts
Hardware Central Processing Unit Memory and Storage Devices Operating Systems Application Software Computer Networks and the Internet
4
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
5
A Typical Design of a Personal Computer
6
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
7
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)
8
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
9
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
10
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.
11
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:
12
Binary Equivalents of Decimal Numbers
Decimal Binary Equivalent
13
Powers of 2 Decimal Decimal , , , , , ,768
14
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 = 2485 = 2 * * * * 1 2485 = 2 * * * * 100
15
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).
16
BINARY TO DECIMAL CONVERSION -Example
Convert the binary number to decimal where the extreme right is the Least Significant binary number: 1*25+0*24+0*23+1*22+1*21+0*20 =0=38 3*101+8*100
17
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)
18
DECIMAL TO BINARY CONVERSION -Example
Therefore the binary number is from MSB to LSM: 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
19
Hexadecimal Numbers Base-16 number system (16 digits)
Uses digits 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.
20
Hexadecimal - Binary Equivalents
Hex Binary Decimal Hex Binary Decimal A B C D E F
21
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
22
Some Unicode Characters
Character Decimal Value Hex Value * A A B a b } D
23
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.
24
Programming Languages
Machine language Assembly language High-level languages
25
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
26
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 numbers-sequences 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).
27
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.
28
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.
29
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
30
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
31
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
32
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
33
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.
34
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.
35
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.
36
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.
37
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.
38
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
39
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
40
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.
41
Example public class FirstProgram 5 {
5 { 6 public static void main( String [] args ) 7 { System.out.println( "Programming is not " " a spectator sport!" ); System.exit( 0 ); 11 } 12 } Question: Identify the methods their return values their arguments and any method calls made in this program.
42
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."
43
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
44
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
45
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” );
46
Building Blocks - 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” ); }
47
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)
48
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
49
Examples Are these identifiers valid? taxRate char intNumber
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
50
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
51
Integer Types - Whole Numbers
Type Size Minimum Value Maximum Value in Bytes byte short , ,767 int , 147, 483, , 147, 483, 647 long ,223,372,036,854,775, ,223,372,036,854,775,807 Example declarations: int testGrade; int numPlayers, highScore, diceRoll; short xCoordinate, yCoordinate; byte ageInYears; long cityPopulation;
52
Text Study Guide Chapter 1 Pages 1-32 Chapter 2 Pages 41-46
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.