Programming Languages-Programming Foundation-Lecture Slides, Slides of Computer Programming

Prof. Dinakar Kity delivered this lecture for Basics of Programming course at Aligarh Muslim University. Its main points are: Programming, Idealized, Computer, Prepackaged, Software, Solutions, Natural, Languages, Java

Typology: Slides

2011/2012

Uploaded on 07/15/2012

sajeev
sajeev 🇮🇳

3.5

(2)

48 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1.1 Your First Program
Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · January 29, 2009 3:12 PM
2
Why Programming?
Idealized computer. "Please simulate the motion of a system of N
heavenly bodies, subject to Newton's laws of motion and gravity."
Prepackaged software solutions. Great, if it does exactly what you need.
Computer programming. Art of making a computer do what you want.
Ada Lovelace Analytic Engine
3
Languages
Machine languages. Tedious and error-prone.
Natural languages. Ambiguous and hard for computer to parse.
High-level programming languages. Acceptable tradeoff.
[ real newspaper headlines, compiled by Rich Pattis ]
Kids Make Nutritious Snacks.
Red Tape Holds Up New Bridge.
Police Squad Helps Dog Bite Victim.
Local High School Dropouts Cut in Half.
“Instead of imagining that our main task is to instruct a computer
what to do, let us concentrate rather on explaining to human
beings what we want a computer to do.” – Donald Knuth
4
Why Java?
Java features.
!Widely used.
!Widely available.
!Embraces full set of modern abstractions.
!Variety of automatic checks for mistakes in programs.
Java economy.
!Mars rover.
!Cell phones.
!Blu-ray Disc.
!Web servers.
!Medical devices.
!Supercomputing.
!
James Gosling
http://java.net/jag
$100 billion,
5 million developers
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Programming Languages-Programming Foundation-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

1.1 Your First Program

Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · January 29, 2009 3:12 PM 2

Why Programming?

Idealized computer. "Please simulate the motion of a system of N

heavenly bodies, subject to Newton's laws of motion and gravity."

Prepackaged software solutions. Great, if it does exactly what you need.

Computer programming. Art of making a computer do what you want.

Ada Lovelace Analytic Engine 3

Languages

Machine languages. Tedious and error-prone.

Natural languages. Ambiguous and hard for computer to parse.

High-level programming languages. Acceptable tradeoff.

[ real newspaper headlines, compiled by Rich Pattis ] Kids Make Nutritious Snacks. Red Tape Holds Up New Bridge. Police Squad Helps Dog Bite Victim. Local High School Dropouts Cut in Half. “Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.” – Donald Knuth 4

Why Java?

Java features.

! Widely used.

! Widely available.

! Embraces full set of modern abstractions.

! Variety of automatic checks for mistakes in programs.

Java economy.

! Mars rover.

! Cell phones.

! Blu-ray Disc.

! Web servers.

! Medical devices.

! Supercomputing.

James Gosling http://java.net/jag $100 billion, 5 million developers

6

Why Java?

Java features.

! Widely used.

! Widely available.

! Embraces full set of modern abstractions.

! Variety of automatic checks for mistakes in programs.

Caveat. No perfect language.

Our approach.

! Minimal subset of Java.

! Develop general programming skills that are applicable to:

C, C++, C#, Perl, Python, Ruby, Matlab, Fortran, Fortress, …

7

A Rich Subset of the Java Language

Primitive Numeric Types != <= >= == -- > < /

%

  • ++ * Integer.parseInt() Double.parseDouble() Parsing Math.min() Math.max() Math.sqrt() Math.pow() Math.abs() Math.PI Math.log() Math.sin() Math Library Math.exp() System.out.println() Math.cos() System.out.print() System.out.printf() System for if Flow Control while else ! || true Boolean && false , ; ( { Punctuation ) } a[i] new a.length Arrays charAt() matches() length()

String compareTo() "" char boolean long int Built-In Types String double toString() equals() new main() public class Objects private static = Assignment

Create, Compile, Execute

9

Programming in Java

Programming in Java.

! Create the program by typing it into a text editor, and

save it as HelloWorld.java

HelloWorld.java

*_ Prints "Hello, World"

  • Everyone's first Java program. *******************************************/_** public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }

14

Dr. Java

command-line argument

1.2 Built-in Types of Data

Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · January 29, 2009 3:11 PM 2

Built-in Data Types

Data type. A set of values and operations defined on those values.

add, subtract, multiply, divide 3.1 415 6.022e floating point numbers double add, subtract, multiply, divide 17 int integers 12345 boolean truth values false^ true and, or, not sequences of characters characters type set of values literal values operations char^ 'A' '@' compare String "Hello "CS is^ World" fun" concatenate 3

Basic Definitions

Variable. A name that refers to a value.

Assignment statement. Associates a value with a variable.

4

Trace

Trace. Table of variable values after each statement.

Text

6

Text

String data type. Useful for program input and output.

7

Subdivisions of a Ruler

% java Ruler 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 "1 2 1" "1 2 1 3 1 2 1" "1" public class Ruler { public static void main(String[] args) { String ruler1 = "1"; String ruler2 = ruler1 + " 2 " + ruler1; String ruler3 = ruler2 + " 3 " + ruler2; String ruler4 = ruler3 + " 4 " + ruler3; System.out.println(ruler4); } } (^) string concatenation

12

Floating-Point Numbers

double data type. Useful in scientific applications.

13

Math Library

14

Quadratic Equation

public class Quadratic { public static void main(String[] args) { // parse coefficients from command-line double b = Double.parseDouble(args[ 0 ]); double c = Double.parseDouble(args[ 1 ]); // calculate roots double discriminant = bb - 4.0c; double d = Math.sqrt(discriminant); double root1 = (-b + d) / 2.0; double root2 = (-b - d) / 2.0; // print them out System.out.println(root1); System.out.println(root2); } }

Ex. Solve quadratic equation x^2 + bx + c = 0.

roots = " b^ ±^ b (^2) " 4 c 2 15 % java Quadratic – 3.0 2.

% java Quadratic – 1.0 – 1.

-0. % java Quadratic 1.0 1. NaN NaN % java Quadratic 1.0 hello java.lang.NumberFormatException: hello % java Quadratic 1. java.lang.ArrayIndexOutOfBoundsException

Testing

Testing. Some valid and invalid inputs.

command-line arguments not a number golden ratio x^2 – 3x + 2 x^2 – x - 1 x^2 + x + 1

Booleans

17

Booleans

boolean data type. Useful to control logic and flow of a program.

18

Comparisons

Comparisons. Take operands of one type and produce an operand of

type boolean.

19

Leap Year

Q. Is a given year a leap year?

A. Yes if either (i) divisible by 400 or (ii) divisible by 4 but not 100.

public class LeapYear { public static void main(String[] args) { int year = Integer.parseInt(args[ 0 ]); boolean isLeapYear; // divisible by 4 but not 100 isLeapYear = (year % 4 == 0 ) && (year % 100 != 0 ); // or divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0 ); System.out.println(isLeapYear); } } % java LeapYear 2004 true % java LeapYear 1900 false % java LeapYear 2000 true