





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · January 29, 2009 3:12 PM 2
Ada Lovelace Analytic Engine 3
[ 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
James Gosling http://java.net/jag $100 billion, 5 million developers
6
7
Primitive Numeric Types != <= >= == -- > < /
%
String compareTo() "" char boolean long int Built-In Types String double toString() equals() new main() public class Objects private static = Assignment
9
HelloWorld.java
*_ Prints "Hello, World"
14
command-line argument
Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · January 29, 2009 3:11 PM 2
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
4
6
7
% 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
13
14
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); } }
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
command-line arguments not a number golden ratio x^2 – 3x + 2 x^2 – x - 1 x^2 + x + 1
17
18
19
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