1 / 339

King Fahd University of Petroleum & Minerals College of Computer Science & Engineering

King Fahd University of Petroleum & Minerals College of Computer Science & Engineering. Information & Computer Science Department. ICS102 - Introduction to computing. Lecture 1 : Introduction. Components of a Personal Computer. Questions : what are the input devices ?

Download Presentation

King Fahd University of Petroleum & Minerals College of Computer Science & Engineering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department ICS102 - Introduction to computing Lecture 1 : Introduction

  2. Components of a Personal Computer • Questions : • what are the input devices ? • what are the output devices ?

  3. Motherboard A motherboard,provides the electrical connections by which the other components of the system communicate and hosts the central processing unit as well as other subsystems and devices.

  4. Motherboard example : Acer E360

  5. CPU (Central Processing Unit) The Central Processing Unit (CPU) or processor is the portion of a computer system that carries out the instructions of a computer program and is the primary element carrying out the computer's functions. Example: (3 + 2) = 5 In an addition operation, the arithmetic logic unit (ALU) will be connected to a set of inputs and a set of outputs. The inputs provide the numbers to be added, and the outputs will contain the final sum. 3 5 2

  6. CPU memory Registers Cache Main memory (RAM): Data has to be in main memory so that CPU can access it Volatile: lost when program exits; computer shuts off Hard Disk, CD, etc. Persistent This is where you keep the data for long-term storage Memory  Memory refers to computercomponents, devices, and recording mediathat hold digital dataused for computing for some interval of time.  There are mainly three types of memory :

  7. Main Memory … Cell Address Memory is divided into Many memory locations (cells) 1002 1003 1004 Each memory cell has a numeric address, which uniquely identifies it 1005 Each cell contains a data value, e.g. 22

  8. Main Memory …

  9. Main Memory … The word “Hello.” stored in 6 memory cells

  10. Memory units : Bits and Bytes • Bit -- most basic unit of memory • 1 or 0, on or off • 1 Byte = 8 bits • In a computer, data values are stored as a sequence of bits 1004 2 1004 00000010 1005 1005 7 00000111

  11. Program / CPU / Memory Interaction Example: Input read a number from keyboard Add 1 to it Output it on screen CPU RAM 1 RAM 2 2 2 1 1 Monitor Keyboard

  12. What is programming?

  13. What is a program? A (software) program is a list of instructions intended to a computer The list must be ordered correctly A program has inputs and outputs Each instruction tells the computer to do something (an action, a calculation, a comparison)

  14. Program Execution A program tells the CPU how to manipulate and/or move information Programming is about processing information Take some input, manipulate it in some way, and produce a particular output Program Manipulation Inputs Outputs

  15. Example 1 : Recipe for Scrambled Eggs Ingredients (Inputs) : two eggs, tablespoon of oil, salt Instructions (program): Add oil to pan Heat pan on stove Crack eggs into pan Add salt Mix until light and flakey Output: scrambled eggs

  16. Example 2 : Currency Exchange Input: Amount Source Currency Desired Currency Instructions Look up in table current exchange rate for the selected currencies Calculate result as amount * exchange rate • Output: result Task : convert an amount of money in some currency (e.g. US Dollars) to another one (e.g. Saudi Riyals).

  17. Programming language A programming language is the language used to write programs A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program statements A programming language has a syntax and semantics There are several types of languages (functional, Object-Oriented, etc.) In this course we focus on Java programming language.

  18. Java • Javais a programming languageoriginally developed by James Goslingat Sun Microsystems • It was first released in 1995. • The language derives much of its syntaxfrom C and C++. • But has a simpler object modeland fewer low-level facilities than C and C++.

  19. Why Java ? Currently, Java is the most popular language in the world !

  20. Why Java ? • Simple • Get started quickly • Concise • Write less code • Object-oriented • Better quality code • Portable • Architecture neutral (write once run anywhere) • Secure • More appropriate for Internet

  21. Portability of Java Classical model : Java model :

  22. Portability of Java Write once, run anywhere: Because applications written in the Java programming language are compiled into machine-independent bytecodes, they run consistently on any Java platform.

  23. A Java Program A Java program consists of one or more classes A Java class consists of one or more methods A Java method consists of one or more statements A Java Program Java classes Java Methods

  24. A Java Program A Java program resides in one or more files. The file name of a Java program has extension .java. One of the classes of a Java program is called the driver class. The name of the driver class must be the same as the name of its Java file. (Java is case sensitive. So EX1 is different from ex1.) The diver class must contain a method called main. The execution of Java program starts from the main method of the driver class.

  25. Example of a Java Program

  26. Example of a Java Program Class name Main method Class body Instruction

  27. Example of a Java Program Also notice: Curly braces { }

  28. Example of a Java Program Also notice: Curly braces { } Parentheses ( )

  29. Example of a Java Program Also notice: Square brackets [ ] Curly braces { } Parentheses ( )

  30. Example of a Java Program Also notice: A pair of braces { } define a block

  31. Compiling and running a program • Type in your program • Save the program • Store all your files in one directory for now • Give the program the same name as the class • Compile the program • this produces a .class file • Translates the program into something the computer can understand and execute (Java bytecode) • Run the program • Observe the result and adjust the program if necessary

  32. Public class Adven /* This program is an Arit */ public static void main /* Program statements g System.out.print(“Wel } } Text Editor Adventure.java Edit Public class /*

  33. Compile - With Errors Public class Adven /* This program is an Arit */ public static void main /* Program statements g System.print(“Welcome } } Compiler Adventure.java Errors and Warnings ------------------- Error : The method print( String) is undefined for Type System

  34. Compile - Success Public class Adven /* This program is an Arit */ public static void main /* Program statements g System.out.print(“Wel } } 001011010001011101 1011010001011101110 1010001011101110101101000101110111010110100010111011101011010001011101110101101000101110111000101110111010110100010111011100101110111010110100010111011100101110111010110100010111011100101110111010110100010111011100101110111010110100010111011100101110 Compiler Adventure.java Adventure.class

  35. Fred 1 Welcome to the Arithm The date is Monday Sep What is your name?Fred Well Fred, after a day The cube appears to be You find a Green door, The door closes behind There is a feel of mat Adventure Run Program

  36. End

  37. King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department ICS102 Lecture 1 : Expressions and Assignment

  38. Outline Declaration Statements Primitive Data Types Identifiers Assignment Statements Arithmetic Operators and Expressions Precedence Rules Round-Off Errors in Floating-Point Numbers Integer and Floating-Point Division The % Operator Type Casting Increment and Decrement Operators December 20, 2019 ICS102: Expressions & Assignment 38

  39. Variable In mathematics: x + y = 17 In Computer Science: a variable is an identifier (usually a letter, word, or phrase) that is linked to a value stored in the computer's memory x = 5; variable variable variable December 20, 2019 ICS102: Expressions & Assignment 39

  40. Variable Declaration • Every variable in a Java program must be declared before it is used int numberOfBeans; numberOfBeans = 10; • A variable declaration tells the compiler what kind of data (type) will be stored in the variable • Basic types in Java are called primitive types

  41. - Primitive Data Types December 20, 2019 ICS102: Expressions & Assignment 41

  42. Names of variables (Identifiers) int variable = 3; The name of a variable must not start with a digit, and all the characters must be letters, digits, or the underscore symbol Java is a case-sensitive language: Rate, rate, and RATE are the names of three different variables It is not allowed to use reserved words as variable names: public class void static December 20, 2019 ICS102: Expressions & Assignment 42

  43. Assignment Statements In Java, the assignment statement is used to change the value of a variable An assignment statement consists of a variable on the left side of the operator, and an expression on the right side of the operator Variable = Expression; Example temperature = 98.6; count = numberOfBeans; December 20, 2019 ICS102: Expressions & Assignment 44

  44. Assignment Statements With Primitive Types When an assignment statement is executed, the expression is first evaluated, and then the variable on the left-hand side of the equal sign is set equal to the value of the expression distance = rate * time; Note that a variable can occur on both sides of the assignment operator count = count + 2; The assignment operator is automatically executed from right-to-left, so assignment statements can be chained number2 = number1 = 3; December 20, 2019 ICS102: Expressions & Assignment 45

  45. Initialize Variables … A variable that has been declared but that has not yet been given a value by some means is said to be uninitialized In certain cases an uninitialized variable is given a default value It is best not to rely on this and always initialize your variables int initialCount = 50; int finalCount; What is the value of initialCount? What is the value of finalCount? December 20, 2019 ICS102: Expressions & Assignment 46

  46. Shorthand Assignment Statements … e.g.  variable += 3; Shorthand assignment notation combines the assignment operator (=) and an arithmetic operator The general form is Variable Op = Expression December 20, 2019 ICS102: Expressions & Assignment 47

  47. Arithmetic Operators and Expressions These are : + (addition), - (subtraction), * (multiplication), /(division), and % (modulo, remainder) If an arithmetic operator is combined with intoperands, then the resulting type is int If an arithmetic operator is combined with one or two double operands, then the resulting type is double If different types are combined in an expression, then the resulting type is the right-most type on the following list that is found within the expression byteshortintlongfloatdouble Char December 20, 2019 ICS102: Expressions & Assignment 48

  48. Assignment Compatibility Is this legal ? int x = 2.99; And this ? double variable = 2; December 20, 2019 ICS102: Expressions & Assignment 49

  49. More generally, a value of any type in the following list can be assigned to a variable of any type that appears to the right of it byteshortintlongfloatdouble Note that as your move down the list from left to right, the range of allowed values for the types becomes larger int x = 5.8; error double y = 6; ok short i = 14.3; error float s = 9.2; ok Assignment Compatibility For these, you need type cast. next slide December 20, 2019 ICS102: Expressions & Assignment 50

  50. Type Casting A type cast takes a value of one type and produces a value of another type with an "equivalent" value int x = (int) 2.9; When type casting from a floating-point to an integer type, the number is truncated, not rounded: (int) 2.9 evaluates to 2, not 3 December 20, 2019 ICS102: Expressions & Assignment 51

More Related