0% found this document useful (0 votes)
19 views34 pages

Week 02 Theory Slides-S

This document provides an overview of introductory programming concepts including definitions of programs, programming languages, and Java. It defines a program as a set of instructions that direct a computer to process inputs and outputs. Programming languages allow programs to be written in a specific language with its own syntax and keywords. High-level languages like Java are easier for humans to read but run slower, while low-level languages like machine code and assembly are faster but harder for humans. Java is an object-oriented language that aims to be platform independent through the use of bytecode and a virtual machine.

Uploaded by

imauno55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views34 pages

Week 02 Theory Slides-S

This document provides an overview of introductory programming concepts including definitions of programs, programming languages, and Java. It defines a program as a set of instructions that direct a computer to process inputs and outputs. Programming languages allow programs to be written in a specific language with its own syntax and keywords. High-level languages like Java are easier for humans to read but run slower, while low-level languages like machine code and assembly are faster but harder for humans. Java is an object-oriented language that aims to be platform independent through the use of bytecode and a virtual machine.

Uploaded by

imauno55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

PROGRAMMING 1 – 2023

PRG510S

“Talk is cheap. Show me the code.”

― Linus Torvalds

Intro. to Java Programming


Languages
CONTENT OVERVIEW:

Administration issues

Definition of a program

A programming language

Programming languages and examples
High level programming languages
Low level programming languages

Java, Interpreters and compilers

120-seconds discussions

Solving Problems

Basic of CMD args in a java program demo 2
What is a Program?

Definition :
A program is a set of instructions that a computer can understand and
follow in order to process a set of inputs into a set of outputs
In simple terms, a program is a set of instructions that directs the
computer on how to do things.
Each program is used to solve a specific problem

3
Programming Language

A program is written in a specific programming language

Just like people use different languages e.g. English, Silozi, Afrikaans and
etc.;

a program can also be written in a specific language e.g. C++, C#, Pascal,
Visual Basic, Java and etc.

Each language uses a specific set of predefined words and has its own
syntax (rules of grammar)

A person who writes computer programs is called a programmer

Most modern programming languages are designed to be relatively easy
for people to understand and use: high-level languages 4
Programming Language

Computer programs are in written a specific programming language, using keywords
from that language.

The keywords that constitute a computer program are written as a logical sequence
that represent a step-by-step approach to solving a problem.

A step-by-step solution to a problem, when written in a language that can easily be
understood by people and not by computers, is called an Algorithm.

Algorithms are either written using pseudocode, flowcharts or Nassi-Schneiderman
diagrams.

The set of instructions for solving a problem that are written in a specific
programming language are often referred to as source code

Computers do not think on their own, if you give them wrong instructions they give
us wrong results (Garbage In Garbage Out) :Java ? 5
Programming Language

There are many programming languages.

Some languages are designed for specific purposes while other languages are
designed for general use and are known as general purpose programming languages

general purpose programming languages can be used for writing programming for
solving problems whose nature ranges from scientific, engineering, business, and etc

Two main categories :
• Low – level programming languages
• High – level programming languages

6
Low level Programming Languages –
machine language

All computers only understand one language known as machine
language .

Machine language consists of sequence of zeroes and ones

Example of an arbitrary code in machine language is as follows:
0010 1010 0001 1101
0011 1100 1010 1111
0101 0110 1101 0101
1101 1111 0010 1001

7
Drawbacks of machine language

The two major drawbacks machine language are:
It is very easy to mistakenly type in a 0 in place of 1 or vis versa, especially if the
sequence of binary number is quite long. It is also difficult to notice such a mistake.
It takes a long time to write a program in machine language, it even takes a longer
time for people to understand a program written in machine language.

As a result, only few people write programs in machine language

the old days, to make it easier to write programs, programmers invented
another low level programming language known as assembly language

is a symbolic form of machine language that is easier for people to read

8
Assembly language

Definition:
is a symbolic form of machine language that is easier for people to read

Assembly language helped programmers to avoid remembering instructions given in
form of long sequences of 0s and 1s.

It uses short, and easy-to-remember phrases such as JMP, MOV, ADD, etc which
represent specific machine-language instructions

It is easier to understand, write and to modify assembly language than machine
language.

Programs are modified when the program requirements change is not working
correctly the first time you wrote it

A person can only modify a program when he/she understands how that program
works so as to know what to and how to modify 9
An example of Assembly Language Program

An assembly language program that adds two numbers is
shown below (You need not worry about details):
MOV ax,5H
MOV bx,4H
ADD ax,bx

10
Programming Languages – assembler

Assembly language source code must be translated into
machine language for it to be understood by computers

To achieve this, programmers developed an assembler, which is
a special program that translate assembly language into
machine language.

Thus all programs which are written in assembly language must
be fed into an assembler

11
Disadvantages assembly language over
machine language

Programs written in assembly language run slower and take up more
memory space than their machine language equivalent

Programs written in assembly language cannot easily be transferred from
one computer to another

It is still tedious, time-consuming, and complicated to write a program in
assembly language. Only a few programmers are currently writing large
programs in assembly language

NB: A language, like machine language and assembly language, whose
instruction set is closer to the machine (hardware) is classified as a low-
level language
12
High Level Programming Languages

The easier it is to read and write a program written in a specific
programming language, the slower and the larger are the
programs it creates

Every programmer wants to create programs that are easy to
write, and which run very fast while taking up as little space as
possible

High level programming languages were created to overcome
the problem of understanding assembly language but have the
disadvantages of running slowly and requiring more space than
assembly language 13
Advantages of high-level programming languages
over Low level programming languages

Programs written in high-level languages:
are easier to read and write than those written in low level
programming languages
can easily be ported from one computer to another; without massive
rewriting, which is not the case with programs written in assembly and
machine languages

Disadvantages high-level languages:
different compiler or interpreter for each type of language or computer
system
NOT Java :slightly different and much more versatile approach: Compile
+ Interpreter 14
Examples of high-level programming
languages

FORTRAN • Objective-C

Pascal • Java

Ada • Visual Basic

Basic • C# (C Sharp)
• COBOL

C
• Javascript

C++ • PHP

Delphi • etc

Prescribed book PG 40 - 47

15
Java

popular and general-purpose programming language

Class-based, object-oriented, and designed to have as few
implementation dependencies as possible

Intended for Write Once, Run Anywhere (WORA)

Java was originally developed by James Gosling at Sun
Microsystems/Oracle (1991 - 1995*)

Quite reputable

First language used for Android app development

16
Java Features:

Very secure, multi-threaded and Object-Oriented Language

Platform Independence: Support for all major OSs

Free and powerful Development Environment

Portability and Scalability (from Mobile Applications to Enterprise
Computing)

Plenty of free (often Open Source) Libraries

Support for Web Development (e.g. Applets and Servlets) Good
Performance

Networking Support (Distributed Computing)
...and more that I forgot to mention 17
Java portability, Why?

Java compiler does not translate your program into the machine
language.

Instead it translates the program into a language called bytecode
• Not the machine language for any particular system/OS
• Mainly for hypothetical computers called virtual machine

Bytecode is then translated into machine language by Java Virtual
machine(JVM)

JVM is a kind of an interpreter.

All computers that runs Java hence need the JVM
18
Interpreter

An interpreter converts your source code into machine language and stores them in
memory each time you run the program

Interpretation is the process of instant translation of a source code into an executable
form; e.g. into a code that can be understood by a machine:
 During interpretation, first the source code is scanned for correctness
The source code is then translated into some intermediate internal form for execution
Interpretation does not produce a target machine code. Hence, target code is interpreted by
software rather than hardware

Programs that are written on interpreter based programming languages:
will always require the source code in order to run them
are slower to run than those that written on compiler based programming language

19
Compiler

A compiler converts your source code into machine language instructions the first
time you run your program. It saves those machine language instructions in a file;
usually called the EXE or executable file The EXE file can be run even if the source code is not
available

Compiled programs run are faster than those that use interpreters

The process of translating source code into machine code (compilation) consists of
several phases
a) Two of those phases that are worth mentioning here are Intermediate code generation In this phase a version
of the program is produced in some intermediate form which is not machine dependent but can run on different
machines.
• Examples of an intermediate code are
i) p-code which is produced after compiling a Pascal program.
ii) byte-code which is produced after compiling a java program.
b) Code Generation In this phase the source code is translated into the target machine code
20
Compiler and Interpreters

Interpreters
 Basic
 Prolog
 Javascript,PHP
 Lava, Smalltalk ..etc.

Compilers:
 Delphi
 C++
 Rust,Ada
• Compilers or Interpreters(byte code):
 C#, Java, Python ..etc.
21
120-Seconds Discussions

Lists at least 6 concepts learnt today

Add your own understanding to each concept

Which concept is not clear to you?

22
Problem solving

Problem :a situation or environment that people must seek a solution for.

Algorithm : precise and complete steps that should be followed in order
to solve a given problem: written in pseudocode or FlowChart

Pseudocode: mixture of English and certain programming language(java)
• Basically writing parts of the algorithm in the easiest language (English or Java)


Flowchart :a diagrammatic representation of an algorithm
• https://www.programiz.com/article/flowchart-programming 23
Pseudocode and Flowchart example

24
Problem solving life cycle
Source:
https://www.linkedin.com/pulse/teach-kids-life-
skills-abraham-femi-abraham

25
Problem solving Approaches

Abstraction
Examples:
o Drawing a plan when one wants to build a house
o Stating major components wants to have when building a car

Divide-and-conquer
• Examples:
o Creating different departments/sections in a milling company

Heuristics
Examples:
o Old people can tell whether it’s going to rain by looking at the type of clouds (experience )

Branch-and-bound

Reduction: transform-and-conquer
26
Anatomy of a Java program
Java comments. Are always
ignored by the compiler

This is the class header


for the class helloWorld

This area is the body of the


helloWorld class. All of the variable
and methods declarations for this
class will be between these curly
braces.

27
Anatomy of a Java program

main method body also enclosed by


{}. All of the actions to be completed
during execution are included here

This is a valid Java statement to print out


“Welcome to Programming 1”, notice the “ ”
enclosing the statement and the mandatory
terminating ;
28
Java Basic Syntax

Case Sensitivity − Java is case sensitive, which means identifier Hello and hello would have different meaning in
Java.

Class Names − For all class names the first letter should(recommended) be in Upper Case. If several words are
used to form a name of the class, each inner word's first letter should be in Upper Case.
 Example: class MyFirstJavaClass

Method Names − All method names should start with a Lower Case letter. If several words are used to form the
name of the method, then each inner word's first letter should be in Upper Case.
 Example: public void myMethodName()

Program File Name − Name of the program file should exactly match the class name.

When saving the file, you should save it using the class name (Remember Java is case sensitive) (if the file name
and the class name do not match, your program will not compile).
 Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'

public static void main(String args[]) − Java program processing/execution starts from the main() method which is
a mandatory part of every Java program. 29
Java Comments

Comments are notes that you write into your program to help a person( yourself or another programmer)
understand the program but that are ignored by the compiler

Line comments or Inline comments
 //This is an inline comment

Block comments or Multi-line comments.
 /*This is a multi-line comment.
Note that there is no comment symbol
of any kind on the second line.*/

JavaDoc or Documentation comments: use for API documentation
 /**
This program should only be used on alternate Thursdays,
except during leap years, when it should only be used on alternate Tuesdays.
*/
 Keywords: @Author,@Version…etc
30
Java Keywords and Reserved words

Words that have a specific meaning to the Java compiler and hence can not be used for any programmer
declarations or Identifier names.

Key words are lower case (Java is a case sensitive language), Examples:
abstract final public
assert finally return
boolean float short
break for static
byte goto strictfp
case if super
catch
(See Appendix 1, “Keywords” from your Absolute Java textbook)

31
Running Java from CMD with Arguments

Java portability means running and compiling can be done via CMD
No need for a specific IDE, but the JDK is a must
Make sure your PATH is correctly set (PRACTICAL)
Compile : javac <filename>.java
E.g. javac CustomGreeting.java
Run : java <filename> //No file extension
E.g. java CustomGreeting

CMD allows for arguments to be passed to the Main method
Hence :public static void main(String[] args)

32
Running Java from CMD with Arguments

Editing can be done with any Text Editor e.g. Notepad (Notepad++)

javac CustomGreeting.java

java CustomGreeting Desmond Tutu


Make sure the package declaration is removed if you are using an IDE before you run
you work on CMD, Below is the expected outcome:

33
Further Reading

https://www.tutorialspoint.com/java/
• Mobile app available : Java Offline Tutorial

https://www.javatpoint.com/how-to-set-path-in-java

https://www.javatpoint.com/java-tutorial

https://www.youtube.com/watch?v=6qp8AiEPMYk

Many many……many more out there !!!!

34

You might also like