IT 103 - Computer Programming 1 Module PDF

Title IT 103 - Computer Programming 1 Module
Course Introduction to programming
Institution King's College of the Philippines
Pages 25
File Size 1.3 MB
File Type PDF
Total Downloads 114
Total Views 162

Summary

Download IT 103 - Computer Programming 1 Module PDF


Description

MODULES IN IT102 (Computer Programming 1) TABLE OF CONTENTS Lesson Nos. 1

Topics An Overview of Computers and Programming Languages 1. Elements of a Computer System 2. Language of a Computer 3. Evolution of Programming Languages 4. Processing a Java Program 5. Internet, World Wide Web, Browser, and Java 6. Programming with Problem 7. Analysis, Coding, Execution Cycle 8. Programming Methodologies

2

Introduction to Java Programming 1. Basics of a Java Program 2. Data Types 3. Arithmetic Operators and Operator Precedence 4. Expressions 5. Type Conversion (Casting) 6. Input 7. Increment and Decrement Operators 8. Output, Packages, Classes, Methods, and the import Statement 9. Creating a Java Application Program 10. Debugging: Understanding and Fixing Syntax Errors 11. Programming Style and Form 12. Avoiding Bugs: Consistent, Proper Formatting and Code Walk-Through 13. More on Assignment Statements

3

Introduction to Object and Input/Output 1. Objects and Reference Variables 2. Using Predefined Classes and Methods in Program 3. Input/Output 4. File Input/Output

Ref References erences A. Books: MALIK D.S. 2012. Java Programming: From Problem Analysis to Program Design, Fifth Edition. Course Technology, Cengage Learning SCHILDT, H. 2012. Java: A Beginner’s Guide. USA. The McGraw-Hill Companies, INC. B. Online References: Oracle and/or its affiliates. 2015. The Java Tutorials. https://docs.oracle.com/javase/tutorial/ IDG Communications. 2017. Java World. http://www.javaworld.com/ Tutorialspoint. 2017. Learn Java Programming. www.tutorialspoint.com/java/ index.htm

MODULE I. Lesson 1. An Overvie Overview w of Computers and Progr Programming amming Languages Learning Outcomes: At the end of this lesson, you should have: 1. Enumerated and identified components of a computer systems and discussed how Central Processing Unit and Main Memory processes data; 2. Defined Machine Language and explained Binary Units; 3. Identified and enumerated Processing a Java Program and determined the significance of Java in Web development; 4. Analyzed problems and designed solution by applying basic problem solving; and 5. Identified the two basic approaches to programming design; I.

Key Concepts: Before starting this module, please watch first your orientation video on the path, Computer Programming 2/0_Computer Programing 1 Orientation. For every discussion of each topic, please refer to the different video files downloaded on your flash drives or visit http://tiny.cc/compprog1 for saved online videos. Please follow the different directories to access your video lectures. A. An Ove Overview rview of Computers and Progr Programming amming Languages Introduction. Computers have greatly affected our daily lives simply by helping us complete many tasks. Also, these programs or softwares are designed specifically for these task. Wherein these softwares were created with the use of programming languages 1. Elements of a Computer S System ystem Computers have two main components. The Hardware and the Softwa Software re re. Hardwar Hardwares es are tangible parts of a computer system or parts that you can touch like the CPU, Main memory, input and output devices, storages devices and etc.  CPU – this is a component where arithmetic and logical operations are carried out.  Main Memory – This is a hardware device that is directly connected to the CPU where all programs must be brought into it before being executed.  Secondary Storages – Provides a permanent storage for information like hard disks, flash memory, flash drives and etc.  Input devices – These are devices that feed the data and computer programs into computers. Ex. Keyboard, Mouse, Secondary Storages  Output devices – These are devices that computer uses to display results or outputs to end-users. Ex. Printers, Monitors

Softwares are intangible parts of a computer system or in the opposite of hardwares, you can’t touch these. But these softwares perform specific tasks. There are 2 types of softwares:  System Softwares – these are programs that control the computer such as Operating Systems. This Operating Systems monitors overall activity of the computer and provides services such as memory management, Input/Output (I/O) management, Storage Management and etc  Application Softwares – These are programs written using programming languages that perform a specific task. Ex. Word processors, Spreadsheets, games and etc. College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 3 of 26

2. Language of a Computer Machine Language is the most basic language of a computer wherein it is a sequence of 0 (zeros) and 1 (ones). Every computer understands its own machine language in term of bit. Bit – smallest size of data and is a binary digit either 0 or 1. Byte – is a sequence of 8 bits. Likewise follows the different data sizes: Unit Symbol Bits/Bytes Kilobyte KB 1024 bytes Megabyte MB 1024 KB = 1,048,576 bytes Gigabyte GB 1024 MB = 1,073,741,824 bytes Terabyte TB 1024 GB = 1,099,511,627,776 bytes Petabyte PB 1024 TB = 1,125,899,906,842,624 bytes Exabyte EB 1024 PB = 1,152,921,504,606,846,976 bytes Zettabyte ZB 1024 EB = 1,180,591,620,717,411,303,424 bytes 3. Evolution of Progr Programming amming Languages Early computers programmed in machine language. Assembly languages were developed to make programmer’s job easier. In assembly language, an instruction is an easy-to-remember form called a mnemonic. To make the assembly language understandable by the computer, an ASSEMBLER takes place to translate these assembly languages into machine language.

High- level languages make programming easier where these languages are closer to spoken languages. Examples: Basic, FORTRAN, COBOL, C/C++, Java High-level pro programming gramming Language comprised of a Compiler and a Virtual Machine. Compiler: this translates a program that are written in a high-level language into the equivalent machine language/bytecodes. Virtual Machine: these are hypothetical computer developed to make programs machine independent. 4. Processing a Jav Java a Progr Program am The following are the different actors considered on how a Java program is being processed. Source progr program: am: written in a high-level language Loader: transfers the compiled code (bytecode) into main memory Interpreter: reads and translates each bytecode instruction into machine language and then executes it. This image shows how a program runs: 1. From an editor, the program is being checked by the compiler for some errors (syntax). If the complier found syntactical errors, this will then be going back to the editor for correction. 2. Once the program passed on the compiler has no errors, it will then be converted into bytecodes to be passed on the loader. 3. In the loader, the program will then look into the different libr libraries aries used in the program to make it work. College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 4 of 26

4. And once all necessary libraries have been loaded, the interpreter will then interpret the entire program and display its output to the user.

5. Internet, W World orld Wide W Web eb eb,, Browser Browser,, and Java The Internet is an interconnection of networks that allows computers around the world to communicate with each other Wor orld ld Wide W Web eb (WWW), or Web, uses software programs that enable computer users to access documents and files (including images, audio, and video) on almost any subject over the Internet with the click of a mouse Browser or a Web Browser, are software programs that is being used to access the world wide web connecting to the internet. Jav Java a applets are programs that run from a Web Browser that makes the Web responsive and interactive. 6. Progr Programming amming with Problem In solving every programming problem, we follow a step-by-step problem solving process in which a solution is arrived at in a finite amount of time. This procedure is called Algorithm. 7. Problem Analysis, Coding, Ex Execution ecution Cycle In the problem-solving process, it follows the different phases: 1. Analyze the problem and outline the problem and its solution requirements 2. Design an algorithm to solve the problem 3. Implement the algorithm in a programming language, such as Java 4. Veri erify fy that the algorithm works 5. Maintain the program by using and improving it and modifying it if the problem domain changes In this image, this still follows how a program is being processed. However, an analysis

and algorithm design was being done before the actual coding (in the previous photo, the Editor). And on the part of the Interpreter, it is again evaluated for run time errors where these errors occur if there are errors within the logic of the program. 8. Progr Programming amming Methodologies There are two basic programming methodologies being used. a. Structured Design; and b. Object-oriented Design In a structured design, a problem is divided into smaller sub-problems. Theses sub-problems is being solved individually and all solutions are being combined to solve the problem as a whole.

College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 5 of 26

In an object-oriented design, the problem is divided into smaller sub-problems that are considered as Objects. These objects are interacting with each other that consist data and operations that then formulate a reusable an simpler solution.

College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 6 of 26

II.

Learning T Tasks: asks: A. Checking ffor or Understanding. These are just review questions that will enhance your comprehension on the different topics. Answer the following based on what was discussed on the different topics given. (Use the Back portion of this paper for your answers. And please write your name.) 1. Mark the following statements as true or false. a. Modern-day computers can accept spoken-word instructions, but cannot imitate human reasoning. b. In Unicode, every character is coded as a sequence of sixteen bits. c. The arithmetic operations are performed inside the CPU and, if an error is found, it outputs the logical errors. d. A sequence of 0s and 1s is called a decimal code. e. A Java compiler is a program that translates a Java program into bytecode. f. The CPU stands for command performing unit. g. RAM stands for readily available memory. h. A program written in a high-level programming language is called a source program. i. ZB stands for zero byte. j. The first step in the problem-solving process is to analyze the problem. 2. Name two input devices. 3. Name two output devices. 4. Why is secondary storage needed? 5. What is the function of an operating system? 6. What are the two types of programs? 7. What are the differences between machine languages and high-level languages? 8. What kind of errors are reported by a compiler? 9. Why do you need to translate a program written in a high-level language into machine language? 10. Why would you prefer to write a program in a high-level language rather than a machine language? 11. What are the advantages of problem analysis and algorithm design over directly writing a program in a high-level language?

College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 7 of 26

III.

Assessment: Assessment 1: Name: _______________________________________ Instruction: Convert the following data sizes to what is being required. Show your solutions and write your final answers on the blanks. 1. 2 GB – KB = _______________________ 2. 1 TB – MB = _______________________ 3. 2.5 MB – Bytes = _____________________ 4. 123456789 Bytes – GB = _______________________ 5. 1024 MB – TB = _______________________ 6. 8GB – MB = _______________________ 7. 0.256 TB – GB = _______________________ 8. 4096 MB – GB = _______________________ 9. 256 GB – TB = = _______________________ 10. 1 ZB – GB = _______________________

College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 8 of 26

Assessment 1: Name: _______________________________________ Instructions: By understanding the concept of an algorithm, create a step-by-step procedure on how to get the solution for the following scenarios. Use the back portion or a separate paper and attach them to this page. Example: Determine if a number is odd or even. Answer: a. Get number from user. b. Divide number by 2. c. Check if the remainder is equal to zero. d. If remainder is equal to zero, output THE NUMBER IS AN EVEN NUMBER. e. If remainder is not equal to zero, output THE NUMBER IS AN ODD NUMBER. 1. Determine if the entered age of 5 students are below 18 or not and display how many are minors. 2. Determine the absolute value of an integer. 3. Determine the grade of a student if they PASSED or FAILED a subject considering that 74.5 is the passing grade. 4. Determine if a person can be accepted in the military considering that their age must be above 21 years old and below 30, and their height must be 5’6” and above.

College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 9 of 26

Lesson 2. Introduction tto o Java Programming Learning Outcomes: At the end of this lesson, you should have: 1. Identified the basic components of a java program, including methods, special symbols, and identifiers; 2. Differentiated and applied primitive datatypes in a program applied arithmetic operators in a program and analyzed how program evaluates arithmetic expressions as well as using data types and applied casting; 3. Identified the parts of input process and applied in writing a program; 4. Differentiated increment from decrement operator and applied in a program; 5. Examined ways to output results using output statement in a program; 6. Determined contents of packages in java and applied in a program; 7. Determined how to create Java Application Program; 8. Identified to fix errors in a program and examined programs bugs using consistent and proper formatting; and 9. I.

Identified and applied assignment operators; Key Concepts: A. Intro Introduction duction to Jav Java a Programm Programming ing Computer progr programs ams are sequence of statements whose objective is to accomplish a task. Progr Programming amming is the process of planning and creating programs. 1. Basics of a Jav Java a Program A Java program is a collection of classes where it contains a main method for every application program. Java programs use different symbols as part of its general structure. Example

Also, java programs use words called Reserved Word or Keywords where these are words that were used that performs a specific task in the program. Example. int, float, char, double, void, public, static, throws, return, and etc. Java identifiers is a term used to letter or words used to name things. This usually consist of letters, digits, the underscore character (_) and the dollar sign ($). These identifiers however must begin with a letter, underscore or the dollar sign. Example: name, product_no, $greet, _START, num1 Also, illegal example of illegal identifiers are the following: Employee Salary – there can be NO SPACE between Employee and Salary Hello! – the exclamation mark (!) cannot be used in an identifier one+two – the plus symbol cannot be used in an identifier 2nd – an identifier cannot begin with a number 2. Data T Types ypes Data type: set of values together with a set of operations Primitive Data T Types: ypes: • Integr Integral al (int, char char,, long, short, byte) byte), which is a data type that deals with integers, or numbers without a decimal part (and characters). Ex. 23, -67, a, Z • Floating-point (float, double) double), which is a data type that deals with decimal numbers. Ex. 9.76, 5.99, 5.0 • Boolean (boolean) (boolean), which is a data type that deals with logical values, true or false 3. Arithmetic Oper Operators ators and Operato Operatorr Precedence There are Five arithmetic operators used in java a. + addition b. - subtraction c. * multiplication d. / division e. % mod (modulus) College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 10 of 26

Unary oper operator: ator: operator that has one operand Binary operator: operator that has two operands Oper Operator ator Precedence: this is the arrangement of arithmetic operators where the interpreter process first than another. This is also the same concept as MDAS (Multiplication- DivisionAddition- Subtraction) 1. * / % (same precedence) 2. + (same precedence) Operators in 1 have a higher precedence than operators in 2 When operators have the same level of precedence, operations are performed from left to right 4. Expressions There are 3 types of expressions: a. Integral Expressions – All operands are integers. Example: 2 + 3 * 5 3+x–y/7 x + 2 * ( y – z ) + 18 b. Floating-point or decimal Expressions – All operands are floating-point numbers Example: 12.8 * 17.5 - 34.50 x * 10.5 + y - 16.2 c. Mixed Expressions – Operands are of different types (integral and floating-point) Example: 2 + 3.5 6 / 4 + 3.9 • • •

Integer operands yield an integer result; floating-point numbers yield floating-point results If both types of operands are present, the result is a floating-point number Precedence rules are followed

5. Typ ype e Conversion (Casting) Used to avoid implicit type coercion (coercion – the process of fitting in the data type into another) Syntax: (datatype) expression4 • Expression evaluated first, then type converted to dataTypeName • Examples (int)(7.9 + 6.7) = (int)14.6 = 14 (int)(7.9) + (int)(6.7) = 7 + 6 = 13 The class String • 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 Strings and the Operator + • •

• •

Operator + can be used to concatenate two strings or a string and a numeric value or character Example: "The sum = " + 12 + 26 -After this statement executes, the string assigned to str is: "The sum = 1226"; Consider the following statement: "The sum = " + (12 + 26) In this statement, because of the parentheses, you first evaluate num1 + num2 – Because num1 and num2 are both int variables, num1 + num2 = 12 + 26 = 38 – After this statement executes, the string assigned to str is: "The sum = 38";

College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 11 of 26

6. Input •







• •



Named constant – Cannot be changed during program execution – Declared by using the reserved word final – Initialized when it is declared Example 2-11 final double CENTIMETERS_PER_INCH = 2.54; final int NO_OF_STUDENTS = 20; final char BLANK = ' '; final double PAY_RATE = 15.75; Variable (name, value, data type, size) – Content may change during program execution – Must be declared before it can be used – May not be automatically initialized – If new value is assigned, old one is destroyed – Value can only be changed by an assignment statement or an input (read) statement Example 2-12 double amountDue; int counter; char ch; int num1, num2; The assignment statement variable = expression; Example 2-13 int num1; int num2; double sale; char first; String str; num1 = 4; num2 = 4 * 5 - 11; sale = 0.02 * 1000; first = 'D'; str = "It is a sunny day."; Example 2-14 1. num1 = 18; 2. num1 = num1 + 27; 3. num2 = num1; 4. num3 = num2 / 5; 5. num3 = num3 / 4;

College of Information and Computing Sciences | IT102 – Computer Programming 1

Page 12 of 26

• • •

Standard input stream object: System.in Input numeric data to program – Separate by blanks, lines, or tabs To read data: – ...


Similar Free PDFs