Overview of Computers and Programming Languages - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Overview of Computers and Programming Languages

Description:

1. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL. The ... human readable. computer readable. Compiler. translated by. Low-level language (Machine language ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 51
Provided by: qizh
Category:

less

Transcript and Presenter's Notes

Title: Overview of Computers and Programming Languages


1
Overview of Computers and Programming Languages
Qi ZhangAug 25
COMP 110 Introduction to Programming
2
Last Week
  • Course Introduction
  • Installation of IDE jGRASP
  • Running the first java program
  • Questions with jGRASP?

3
Todays Lecture
  • Before you start programming, you need to
    understand how computers work
  • Hardware and software
  • Programming languages and compilers
  • Designing programs

4
Hardware vs. Software
A computer is made up of hardware and software
Software
Hardware
  • CPU
  • e.g. Intel Core 2 Duo
  • Input/Output devices
  • keyboard
  • monitor
  • mouse
  • main memory
  • e.g. 2GB RAM
  • secondary memory
  • e.g. 120 GB hard drive
  • operating systems
  • Windows XP
  • Mac OS X
  • Unix / Linux
  • applications
  • games
  • Microsoft Word
  • Internet Explorer

5
Hardware Organization
CPU
memory
motherboard
hard drive
6
Central Processing UnitCPU
  • "the brain" of a computer
  • Arithmetic Logic Unit (ALU) .
  • carries out all arithmetic and logical ops
  • Microcontroller
  • Fetch and decode instructions
  • Control the flow of info between CPU and main
    memory

CPU
Program

7
Memory
  • Provides permanent and temporary storage of
    information
  • Two basic types

Auxiliary memory
Main memory
8
Main Memory
  • Directly connected to the CPU
  • Aka Random Access Memory (RAM)
  • Main memory holds
  • The current program
  • Data the program is using
  • The results of intermediate calculations
  • All programs must be brought into main memory
    before execution
  • When power is turned off, everything in main
    memory is lost

9
Bit and Byte
  • Ordered sequence of storage cells
  • Each cell is a physical device which has two
    stable states (0/1)
  • Bit a 0/1 digit
  • Byte
  • 8 bits of memory
  • A quantity of memory
  • 4GB main memory
  • Memory Location
  • Adjacent bytes

10
Address
  • A computers main memory is divided into
    numbered bytes
  • Address of a Byte The number of the byte
  • Address of a Memory Location The address of the
    first byte

11
CPU and Main Memory
All programs must be brought into main memory
before execution
CPU Executes program instructions
Main memory Primary storage area for programs
and data that are in active use
12
Auxiliary Memory (Secondary Memory)
  • Provides permanent storage for information
  • Retains information even when power is off
  • Examples of secondary storage
  • Hard Disks
  • Floppy Disks
  • USB Flash Driver
  • CD-ROMs
  • Tapes
  • Measured in bytes
  • Java programs files are copied from auxiliary
    memory to main memory in order to be run.

13
Secondary Storage
Secondary memory devices provide long-term storage
Information is moved between main memory and
secondary memory as needed
14
Input/Output Devices
I/O devices facilitate user interaction
Keyboard, mouse, etc
monitor, printer, etc
15
Hardware vs. Software
A computer is made up of hardware and software
Software
Hardware
  • CPU
  • e.g. Intel Core 2 Duo
  • Input/Output devices
  • keyboard
  • monitor
  • mouse
  • main memory
  • e.g. 2GB RAM
  • secondary memory
  • e.g. 120 GB hard drive
  • operating systems
  • Windows XP
  • Mac OS X
  • Unix / Linux
  • applications
  • games
  • Microsoft Word
  • Internet Explorer

16
Programs
  • A program is a set of instructions for a computer
    to follow
  • Following the instructions is called running or
    executing the program.

17
Running a Program
  • Sometimes the computer and the program are
    considered to be one unit.
  • Programmers typically find this view to be more
    convenient.

18
Questions
  • Where is your Microsoft Word program stored when
    your computer is powered off?
  • What is the first program which is running when
    you first turn on the computer?

19
Programming Languages
High-level Programming Languages (e.g. Java)
human readable
translated by
Compiler
Low-level language (Machine language /assembly
language)
computer readable
20
Programming Languages
High-level Programming Languages (e.g. Java)
Source code
human readable
translated by
Compiler
Low-level language (Machine language /assembly
language)
Object code
computer readable
21
Portability Issue
Source code
Compiler 1
Compiler n
Compiler N
Object code N
Object code n
Object code 1
Machine N
Machine 1
Machine n
  • 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.

22
Java Does It Differently
Java source code
Compiler
Java byte code
Interpreter 1
Interpreter n
Interpreter N
Machine N
Machine 1
Machine n
  • Byte code provides portability
  • Interpreter is much simpler compared with compiler

23
Java Does It Differently
Java source code
Compiler
Java byte code
Java Virtual Machine (JVM)
Interpreter 1
Interpreter n
Interpreter N
Machine N
Machine 1
Machine n
  • Byte code provides portability
  • Interpreter is much simpler compared with compiler

24
Java Byte Code, Java Virtual Machine
  • Java byte code
  • not a machine language
  • generated when you compile your Java source code
  • similar to the machine language of most common
    computers, can be easily translated to machine
    language
  • good portability after compiling a Java program
    into byte-code, that byte-code can be used on any
    computer with a byte-code interpreter and without
    a need to recompile.
  • Java Virtual Machine
  • Translates byte code to machine language and
    executes it
  • Java Virtual Machine is a simpler program
    compared with compiler
  • Java Virtual Machine has to be installed to run
    the byte code on any particular machine

25
Questions
  • In Lab 0, you worked on a program which prints
    out Hello world!, recall the files generated
    and the actions you took
  • Which is the Java source code?
  • Which is the byte code, how was it generated?
  • Did you install the Java Virtual Machine? When?
  • Given you a Java source code, what are the steps
    you need to take to run it?

26
Run your Java Program
Java source code
Hello.java
Compiler
Step 1 Compile
Java byte code
Hello.class
Step 2 Run
Java Virtual Machine (JVM)
Interpreter 1
Interpreter n
Interpreter N
Machine N
Machine 1
Machine n
  • Byte code provides portability
  • Interpreter is much simpler compared with compiler

27
Program Design
  • Programming is a creative process
  • Java is an Object-oriented programming language.

28
Object-Oriented Programming
  • Our world consists of objects (people, trees,
    cars, cities, airline reservations, etc.).
  • Objects can perform actions which affect
    themselves and other objects in the world.
  • People drive cars
  • People make airline reservations
  • Object-oriented programming (OOP) treats a
    program as a collection of objects that interact
    by means of actions.

29
OOP Terminology
  • Objects, appropriately, are called objects.
  • Actions are called methods.
  • Objects of the same kind have the same type and
    belong to the same class.
  • Objects within a class have a common set of
    methods and the same kinds of data
  • but each object can have its own data values.

30
Algorithms
  • An algorithm describes a strategy/plan of
    performing an action.
  • Once an algorithm is defined, expressing it in
    Java (or in another programming language) usually
    is easy.
  • Algorithms usually are expressed in English or in
    pseudocode

31
Example Total Cost of All Items
  • Write the number 0 on the whiteboard.
  • For each item on the list
  • add the cost of the item to the number on the
    whiteboard
  • replace the number on the whiteboard with the
    result of this addition.
  • Announce that the answer is the number written on
    the whiteboard.

Class TotalCostCalculator Object
myTotalCostCalculator Method add
32
OOP Design Principles
  • Encapsulation.
  • Polymorphism.
  • Inheritance.

33
Encapsulation
  • Information hiding hides the detail of what is
    inside the capsule
  • Describe a class/object by giving only enough
    information to allow a programmer to use it.

34
Polymorphism
  • many forms
  • Example.
  • class car
  • object myAutoCar, myManualCar
  • method start, stop, turn, etc..
  • More detail in Ch.7

35
Inheritance
  • The way of organizing classes
  • Avoid repeat description

Vehicle
Automobile
Motorcycle
Bus
Automobile
Automobile
Automobile
Automobile
36
Testing and Debugging
  • Eliminate errors by avoiding them in the first
    place.
  • Carefully design classes, algorithms and methods.
  • Carefully code everything into Java.
  • Test your program with appropriate test cases
    (some where the answer is known), discover and
    fix any errors, then retest.

37
Errors
  • An error in a program is called a bug.
  • Eliminating errors is called debugging.
  • Three kinds or errors
  • syntax errors
  • runtime errors
  • logic errors

38
Syntax Errors
  • grammatical mistakes in a program
  • the grammatical rules for writing a program are
    very strict
  • The compiler catches syntax errors and prints an
    error message.
  • example using a period where a program expects a
    semicolon

39
Runtime Errors
  • Errors that are detected when your program is
    running, but not during compilation
  • When the computer detects an error, it terminates
    the program and prints an error message.
  • Example attempting to divide by 0

40
Logic Errors
  • Errors that are not detected during compilation
    or while running, but which cause the program to
    produce incorrect results
  • Example an attempt to calculate a Fahrenheit
    temperature from a Celsius temperature by
    multiplying by 9/5 and adding 23 instead of 32

41
Sample Java Program p.23
  • import java.util. public class
    FirstProgram public static void
    main(String args)
    System.out.println("Hello out there.")
    System.out.println("I will add two number for
    you.") System.out.println("Enter two
    whole number on a line") int
    n1, n2 Scanner keyboard
    new Scanner(System.in) n1
    keyboard.nextInt() n2
    keyboard.nextInt()
    System.out.println("The sum of those two numbers
    is") System.out.println(n1 n2)

41
42
import Package
  • import java.util.
  • Package Library of classes which are already
    defined
  • Syntax
  • import packagename.
  • The class(es) in the package is going to be used
    later in the code

42
43
Begin the Class
  • public class FirstProgram public
    static void main(String args)
  • Begin a definition of a class named FirstProgram
  • A Java program consists of one or more classes
  • Each class should be in a separate file.
  • The name of the file should be the same as the
    name of the class. FirstProgram.java

43
44
Output to screen
  • System.out.println("Hello out there.")
  • System.out.println("I will add two numbers for
    you.")
  • System.out.println("Enter two whole numbers on a
    line")
  • Write what is in quotes to screen
  • Object System.out
  • Method println

44
45
Invoke methods on objects
  • System.out.println(Hi)

parameters)
Object.
Method (
Syntax
Invoke Method
45
46
Variable
  • int n1, n2
  • Variable - store piece of data
  • n1 - store integer
  • n2 - store integer

46
47
Create Scanner Object
  • Scanner keyboard new Scanner(System.in)
  • Create an object (keyboard) of Scanner class
  • Syntax
  • Classname objectname new Classname(parameters
    )

47
48
Call method on object
  • n1 keyboard.nextInt()
  • Calls the method nextInt of object keyboard
  • nextInt is defined in class Scanner
  • Read an integer from the keyboard and store it in
    n1

Object
48
49
Output to screen
  • System.out.println("The sum of those two numbers
    is")
  • System.out.println(n1 n2)

Add n1 and n2 Print the sum to the screen
49
50
Next class
  • Primitive Types, Strings
Write a Comment
User Comments (0)
About PowerShow.com