Upload
pauline-shaw
View
246
Download
1
Embed Size (px)
Citation preview
Introduction to Programming Languages
Problem Solving in Programming
Problem Solving
Primitive and Higher calculations Computer programming is artifact (tool) used
for the Problem solving mechanism via computers
Few Terms
Program Programming Programability
Algorithm and program
Algorithm is sequence of steps usually written in natural language readable for humans
Programming is the algorithm translated in programming language so that computer can understand
Programming – By Example
Mean of three Numbers Instructions Program Counter Fetch and Execute Cycle
Flow Charts
FlowCharts
Is common type of chart used to represents an algorithm or process.
The steps are shown in boxes of various kinds, and their order by connecting these with arrows.
Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.
Flowchart The notations for flowchart are
Draw a flowchart that will compute the sum, average and product of three numbers.
Process Start / End
Decision Input / OutputT
F
How to develop Algorithm
Write Algorithm and draw flow chart of 10 numbers in a loop
Table of 2 Factorial
Jumping to Programming Languages
History (Paradigms) Unstructured Languages
Single continuous block of code e.g Assembly
Structured / Procedural Languages Functions and procedures e.g C, Pascal, Fortran
Object-Oriented Languages Real-world modeling (Attributes, Behavior) e.g C++, Java
Why we are going towards OOP? Object-oriented creatures Advantages:
Simpler, easier to read programs More efficient reuse of code Faster time to market More robust, error-free code
Concepts on the way ….
some basic questions??
What are the objects in OOP? What should be the objects in the design of
an object-oriented program? Is OOP better than previous programming
paradigms?
How will I understand?????
Computer and Humans
Computers understand 0’s and 1’s High level Language Machine level Language
Execution of a Program
Object Code
Source Code
Other Object Files
Linker
.exe fileExecute
Output
010101010101010101101000000……
Compiler
MyProg.class
MyProg.java
Compiler
Other class Files
Linker
A2 HJ JINJ JI JOJO 99 O1
JAR Archive
public class Sample{ public void main() { println( “hello”); }}
A2 N8 BH HJBK JI KO O9NJ JK L8 J9JI JO KJ KOK3 K9 98 90 ….
AB CD F3S6 JS JHN9 J5 33
JVM
hello
Java Interpreter
Java OOP language developed by SUN Microsystems Java is compiled into bytecodes Bytecodes are high-level, machine-independent
instructions for a hypothetical machine, the Java Virtual Machine (JVM)
The Java run-time system provides the JVM Extensive networking facilities Extensive set of APIs for GUIs, distributed
computing, 2D/3D graphics, mail, and others Portable: Write Once, Run Anywhere Multithreading support built into the language
Java Features Automatic garbage collection
No manual memory allocation and deallocation Never have to worry about memory leaks
No pointers or pointer arithmetic No off-by-one bugs
Arrays are first-class objects Array bounds are always checked
Multiple inheritance replaced by interfaces Eliminates complexities of multiple inheritance
Java Virtual Machine
Java is compiled into bytecodes Bytecodes are high-level, machine-independent
instructions for a hypothetical machine, the Java Virtual Machine (JVM)
The Java run-time system provides the JVM The JVM interprets the bytecodes during program
execution Since the bytecodes are interpreted, the performance of
Java programs slower than comparable C/C++ programs But the JVM is continually being improved and new
techniques are achieving speeds comparable to native C++ code
JVM All Java programs run on top of the JVM JVM was first implemented inside Web browsers, but
is now available on a wide variety of platforms JVM interprets the bytecodes defined in a machine-
independent file format called a class file
Types Of Java Programs
Application
Standalone Java program that can run independent of any Web browser
Applet
Java program that runs within a Java-enabled Web browser
Servlet
Java software that is loaded into a Web server to provide additional server functionality
Instructions for Java Installation Download and Install JDK NetBeans 6.7.1
Refference Book
The Complete Reference
Java 2 Fifth Edition. By Herbert Schildt
Hello World
class HelloWorld { public static void main(String args[]) { System.out.println("Hello World"); } }
Factorial
public class Factorial2{ public static void main(String args[]) { int n = 3; int fact = 1; for (int c = 1 ; c <= n ; c++ ) fact = fact*c; System.out.println("Factorial of "+n+" is = "+fact); }}
Table of Two or not ?
public class tables{ public static void main(String args[]) { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.print(i + "x" + j + "=" + (i * j) + "\t"); } System.out.println(); } }}
Tables Output
Tasks
Just print Table of Two. Find Factorial with proper solution as output
like 5! = 5 x 4 x 3 x 2 x 1 = 120. Each new term in the Fibonacci sequence is
generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Write a program to find out if a number is prime?