Lec 03 - Introduction to Programming
Lec 03 - Introduction to Programming
on to
Programm
ing
Lecture 3
Contents
Introduction to Programming
Introduction to Object-Oriented
Programming
Data Types
Variables
Operators
Standard Output
Standard Input
Strings
Strings Formatting
Arrays [ Java ]
Lecture 1 Sample Programs
Terminology
Software: A set of statements written in a programming language use to
operate computers
Statement: A line of code in a software program.
Snippet: A block of statements grouped together.
SD: Software Development – The process of creating a software program.
OOP: Object Oriented Programming – Program composed of interconnected
objects at runtime.
Expression: An entity-code component of a statement that can be evaluated to
produce a value.
Assign: The process of giving a combination of one or many expressions (result
is a value) on the right-hand side of the ‘ = ‘ to the left hand-side. Value: Data
Introduction to Programming
A program (software) is a set of code (statements) written in a
programming language. The program communicates with
computers to performs tasks and operations, such as:
Generate
Connect to a Display Output
Random Modify Text
Database to Screen
Numbers
Reference: https://en.wikipedia.org/wiki/Software
Introduction to Programming
Programming is the process of creating a program (software) using a
programming language. Each programming language has its own:
- Vocabulary (keywords, identifiers)
- Grammar (structure, syntax)
- Punctuation (operators, delimiters)
Reference: https://www.webopedia.com/definitions/programming-language/
Introduction to Programming
A programming language is either compiled or interpreted. Compilers
and interpreters turn the high-level programming language code into
low-level machine code for the computer to understand and process.
Develop
and
Debug
in time (instance).
… object is created when the code in the class template written in Java
… all objects created from a class have access to the methods defined
in that class.
Object Oriented Programming
Java Class Python Class
Structure { Optio
import packages Structure { Optio
import packages
nal} nal}
English comments { Optio English comments { Optio
nal} nal}
Class header { Requ Class header { Requ
ired} ired}
Fields { Optio Fields { Optio
nal} nal}
Constructors { Requ Constructors { Requ
ired} ired}
Methods { Optio Methods { Optio
nal} nal}
toString() { Optio __str__() { Optio
nal} nal}
Object Oriented Programming
Java type-strict declarative
programming language:
- Variable type must be specified
- Function type must be specified
- Object type must be specified
total = calculator.total(value)
print("Current value = ",total)
+- +-
== != == !=
|| or
Assignment = += -= *= /= %= = += -= *= /= %= **=
Example:
4 + 2 * 3 = 10
(due to the higher precedence of
multiplication 2*3 is evaluated first)
( 4 + 2 ) * 3 = 18
(due to the higher precedence of
parentheses 4+2 is evaluated first)
Operators
Arithmetic Operators:
Example:
Java Python
int x = 2;
int y = 4;
Code x = 2;
Code
#declare and initialize
y = 4
int z = 1/2 #z holds the value 0.5
/* #python allows decimal division by default
z holds the value 0 since both operands are z = x/y;
integers the division is integer division.
To obtain 0.5 either x or y should be type-
casted to floating point
*/
Operators
Logical Operators:
Example:
Java Python
boolean x = trueCode
; x = True ; Code
boolean y = false; y = False;
// q is true
boolean q = (5%2 != 2) ? true : false; # q is True
q = True if (5%2 != 0) else False;
Standard Output
Jav Pytho
a n
The System class is part of the Displaying data to STDOUT with
java.lang package, which is Python is simple using the function
automatically imported into every print( ).
Java program.
- System.out (standard output print(“Welcome to Python”);
stream) Will output the string to the
- System.err (standard error STDOUT terminal :
stream) Welcome to Python
- System.in (standard input
stream)
Standard Input
Jav
a
Pyth
Scanner class in Java is part of the
on
java.util package. You need to Reading data with Python is simple.
import it at the beginning of your It can be accomplished using the
code. input function or by-passing
import java.util.Scanner; arguments to the scripts at
execution phase:
Java provides various ways to read
input from the keyboard, using the var = input(“prompt: ”) #var is a
System.in open stream. string type
nextInt() (read an integer)
nextLine() (read a line or a OR
String)
Standard Input
Example: Java
Code
import java.util.Scanner; Python
import sys Code
#has the argv list
public class Inputs{
// Create a Scanner object to read from
System.in (keyboard input) x = int(input("x = "))
static Scanner in = new Scanner(System.in);
print("x squared = ",pow(x,2))
public static void main(String[] args){
System.out.print("x = ");
#sys.argv[1] stores first argument
int x = in.nextInt();
System.out.println("x squared = "+Math.pow(x,2)); after the script name
} y = sys.argv[1]
} print("y = ",y)
Example: Considerindex
String s = “hello”; 0 1 2 3 4
index 0 1 2 3 4
Example: Consider s = “hello”;
Jav Pyth
a on
• Using the DecimalFormat class • Using the modulo % operator for
string formatting
from java.text package • Using the format() function from
• Using the printf() method from string
the System class • Using f command for string
formatting (an f-string or a string
• Using the format() method from
literal prefixed with ‘f’ or ‘F’)
the System class
• Using the format() method from
Strings Formatting
Example (Java): Consider variables: String name =
“Tom”; double balance = 12.4556;
The objective
String is to display the output: <name> has
pattern = "####,####.##"; Tom has 12.46
DecimalFormat df = new DecimalFormat(pattern);
<balance>
System.out.println(name+” has ”+df.format(balance));
<balance>
print(“%10s has %.2f”%(name,balance)) 7 spaces Tom has 12.46
NOTE: Arrays in Java have fixed size, once they are created the size does
not change
Arrays (Java)
Example:
int a1 []; RAM
Declaring the a1 array a1
11
By default a1 points to null
null {Memory
in the RAM
location for a1}
a1 = new int[3];
22 Creating the a1 array a1
By default a1 values are int
default (zeros) 0 0 0 {Memory
a1[0] = 1; location for a1}
a1[1] = 7; {a1 values
33
a1[2] = -3; updated}
Initializing the a1 array 1 7 -3
Arrays (Java)
Access:
Arrays in Java are indexed in a proper order. Array elements are
accessed using the index of these elements.
Syntax: Accessing
Given:
<data type>[] <variable-name> = {<value0>, <value1>,…, <value(size-1)>};
<data type> x0 = <variable-name>[0]; //Accessing first element
x0 x1 x2 … … xn
0 1 2 … … size-1
<variable>
Arrays (Java)
Example: Creating, Initializing, Accessing, and Modifying
int x [] = {2,4,-1,11,3};
● Declaring an integer array
String s = Arrays.toString(x);
● Creating and Initializing the array
System.out.println(s);
class Geometry:
def __init__(self,radius):
self.radius = radius
geo = Geometry(3)
print(geo)
Thank You