Lecture 2 2024
Lecture 2 2024
Introduction to programming
1. Introduction
2. Your first Java Program
• Compiling your program
• Executing your program
3. Modifying your first program
4. Displaying text
Introduction
Program:
A set of instructions written using a programming language for a
computer to follow
Programming:
The art & science of writing computer programs.
Programming Language:
A formal language used to write computer programs / algorithm
Executing a program:
the act of carrying out program instructions (AKA running the
program)
Java application programming
Use tools from the JDK to compile and run programs.
Executing a Computer Program
Program
Drawbacks?
• Programmer has to know the following:
• Operation codes
• Address of data in memory
• The architecture of the CPU
• Writing programs this way is prone to errors
• Different CPUs have different operation codes!
• Programs are difficult to read and modify.
Assembly Language
Assembly language:
Still a low-level language. And is machine dependent.
• Uses mnemonics to represent instructions and
memory cells
• MUST be translated into machine language.
Drawbacks
• Programmer still needs to think in terms of individual
machine instructions
But note that
• Program is more readable & "somewhat“ more
understandable
Assembler:
Program that translates a program written in assembly
language into an equivalent program in machine language.
High-level Language
• Developed to make programming even more easier
• Closer to English and Secondary School Algebra
• Easier to read and understand
• Machine independent
• Examples: C, C++, Java, Python, Pascal, Ada, COBOL, etc
Note
• Program is readable
• Programmer does not have to know the individual
machine instructions: components have been abstracted
away.
High-level Language Continued
• High-level languages allow us to write portable
programs (machine independent)
BUT,
• MUST be translated (compiled) into machine
language
• Compiled program can usually be executed on the
computer it was compiled on (or a similar one).
Compiler:
A program that translates a program written using
a high-level language into an equivalent machine
language program.
A Typical Java Development Environment
Compiler 1
(IBM)
Editor
Compiler 2
Java Source Code File (Sun) Java Bytecode
(HelloWorld.java) (HelloWorld.class)
Compiler 3
(Apple)
Machine Instructions
Output Data
(Execution)
A Typical Java Development Environment
Phase 5: Execution
◦ The JVM executes the program’s bytecodes.
◦ JVMs typically execute bytecodes using a
combination of interpretation and so-called just-in-
time (JIT) compilation.
◦ Analyzes the bytecodes as they’re interpreted
◦ A just-in-time (JIT) compiler—such as Oracle’s Java
HotSpot compiler—translates the bytecodes into
the underlying computer’s machine language.
The Programming Process
• Systematic steps involved in solving a programming
problem.
Define
Problem
Coding
17
Programming Process Cont.
1. Defining the problem
• State the problem concisely
• A lot of software systems fail due to vague problem
definition.
• Usually specified by Management and Systems Analyst.
2. Analysis / Algorithm Design
• Understand the problem completely.
• The requirements (input, output, processing)
• Data representation for data to be processed
• Design an algorithm to solve the problem (specify it using
pseudocode)
• Check the algorithm for correctness (test data and/or
mathematical analysis) 18
Programming Process Cont.
3. Coding
• Implement your algorithm using an appropriate language
• Fix all syntax errors
• Properly document your source code
4. Maintenance
• Use the program
• Execute the program
• Fix all logical and run-time errors
• If requirements change, adapt the program
19
Java Programming Process
Execute Compile
(Interpreter) (Compiler) Coding
Syntax errors
Results
Logic and runtime errors
Java Programming Basics
a) Structure of a Java Application
public class HelloWorld
{
public static void main (String[] args)
{
<statements>; // Java statement(s) goes here
}
}
• HelloWorld: the class identifier, AKA class name
• Has to match the file name (with a .java extension)
• Program should be saved in a file called HelloWorld.java
• Java statements end with a semi-colon (‘;’)
• Omitting these would result in syntax errors.
- Be warned! 21
Java Program Structure
Class declaration
public class Welcome1
◦ Every Java program consists of at least one class that you
define.
◦ class keyword introduces a class declaration and is
immediately followed by the class name.
◦ Keywords (Appendix C) are reserved for use by Java and are
always spelled with all lowercase letters.
Filename for a public Class
A public class must be placed in a file that has a filename of the
form ClassName.java, so class Welcome1 is stored in the file
Welcome1.java.
Copyright © 2018 Pearson Education, Ltd. All Rights Reserved
Class Names and Identifiers
System.out object
◦ Standard output object.
◦ Allows a Java application to display information in the
command window from which it executes.
System.out.println method
◦ Displays (or prints) a line of text in the command window.
◦ The string in the parentheses is the argument to the method.
◦ Positions the output cursor at the beginning of the next line in
the command window.
Most statements end with a semicolon.
Compiling Your First Java Application