Lec 1
Lec 1
Programming I
Email: [email protected]
TAs:
Miss. Mirna Hosny
Mr. Ahmed Moustafa
Text Book
– FORTRAN
• science / engineering
– COBOL
• business data
– LISP
• logic and AI
– BASIC
• a simple language
Some modern languages
• procedural languages: programs are a series of commands
– Pascal (1970): designed for education
– C (1972): low-level operating systems and device drivers
• functional programming: functions map inputs to outputs
– Lisp (1958) / Scheme (1975), ML (1973), Haskell (1990)
• object-oriented languages: programs use interacting "objects"
– Smalltalk (1980): first major object-oriented language
– C++ (1985): "object-oriented" improvements to C
• successful in industry; used to build major OSes such as Windows
– Java (1995): designed for web apps/servers
• Created by James Gosling and released by Sun microsystems
• Runs on many platforms (Windows, Mac, Linux, cell phones...)
Process of Programming
• Code: describes program fragments (e.g., “four lines of code”) or the
act of programming (e.g., “Let’s code this into Java”)
• The process of execution is often called running
• Program is stored in the computer as a series of binary numbers known
as machine language
– Machine language programs are executable programs
– Modern programmers use high-level programming languages such as
C++ and Java.
Source Running
Editor Compiler
File Byte code JVM Program
(IDE) (javac)
(.java)
MyProgram.java MyProgram.class
Output
IDE (Editor)
• Eclipse is an integrated development environment
(IDE).
– Syntax highlighting editor
– Easy to compile and execute programs
– Debugging (finding and eliminating errors in the
program)
Basic Java programs with println
statements
A Java program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
• Its output:
Hello, world!
2. Compile it.
– javac: translates the program from Java to bytecode
– bytecode: runs on many computer types (any computer with JVM)
• System.out.println("text");
Prints the given message as output.
• System.out.println();
Prints a blank line of output.
Another Java program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println();
System.out.println("This program produces");
System.out.println("four lines of output");
}
}
• Its output:
Hello, world!
• Compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:3: ';' expected
}
^
2 errors
– The compiler shows the line number where it found the error.
– The error messages can be tough to understand!
Other types of Errors
• Logic errors: occur when you write code that doesn’t
perform the task it is intended to perform BUG!
– Examples:
"hello"
"This is a string. It's very long!"
• Restrictions:
– May not span multiple lines.
"This is not
a legal String."
– Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");
– Output:
\hello
how are "you"?\\
Questions
• What is the output of the following println statements?
System.out.println("\ta\tb\tc");
System.out.println("\\\\");
System.out.println("'");
System.out.println("\"\"\"");
System.out.println("C:\nin\the downward
spiral");
• Syntax:
// comment text, on one line
or,
/* comment text; may span multiple lines */
• Examples:
// This is a one-line comment.
Using comments
• Where to place comments:
– at the top of each file (a "comment header")
– at the start of every method (seen later)
– to explain complex pieces of code
// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}