COURSE - Problem Solving and
Programming Concepts (CSC3100)
TOPIC 1 - Programming Concepts
   ⮚ Components and Programming Languages
   ⮚ Programming Phase
   ⮚ Error and debugging
                    Learning Outcomes
• At the end of this topic, student should be able to:
  • Explain and compare type of programming language
    (C2)
  • Explain programming phases (C2)
  • Identify type of error (C2)
  ASSESSMENT 1, FINAL EXAM
                         Computer Components
                           What is a Computer?
◦   An electronic device that performs computations, logical operations, stores and
    processes data according to some prescribed sequence of instructions called a
    computer program.
◦   hardware - physical components of a computer, physical aspect of the computer that
    can be seen
◦   software - programs in which contents invisible instructions that control the hardware
    and make it perform specific tasks.
◦   Computer programming consists of writing instructions for computers to perform.
◦   You can learn a programming language without knowing computer hardware, but
    you will be better able to understand the effect of the instructions in the program if
    you do.
   What is a Computer?
A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer,
and communication devices.
                                                                4
                      The Hardware
⚫ The major hardware components:
  ◦   The central processing unit (CPU)
  ◦   Primary or random access memory (RAM)
  ◦   Secondary or long term memory
  ◦   Input and output devices (I/O devices)
  ◦   Communication devices
             The Central Processing Unit
⚫ The CPU does:
  ◦ the computing
  ◦ the processing
  ◦ the bulk of the work
⚫ Important components of the CPU:
  ◦ arithmetic and logic unit (ALU)
  ◦ control unit (CU)
  ◦ registers
                 The Central Processing Unit
◦ The ALU performs calculations, billions per second
◦ The CU controls or coordinates which calculations the ALU performs
◦ Registers – to hold data and instructions temporarily for execution
◦ The CPU clock determines how frequently the computer hardware
  executes instructions.
◦ A system’s hardware components are synchronized with the clock.
  Every time the clock ticks, another hardware action occurs.
      Primary and Random Access MemoryUnit
⚫ How Data is Stored:
  ◦ binary format
  ◦ a sequence of 0’s and 1's called bits.
⚫ ASCII encoding:
  ◦ ‘a’ is represented by 01100001
  ◦ ‘b’ is encoded as 01100010
• A sequence of eight bits is called a byte.
                         Where is Data Stored?
⚫ When the CPU executes a program, the program
  instructions, along with relevant data, are stored
  in primary memory.
⚫ Primary memory is also known as random access
  memory (RAM) because data may be retrieved or
  accessed in random, rather than sequential, order.
⚫ You can conceptualize RAM as a collection of
  storage cells or boxes, each capable of holding
  just a single byte of information.
⚫ A unique number, or memory address, identifies
  each such storage cell.
                          Secondary Storage
⚫ Second Storage                ■ Secondary memory devices:
  ◦ Long term                     ■ hard disks
  ◦ Permanent storage             ■ tapes
                                  ■ CDs
                                  ■ flash memory sticks.
■ The programs that you use every day such as word processors, spreadsheets, and
  games are permanently stored on secondary storage devices.
■ Compared to RAM, secondary memory is, in general, cheaper (per bit), slower,
  larger, electromechanical rather than electronic, and persistent: secondary
  memory devices do not lose their values when you turn off the computer.
                  Input/Output Devices
⚫ Standard Input Devices:     ■ Output devices:
  ◦ keyboards                   ■ Monitors
                                ■ Printers
  ◦ mouses
                                ■ speakers
  ◦ joysticks
  ◦ stylus pens
  ◦ cameras
  ◦ microphones
    Communication Devices
A regular modem uses a phone line and can transfer data in a speed up to 56,000 bps (bits
per second).
A DSL (digital subscriber line) also uses a phone line and can transfer data in a speed 20
times faster than a regular modem.
A cable modem uses the TV cable line maintained by the cable company. A cable modem is
as fast as a DSL.
Network interface card (NIC) is a device to connect a computer to a local area network (LAN).
The LAN is commonly used in business, universities, and government organizations. A typical
type of NIC, called 10BaseT, can transfer data at 10 mbps (million bits per second).
                                                                     12
                         Programs
◦ Computer programs, known as software, are instructions to
  the computer.
◦ You tell a computer what to do through programs. Without
  programs, a computer is an empty machine. Computers do not
  understand human languages, so you need to use computer
  languages to communicate with them.
◦ Programs are written using programming languages.
                  Programming Language
⚫ Programming Language is an agreed upon format of
  symbols that allow a programmer to instruct a computer to
  perform certain predefined tasks.
⚫ Provide features to support the data processing activities,
  which include declaring variables, evaluating numeric
  expressions, assigning values to variables, reading and writing
  data to devices, looping and making decisions.
⚫ Machine Languages
  ◦ Is the natural language of a computer.
   ◦    Does not need to translate and is ready for immediate execution.
   ◦    Machine language instruction is a binary string of 0s and 1s.
          010 1 1000 0001 0000 1100 0000 0001 0000
   ◦    Are machine-dependent
       ● each computer type has its own machine language.
   ◦    Programs written in machine languages are not portable because
        programs written in for one type of computer cannot be run on
        another type
⚫ Assembly Languages
  ◦   Consists of English-like abbreviations.
  ◦   Program written in assembly languages cannot be directly processed by a computer.
  ◦   Must use language translators, called assemblers, to convert them to machine code.
  ◦   Disadvantages:
      ◦ In general, each assembly language instruction corresponds to one machine
         language instruction. Therefore, the programs written in them are lengthy.
  ◦   Because of variations in assembly languages, programs written using them are not
      portable.
  ◦   For example ADDF3 R1, R2, R3
⚫ High-Level languages
  ◦ Instructions are quite English-like, and a single instruction can be
    written to correspond to many operations at the machine level.
  ◦ For example, the following is a high-level language statement that
    computes the area of a circle with radius 5:
      area = 5 * 5 * 3.1415;
  ◦ Are easier to learn than machine or assembly languages.
  ◦ Have to be converted to machine languages before they can be
    executed using compilers, system software that translates a source
    program into an almost executable object program
Popular High-Level Languages
                               18
             Interpreting/Compiling Source Code
◦   A program written in a high-level language is called a source program.
◦   Since a computer cannot understand a source program, a source program must be translated
    into machine code for execution.
◦   A compiler is a program used to translate the source program into a machine language
    program called an object program.
◦   The object program is often then linked with other supporting library code before the object
    can be executed on the machine.
◦   You can port a source program to any machine with appropriate compilers.
◦   The source program must be recompiled, however, because the object program can only run
    on a specific machine.
◦   Nowadays computers are networked to work together.
Interpreting Source Code
An interpreter reads one statement from the source
code, translates it to the machine code or virtual
machine code, and then executes it right away, as shown
in the following figure. Note that a statement from the
source code may be translated into several machine
instructions.
                                       20
Compiling Source Code
A compiler translates the entire source code into a
machine-code file, and the machine-code file is then
executed, as shown in the following figure.
                                        21
⚫   Operating Systems:
    ◦ The operating system (OS) is a program that
      manages and controls a computer’s activities.
    ◦   You are probably using Windows 98, NT,
        ME, 2000, XP, VISTA, Windows 7 or 8 or
        10.
    ◦   Windows is currently the most popular PC
        operating system.
    ◦   Application programs such as an Internet
        browser and a word processor cannot run
        without an operating system.
               Java Programming Language
⚫ Why Java?
  ◦ The answer is that Java enables users to deploy applications
     on the Internet for servers, desktop computers, and small
     hand-held devices.
  ◦
  ◦ The future of computing will be profoundly influenced by
     the Internet, and Java promises to remain a big part of that
     future.
    ●Java is a general purpose programming language.
    ●Java is the Internet programming language.
⚫ Java, Web, and Beyond
  ◦ Java can be used to develop Web applications.
   ◦ Java Applets
      ● Java programs that run from a Web Browser
      ● Applets use a modern graphical user interface with buttons, text fields etc.
        to interact with users on the web and process their request.
   ◦ Java Servlets and JavaServer Pages
      ● Application on server.
      ● Run from a web server to generate dynamic Web pages.
      ● Example: self-test website
   ◦ Java can also be used to develop applications for hand-held devices such as
     Palm and cell phones
                                              �
⚫ Java’s History
 ◦ James Gosling and Sun Microsystems         �
 ◦ Originally called Oak
 ◦ Java, May 20, 1995, Sun World
 ◦ HotJava
   ●The first Java-enabled Web browser
 ◦ Early History Website:
   ●http://java.sun.com/features/1998/05/birthday
     .html
                                                                 �
Companion
Website       Characteristics of Java
        ⚫ Java Is Simple
        ⚫ Java Is Object-Oriented
        ⚫ Java Is Distributed
                                                                 �
        ⚫ Java Is Interpreted
        ⚫ Java Is Robust
        ⚫ Java Is Secure
        ⚫ Java Is Architecture-Neutral
        ⚫ Java Is Portable
        ⚫ Java's Performance
        ⚫ Java Is Multithreaded                26
        ⚫ Java Is Dynamic
            www.cs.armstrong.edu/liang/JavaCharacteristics.pdf
⚫ The Java Language Specification, API, JDK and IDE
  ◦ The Java language specification is a technical definition of
    the language that includes the syntax and semantics of the
    Java programming language.
  ◦ Application Program Interface (API) contains predefined
    classes and interfaces for developing Java programs.
    (http://docs.oracle.com/javase/7/docs/api/)
  ◦ Three editions of the Java API: Java 2 Standard Edition
    (J2SE), Java 2 Enterprise Edition (J2EE) and Java 2 Micro
    Edition (J2ME).
⚫ JDK Version
 ◦ JDK Alpha and Beta (1995)
 ◦ JDK 1.0 (1996) – codename Oak
 ◦ JDK 1.1 (1997) …. JDK 10.0 (2018)
⚫ Major changes
 ◦   J2SE 1.2 (a.k.a JDK 1.2, 1998) – codename Playground
 ◦   J2SE 1.3 (a.k.a JDK 1.3, 2000) – codename Kestrel
 ◦   J2SE 1.4 (a.k.a JDK 1.4, 2002) – codename Merlin
 ◦   J2SE 5.0 (a.k.a JDK 1.5, 2004) – codename Tiger
 ◦   Java SE 6 (a.k.a JDK 6, 2009) – codename Mustang
 ◦   Java SE 7 (a.k.a JDK 7, 2011) – codename Dolphin
 ◦   Java SE 8 (a.k.a JDK 8, 2014)
Java SE                                                           Java SE Version
Version        Version        Release Date                                          Version Number Release Date
               Number
                                                                  Java SE 9         9              September, 21st 2017
JDK 1.0        1.0            January 1996
(Oak)
                                                                  Java SE 10        10             March, 20th 2018
JDK 1.1        1.1            February 1997
                                                                  Java SE 11        11             September, 25th 2018
J2SE 1.2       1.2            December 1998
(Playground)                                                      Java SE 12        12             March, 19th 2019
J2SE 1.3       1.3            May 2000
(Kestrel)                                                         Java SE 13        13             September, 17th 2019
J2SE 1.4       1.4            February 2002
(Merlin)                                                          Java SE 14        14             March, 17th 2020
J2SE 5.0       1.5            September 2004
(Tiger)                                                           Java SE 15        15             September, 15th 2020
Java SE 6      1.6            December 2006
(Mustang)                                                         Java SE 16        16             March, 16th 2021
Java SE 7      1.7            July 2011
(Dolphin)                                                         Java SE 17        17             Expected on Sept. 2021
Java SE 8      1.8            March 2014
Ref : https://www.codejava.net/java-se/java-se-versions-history
⚫ JDK Editions
  ◦ Java Development Toolkit (JDK) consists of a set of separate
    programs for developing and testing Java programs.
  ◦ Java Standard Edition (J2SE)
    ● J2SE can be used to develop client-side standalone applications or
      applets.
  ◦ Java Enterprise Edition (J2EE)
    ● Java EE can be used to develop server-side applications such as
      Java servlets and Java ServerPages.
  ◦ Java Micro Edition (J2ME).
    ● Java ME can be used to develop applications for mobile devices
      such as cell phones.
⚫ This course uses J2SE to introduce Java programming.
⚫ Java IDE Tools
  ◦ A Java development tool is software that provides an integrated development
    environment (IDE) for rapidly developing Java programs, such as:
     ● Borland JBuilder
     ● NetBeans Open Source by Sun
     ● Sun ONE Studio by Sun MicroSystems
     ● Eclipse Open Source by IBM
   ◦ Editing, compiling, building, debugging, and online help are integrated in one
     graphical user interface.
   ◦ Just enter source code in one window or open an existing file in a window,
     then click a button, menu item, or function key to compile and run the
     program.
     A Simple Program (IDE tool – eclipse)
⚫ Example
     A Simple Program (IDE tool – BlueJ)
⚫ Example https://www.bluej.org/
Creating and Editing Using
NotePad
DOS prompt – use command cmd
To use NotePad, type command
   notepad Welcome.java
Creating and Editing Using WordPad
 To use WordPad, type
   write Welcome.java
 from the DOS prompt.
                        35
⚫ Creating, Compiling, and Running Programs
    Compiling Java Source Code
⚫ You can port a source program to any machine
  with appropriate compilers.
⚫ The source program must be recompiled,
  however, because the object program can only
  run on a specific machine. Nowadays
  computers are networked to work together.
  Java was designed to run object programs on
  any platform.
⚫ With Java, you write the program once, and
  compile the source program into a special type
  of object code, known as bytecode.
⚫ The bytecode can then run on any computer
  with a Java Virtual Machine, as 37
                                   shown below.
  Java Virtual Machine is a software that
  interprets Java bytecode.
animation
    Trace a Program Execution
                                Enter main method
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                   38
animation
    Trace a Program Execution
                                Execute statement
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                   39
animation
    Trace a Program Execution
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                print a message to the
                                console
                                    40
             Anatomy of a Java Program
⚫ Comments
⚫ Package
⚫ Reserved words
⚫ Modifiers
⚫ Statements
⚫ Blocks
⚫ Classes
⚫ Methods
⚫ The main method
Comments
Comments are ignored by the compiler.
//… (one line), or
/* …. */ (for one or multiple lines)
◦ When the compiler sees //, it ignores all text after // in the
  same line.
◦ When it sees /*, it scans for the next */ and ignores any text
  between /* and */.
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                                  42
  Reserved words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program. For example, when the
compiler sees the word class, it understands that the word
after class is the name for the class.
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                            43
  Class Name
⚫ Every Java program must have at least one class.
⚫ Each class has a name.
⚫ By convention, class names start with an uppercase letter.
⚫ In this example, the class name is Welcome.
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                           44
     Methods
⚫   What is System.out.println("Welcome to Java!" )?
⚫   It is a method: a collection of statements that performs a
    sequence of operations to display a message on the console.
⚫   It can be used even without fully understanding the details of
    how it works.
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }                           string argument
}
                                                 45
  Main Method
⚫ Line 2 defines the main method. In order to run a class,
  the class must contain a method named main.
⚫ The program is executed from the main method.
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                          46
    Statement
⚫   A statement represents an action or a sequence of actions.
⚫   The statement
    System.out.println("Welcome to Java!")
    in the program in Listing 1.1 is a statement to display the
    greeting "Welcome to Java!“.
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                                    47
 Statement Terminator
Every statement in Java ends with a semicolon (;).
//This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}
                                               48
 Blocks
 A pair of braces in a program forms a block that
 groups components of a program.
// This program prints Welcome to Java!
public class Welcome {
  public static void main(String[] args) {
    System.out.println("Welcome to Java!");
  }
}                                   49
Special Symbols
     Name              Description
{}   Braces            Block to enclose statements
()   Parentheses       Used with methods
[]   Brackets          Array
//   Double slashes    Precedes a comment line
“”   Quotation marks   Enclosing a string (sequence of characters)
;    Semicolon         Marks the end of a statement
                                                        50
Programming Or Implementation Phase
⚫   Transcribing the logical flow of solution steps in flowchart or algorithm to
    program code and run the program code on a computer using a programming
    language.
⚫   Programming phase takes 5 stages:
      ●Coding.
      ●Compiling.
      ●Debugging.
      ●Run or Testing.
      ●Documentation and maintenance.
⚫   Once the program is coded using one of the programming language, it will be
    compiled to ensure there is no syntax error. Syntax free program will then be
    executed to produce output and subsequently maintained and documented for
    later reference.
     CODING
   COMPILE THE              MAKE
    PROGRAM              CORRECTION
     SYNTAX        YES
     ERROR ?
        NO
   EXECUTE OR
      RUN
DOCUMENTATION OR
  MAINTENANCE
Programming Phase: Coding
◦ Translation or conversion of each operation in the flowchart or
  algorithm (pseudocode) into a computer-understandable language.
◦ Coding should follow the format of the chosen programming
  language.
◦ Many types or levels of computer programming language such as:
  ● Machine language
  ● Symbolic language or assembly language
  ● Procedure-oriented language
◦ The first two languages are also called low-level programming
  language. While the last one is called high-level programming
  language.
Programming Phase: Compiling
◦ Compiling is a process of a compiler translates a program written
  in a particular high–level programming language into a form that
  the computer can execute.
◦ The compiler will check the program code know also as source
  code so that any part of the source code that does not follow the
  format or any other language requirements will be flagged as
  syntax error.
Programming Phase: Debugging
◦ This syntax error in also called bug, when error is found the
  programmer will debug or correct the error and then recompile
  the source code again.
◦ The debugging process is continued until there is no more error in
  the program.
Programming Phase: Run or Testing
 ◦ The program code that contains no more error is called
   executable program. It is ready to be tested.
 ◦ When it is tested, the data is given and the result is verified so
   that it should produced output as intended.
 ◦ Though the program is error free, sometimes it does not produced
   the right result. In this case the program faces logic error.
 ◦ Incorrect sequence of instruction is an example that causes logic
   error.
Programming Phase: Documentation and Maintenance
◦ When the program is thoroughly tested for a substantial period of time
  and it is consistently producing the right output, it can be documented.
◦ Documentation is important for future reference. Other programmer may
  take over the operation of the program and the best way to understand a
  program is by studying the documentation.
◦ Trying to understand the logic of the program by looking at the source
  code is not a good approach.
◦ Studying the documentation is necessary when the program is subjected
  to enhancement or modification.
◦ Documentation is also necessary for management use as well as audit
  purposes.
         Error and debugging
         Programming Errors
⚫ Syntax Errors
  ◦ Detected by the compiler
  ◦ Result from errors in code construction, such as
    mistyping a keyword.
⚫ Runtime Errors
  ◦ Causes the program to abort
  ◦ Occur while an application is running such as input
    error (unexpected value that the program cannot
    handle) or division by zero.
⚫ Logic Errors
  ◦ Produces incorrect result
 Syntax Errors
public class ShowSyntaxErrors {
  public static void main(String[] args) {
    i = 30;
    System.out.println(i + 4);
  }
}
                                             59
Runtime Errors
public class ShowRuntimeErrors {
  public static void main(String[] args) {
    int i = 1 / 0;
  }
}
                                             60
 Logic Errors
// Program to compute the sum of two integer numbers
public class Sum {
  public static void main(String[] args) {
     int num1 = 3;
     int num2 = 2;
     int sum = num1 + num2;
     sum = 0;
    System.out.println(“Total is " + sum);
  }
}
Which displays:
Total is 0
But the result that we want is 5.
                                                       61
 Debugging
▪ Logic errors are called bugs. The process of finding and correcting
  errors is called debugging.
▪ A common approach to debugging is to use a combination of
  methods to narrow down to the part of the program where the bug is
  located.
▪ You can hand-trace the program (i.e., catch errors by reading the
  program), or you can insert print statements in order to show the
  values of the variables or the execution flow of the program.
  ▪ This approach might work for a short, simple program.
  ▪ But for a large, complex program, the most effective approach for debugging
    is to use a debugger utility.