Top Banner
Computer Programming I - CSC111 Introduction Dr. Mejdl Safran [email protected] 1
30

Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Oct 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Computer Programming I - CSC111

Introduction

Dr. Mejdl Safran

[email protected]

Page 2: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

SyllabusSyllabusSyllabusSyllabus

• Textbook

‒ Java: An Introduction to Problem Solving and Programming,7ed, W. Savitch, Pearson International (Textbook)

‒ Java How to Program, 7ed, Deitel and Deitel, Pearson International (Reference)

‒ Introduction to Java Programming, Comprehensive Version, 10ed Y. Daniel Liang, Prentice Hall (Reference)

2

Page 3: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

ContentContentContentContent

• Chapter 1: Intro to computers and Java

• Chapter 2: Basic computation

• Chapter 3: Flow of control: Branching

• Chapter 4: Flow of control: Loops

• Chapter 5: Defining classes and objects

• Chapter 6: More about objects and methods

• Chapter 7: Arrays

3

Page 4: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Assessment Methods & PolicyAssessment Methods & PolicyAssessment Methods & PolicyAssessment Methods & Policy

Assignments , Quizzes & Attendance 10%

Class Project 5%

Lab Exams 10% + 15%

Midterms 10% + 10%

Final exam 40%

4

Page 5: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Homework Assignments & QuizzesHomework Assignments & QuizzesHomework Assignments & QuizzesHomework Assignments & Quizzes

• Homework will be assigned and graded

• All homework assignments will be given with a strict deadline, and students are required to submit assignments on or before the deadline.

• Cheating will not be tolerated.

• All homework assignments or project documents should be submitted using MS-Word and/or appropriate computer software.

• No hand written submission will be accepted.

• In-class quizzes will be given throughout the semester

5

Page 6: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Chapter OutlineChapter OutlineChapter OutlineChapter Outline

• What a computer is

• What a computer program is

• The Programmer’s Algorithm

• How a program that you write in Java is changed into a form that your computer can understand

• Characteristics of Java

6

Page 7: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Hardware & SoftwareHardware & SoftwareHardware & SoftwareHardware & Software

• Computer systems consist of hardware and software.

• Familiarity with hardware basics helps us understand software.

7

Page 8: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Components of computer systemComponents of computer systemComponents of computer systemComponents of computer system

• Most modern computers have similar components including

‒ Input devices (keyboard, mouse, etc.)

‒ Output devices (display screen, printer, etc.)

‒ A processor (CPU): processes a program’s instructions

‒ Two kinds of memory: 1) Main memory (RAM): temporary storage

2) Secondary Memory: persistent storage 8

Page 9: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Components of computer systemComponents of computer systemComponents of computer systemComponents of computer system

9

Page 10: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

ProgramsProgramsProgramsPrograms

• A program is a set of instructions for a computer to follow.

• We use programs almost daily (email, word processors, video games, bank ATMs, etc.).

• Programs are stored in files.

• Programs files are copied from secondary memory to main memory in order to be run.

How to write a program10

Page 11: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Levels of AbstractionLevels of AbstractionLevels of AbstractionLevels of Abstraction

• Human thought

• Pseudo-Natural Language (Arabic, English)

• High-level Programming Language (C, C++, Java, …)

• Machine Code

11

Page 12: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

The Programmer’s AlgorithmThe Programmer’s AlgorithmThe Programmer’s AlgorithmThe Programmer’s Algorithm

• An algorithm is a finite sequence of instructions that produces a solution to a problem.

• The programmer’s algorithm:

‒ Define the problem

‒ Plan the problem solution

‒ Code the program

‒ Compile the program

‒ Run the program

‒ Test and debug the program12

Page 13: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Defining the problemDefining the problemDefining the problemDefining the problem

•The problem must be defined in terms of:

‒ Input: data to be processed

‒ Output: the expected results

‒Look for nouns in the problem statement that suggest output and input

‒ and processing: the statements to achieve

‒Look for verbs to suggest processing steps

13

Page 14: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Example: sum and average of 5 numbers Example: sum and average of 5 numbers Example: sum and average of 5 numbers Example: sum and average of 5 numbers

• Input:

‒ Five numbers: x1, x2, x3, x4, x5

• Processing:

‒ Sum = x1 + x2 + x3 + x4 + x5

‒ Average = Sum/5

• Output:

‒ Sum

‒ Average

14

Page 15: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Planning the solutionPlanning the solutionPlanning the solutionPlanning the solution

• When planning, algorithms are used to outline the solution steps using Englishlike statements, called pseudocode

• Simple pseudocode:

1.1.1.1. Start programStart programStart programStart program2.2.2.2. Get five numbers (x1, x2, x3, x4, x5)Get five numbers (x1, x2, x3, x4, x5)Get five numbers (x1, x2, x3, x4, x5)Get five numbers (x1, x2, x3, x4, x5)3.3.3.3. Add them (sum = x1 + x2 + x3 + x4 + x5)Add them (sum = x1 + x2 + x3 + x4 + x5)Add them (sum = x1 + x2 + x3 + x4 + x5)Add them (sum = x1 + x2 + x3 + x4 + x5)4.4.4.4. Compute average (Compute average (Compute average (Compute average (avgavgavgavg = sum / 5)= sum / 5)= sum / 5)= sum / 5)5.5.5.5. Print sum & Print sum & Print sum & Print sum & avgavgavgavg6.6.6.6. End programEnd programEnd programEnd program

15

Page 16: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Coding the programCoding the programCoding the programCoding the program

• Coding is writing the program in a formal language called programming language.

• The program is written by translating the algorithm steps into a programming language statements

• The written program is called source code and it is save in a file with “.java” extension.

Why coding in programming languages

16

Page 17: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Why coding in programming languagesWhy coding in programming languagesWhy coding in programming languagesWhy coding in programming languages

• High-level languages are relatively easy to use

• Java, C#, C++, Visual Basic, Python, Ruby.

• Unfortunately, computer hardware does not understand high-level languages.

• Therefore, a high-level language program must be translated into a low-level language (machine code).

How to translate source code into machine code17

Page 18: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Compiling computer programsCompiling computer programsCompiling computer programsCompiling computer programs

• A compiler translates a program from a high-level language to a low-level language the computer can run.

CompilerSource code Machine code

• The compiler

� checks correctness of the source code (syntax errors)

� translates the source code into a machine code if no errors were found

18

Page 19: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Platform dependent compilingPlatform dependent compilingPlatform dependent compilingPlatform dependent compiling

• Most high-level languages need a different compiler for each type of computer and for each operating system.

• Most compilers are very large programs that are expensive to produce.

program

compiler

machine code machine code machine code

compiler compiler

Win Mac Unix

How to run a Java program on each computer, with no need to

recompile

19

Page 20: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Compiling Java ProgramsCompiling Java ProgramsCompiling Java ProgramsCompiling Java Programs

• The Java compiler does not translate a Java program into machine code for a particular computer.

• Instead, it translates a Java program into bytecode.

• Bytecode is converted into machine code using Java Interpreter

• Platform independent!

javac

java

20

Page 21: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Running Java ProgramsRunning Java ProgramsRunning Java ProgramsRunning Java Programs

• The Java Virtual Machine (JVM):

‒ Class Loader

‒ stores bytecodes in memory

‒ Bytecode Verifier

‒ ensures bytecodes don’t violate security requirements

‒ Bytecode Interpreter:

‒ translates bytecodes into machine codes

Class

Loader

Bytecode

Verifier

Bytecode

Interpreter

JVM

OS

Hardware

The bytecode

(.class file)

21

Page 22: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Testing and Debugging the programTesting and Debugging the programTesting and Debugging the programTesting and Debugging the program

• Testing

‒ Be sure that the output of the program conforms with the input

‒ Two types of errors:

‒ Logical errors: the program runs but provides wrong output

‒ Runtime errors: the program stops running suddenly when asking the OS executing a non accepted statement (divide by zero, etc.)

‒ Debugging

‒Find, understand and correct the errors22

Page 23: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Some characteristics of JavaSome characteristics of JavaSome characteristics of JavaSome characteristics of Java

• Object-oriented programming (OOP)

� Treats program as a collection of objects that interact by means and actions

� Encapsulation (information hiding)

� Data Abstraction (implementation hiding)

� Inheritance (Undergraduate is a subclass of Student)

� Polymorphism (single action in different ways)

• Platform independent

• Portable

• Architecture neutral

• “Write-once, run anywhere”

• Secure

• Bytecode verifier of the VM23

Page 24: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Setting up your Development EnvironmentSetting up your Development EnvironmentSetting up your Development EnvironmentSetting up your Development Environment

• Download latest JDK from

https://www.oracle.com/technetwork/java/javase/downloads/jdk10-downloads-4416644.html

• Java Development Kit (JDK) is required to develop and compile programs

• Java Runtime Environment (JRE) is required to run programs

• Users must have JRE installed

• Developers must have the JDK installed

• JDK includes the JRE24

Page 25: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

What is an IDE?What is an IDE?What is an IDE?What is an IDE?

• IDE = Integrated Development Environment

• Makes you more productive

• Includes text editor, compiler, debugger, syntax highlighting and code completion, etc.

• Eclipse is the most widely used IDE

• Alternatives:

• NetBeans (Oracle)

• IntelliJ IDEA (JetBrains)25

Page 26: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Installing EclipseInstalling EclipseInstalling EclipseInstalling Eclipse

• Download and install the latest Eclipse for Java from:

http://www.eclipse.org/downloads/packages/

• Unzip the content of the archive file you downloaded

• To start Eclipse

• On PC, double-click on Eclipse.exe

• On Mac, double-click on Eclipse.app in Application folder

26

Page 27: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Run Java program Run Java program Run Java program Run Java program –––– command linecommand linecommand linecommand line

27

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello");

}

}

Page 28: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Run Java program Run Java program Run Java program Run Java program –––– EclipseEclipseEclipseEclipse

28

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello");

}

}

Page 29: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

Print triangle of symbolsPrint triangle of symbolsPrint triangle of symbolsPrint triangle of symbols

29

public class PrintTriangle {

public static void main(String[] args) {

System.out.println("*");

System.out.println("**");

System.out.println("***");

System.out.println("****");

System.out.println("*****");

System.out.println("******");

System.out.println("*******");

}

}

Page 30: Computer Programming I - CSC111 Introduction · Components of computer system •Most modern computers have similar components including ‒Input devices (keyboard, mouse, etc.) ‒Output

printlnprintlnprintlnprintln() vs. print()() vs. print()() vs. print()() vs. print()

30

System.out.print("My name is ");

System.out.print("Mejdl");

System.out.println("My name is ");

System.out.print("Mejdl");

My name is

Mejdl

My name is Mejdl

System.out.print("My name is \n");

System.out.print("Mejdl");

My name is

Mejdl

System.out.print("Student name:\t");

System.out.print("Saad");

Student name: Saad

System.Out.print("Student name:");

System.out.print("Saad");

System.out.println("Student name:");

system.out.println("Saad");

Syntax error

Syntax error