Computer Programming
Applications
تطبيقات فى برمجة الحاسب
Lecture #2
Course Presentation by
Dr. Mohamed Ali
Learning Objectives
• Section I: Getting Started with Java Programming.
• Section II: Exploring the Parts of a Program.
2
Section I: Getting Started with Java Programming
• Introduction,
• 1st Java Example,
• Using a command-line argument.
3
Introduction
public class HelloWorld {
public static void main (String[] args)
{
[Link]("Hello World");
[Link]();
}
}
• Complete Java program its name is HelloWorld, which means that its
code resides in a file named [Link].
• The program’s sole action is to print a message back to the terminal
window.
• For continuity, we will use some standard Java terms to describe the
program, but we will not define them until later.
• The program consists of a single class named HelloWorld that has a
single method named main().
• This method uses two other methods named [Link]() to do
the job.
4
Introduction
• For the time being, you can think of “class” as meaning “program.”.
• The rest is a sequence of statements enclosed in braces and each
followed by a semicolon.
• For the time being, you can think of “programming” as meaning
“specifying a class name and a sequence of statements for its main()
method.
• In the next two sections, you will learn many different kinds of
statements that you can use to make programs.
• For the moment, we will just use statements for printing to the terminal
like the ones in Hello World.
5
1st Java Example
• When you type java followed by a class name in your terminal
application, the system calls the main() method that you defined in that
class, and executes its statements in order, one by one.
• Thus, typing java HelloWorld causes the system to call on the main()
method in and execute its two statements.
• The first statement calls on
[Link]() to print in
the terminal window the
message between the
quotation marks, and the
second statement calls on
[Link]() to
terminate the line.
6
1st Java Example
public class FirstJavaProgram {
public static void main (String[] args)
{
[Link]("Hello World");
}
}
• In Java programs, it is very important to keep in mind the following points:
• Case Sensitivity (Hello and hello would have different meaning in Java).
• Class Names (first letter should be in Upper Case, each inner word's first letter should be in
Upper Case also, due to code convention).
• Method Names (should start with a Lower Case letter, each inner word's first letter should
be in Upper Case).
• Program File Name (program file should exactly match the class name).
• public static void main(String args[]).
7
Using a Command-Line Argument
• This program shows the way in which we can control the actions of our
programs: by providing an argument on the command line. Doing so
allows us to tailor the behavior of our programs.
public class UseArgument {
public static void main(String[] args) {
[Link]("Hi, ");
[Link](args[0]);
[Link](". How are you?");
}
}
% javac [Link]
% java UseArgument Alice
Hi, Alice. How are you?
% java UseArgument Bob
Hi, Bob. How are you?
• Whenever UseArgument is executed, it reads the command-line argument
that you type after the program name and prints it back out to the terminal
as part of the message.
8
Section II: Exploring the Parts of a Program
• Introduction,
• The Elements in a Java Program: Keywords,
• The Elements in a Java Program: Identifiers,
• The Elements in a Java Program: Literals,
• The Elements in a Java Program: Punctuation,
• The Elements in a Java Program: Comments.
9
Introduction
• That both English and Java are called languages is no coincidence.
• You use a language to express ideas. English expresses ideas to
people, and Java expresses ideas to computers. What’s more.
• Both English and Java have things like words, names, and
punctuation.
• In fact, the biggest difference between the two languages is that
Java is easier to learn than English. (If English were easy, then
computers would understand English. Unfortunately, they can’t.
• In your school grammar class, you worried about verbs, adjectives,
and other such things. But in this course, you’ll think in terms of
keywords and identifiers.
10
Introduction
• Take an ordinary English sentence and compare it with Java code.
• Here’s the sentence:
Suzanne says “eh” because, as you know, she lives in Canada.
11
The Elements in a Java Program: Keywords
• A keyword is a dictionary word, a word that’s built right into a language.
• A word like “says” in English is a keyword because “says” plays the same role
whenever it’s used in an English sentence.
• Computer programs have keywords, too. In fact, the 1st Java program, uses four of
Java’s keywords (public, class, static and void).
public class UseArgument {
public static void main(String[] args) {
• Each Java keyword has a specific meaning, a meaning that remains unchanged
from one program to another.
• For example, whenever you write a Java program, the word public always signals a
part of the program that’s accessible to any other piece of code.
• The java proGRAMMing lanGUage is case-sensitive. This Means that if you change a
lowerCASE LETTer in a wORD TO AN UPPercase letter, you chANge the wORD’S
MEaning. ChangiNG CASE CAN MakE the enTIRE WORD GO FROM BeiNG
MEANINGFul to bEING MEaningless.
• You can’t replace public with Public. If you do, the WHOLE PROGRAM STOPS
WORKING.
12
The Elements in a Java Program: Keywords
• In Java, the words true, false, and null have specific meanings.
• Like the keywords, you can’t use true, false, and null to mean anything other
than what they normally mean in a Java program.
Java Keywords
abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
Java Keywords
13
The Elements in a Java Program: Keywords
• In Java, each keyword has an official, predetermined meaning.
• You can’t make up your own meaning for any of the Java keywords. For
example, you can’t use the word public in a calculation.
//This is BAD, BAD CODE:
public = 6;
• If you try to use a keyword this way, then the compiler displays an error
message and refuses to translate your source code.
• It works the same way in English. Have a baby and name it “Because.”
• Those keywords const and goto are reserved for non-use in Java.
• If you try to create a variable named goto, Eclipse displays an Invalid
VariableDeclaratorId error message.
14
The Elements in a Java Program: Identifiers
• A name is a word that identifies something, we will stop calling these things
names and start calling them identifiers.
• In computer programming, an identifier is a noun of some kind. An
identifier refers to a value, a part of a program, a certain kind structure, or
any number of things.
• If you have a new program or class, you can call it “HelloWorld” or “MyProg”
or even "xyz".
public class UseArgument {
public static void main(String[] args) {
• Just as the names Suzanne and Chrisanta have no special meaning in
English, so the names UseArgument and args have no special meaning in
Java.
• When you name Java program, you can use ThingsILike or HelloWorld, but
you can’t use the word public.
• KeyWords like class, public, static, and void are keywords in Java.
15
Identifiers With Agreed-Upon Meanings
• Most programming languages have identifiers with agreed-upon meanings
()المعاني المتفق عليها
• In Java, almost all these identifiers are defined in the Java API.
class ThingsILike {
public static void main(String args[]) {
[Link]("Chocolate, royalties, sleep");
}
}
• Here we have five such identifiers. They’re the words main, String, System, out,
and println.
• main: The main starting point for execution in every Java program.
• String args[]: A bunch of text; a row of characters, one after another.
• System: A canned program in the Java API. This program accesses some
features of your computer that are outside the direct control of the Java Virtual
Machine (JVM).
• out: The place where a text-based program displays its text. (For a program
running in Eclipse, the word out represents the Console view.
• println: Display text on your computer screen.
16
The Elements in a Java Program: Literals
• A literal is a chunk of text that looks like whatever value it represents.
• In Suzanne’s sentence, “eh” is a literal because “eh” refers to the word “eh.”
• Programming languages have literals, too, the stuff in quotes is a literal.
[Link]("Chocolate, royalties, sleep");
17
The Elements in a Java Program: Literals
• Most of the numbers that you use in computer programs are literals. If you
put the statement.
double mySalary = 2000.00;
//the 2000.00 is the Literal value
double oldSalary = 1000.00;
//the 1000.00 is the Literal value
18
The Elements in a Java Program: Punctuation
• Each bracket, each brace, each squiggle of any kind plays a role in making
the program meaningful.
class ThingsILike {
public static void main(String args[]) {
[Link]("Chocolate, royalties, sleep");
}
}
• Program are contained inside other parts
(nested).
• Notice how a pair of curly braces acts like a
box.
• To make the program’s structure visible at
a glance, you indent all the stuff inside of
each box.
• Eclipse can indent your code automatically
for you.
• Select the .java file whose code you want to
indent. Then, in Eclipse’s press Ctrl + Shift +
f. Eclipse rearranges your lines in the editor,
indenting things that should be indented and
generally making your code look good.
19
The Elements in a Java Program: Comments
• A comment is text that’s outside the normal flow.
• The first five lines form one big comment. The computer doesn’t act on this
comment.
• There are no instructions for the computer to perform inside this comment.
• Instead, the comment tells other programmers something about your
code.
/*
* A program to list the good things in life
* Author: Barry Burd, BeginProg@[Link]
* February 13, 2014
*/
class ThingsILike {
public static void main(String args[]) {
[Link]("Chocolate, royalties, sleep");
}
}
20
The Elements in a Java Program: Comments
• The Java programming language has three kinds of comments:
• Traditional comments (multiple line comments):
• The comment begins with /* and ends with */. Everything between the opening
/* and the closing */ is for human eyes only. Nothing between /* and */ gets
translated by the compiler.
• The second, third, and fourth lines, have extra asterisks. I call them “extra”
because these asterisks aren’t required when you create a comment. They
just make the comment look pretty.
• End-of-line comments (single line comments):
• An end-of-line comment starts with two slashes // and goes to the end of a line
of type.
• Javadoc comments:
• A special Javadoc comment is any traditional comment that begins with an
extra asterisk /** and end with */.
• This is a cool Java feature. The software that you can download from
[Link] includes a little program called javadoc. The javadoc program
looks for these special comments in your code. The program uses these
comments to create a brand new web page, a customized documentation
page for your code.
21
The Elements in a Java Program: Comments
• The Java programming language has three kinds of comments:
• Traditional comments (multiple line comments):
• End-of-line comments (single line comments):
• Javadoc comments:
22
The Elements in a Java Program: Comments
• The Java programming language has three kinds of comments:
• Traditional comments (multiple line comments):
• End-of-line comments (single line comments):
• Javadoc comments:
23
Thank you
24