0% found this document useful (0 votes)
33 views21 pages

Lecture 2 (Chapter 2)

Uploaded by

mahmoudezat507
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views21 pages

Lecture 2 (Chapter 2)

Uploaded by

mahmoudezat507
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Programming 1

Lecture 2
Chapter 2:
Introduction to Java Programming

Dr/ Mahmoud Gamal


1
A Java Program:

Output:

2
A Java Program:
• Package:
Group of classes.
• Class:
1. Data items
2. Methods ( or functions or operations)
• The main method is the entry point to the program.
• Java is case sensitive language.
• Console window:
Text box into which the program's output is printed.

3
Structure of a Java program

class: a program
public class name {
public static void main(String[] args) {
Body of
statement;
statement;
main method: a named group
method ...
statement; of statements
}
}
statement: a command to be executed

• Every executable Java program consists of a class,


• that contains a method named main,
• that contains the statements (commands) to be
executed.
System.out.println Statement:

• A statement that prints a line of output on the console.

• Two ways to use System.out.println :


• System.out.println("text");
Prints the given message as output.

• System.out.println();
Prints a blank line of output.
5
Example:
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");
}
}
Output:
Hello, world!
This program produces
four lines of output
6
Compile/run a program
1. Write it.
• code or source code: The set of instructions in a program.
2. Compile it.
• javac: translates the program from Java to bytecode
• bytecode: runs on many computer types (any computer
with JVM)
3. Run (execute) it.
• output: whatever the programmer instructs the program
to do
source code byte code output
compile run Hello, World!
javac Hello.java java Hello
Identifier:
A name given to an item in your program.
• Must start with a letter or _ or $
• Do not use space or any special character.
• Do not use any reserved name like (void, class, int)
• Do not start with a number.

• legal:
_myName TheCure ANSWER_IS_42 $bling$
• illegal:
me+u 49ers side-swipe Ph.D’s

Example:
public class MyClass {
8
Syntax:
• Syntax: The set of legal structures and commands that can
be used in a particular language.
• Every basic Java statement ends with a semicolon ;
• The contents of a class or method occur between { and }

9
Programming Errors:
1- Syntax Errors:
• Detected by the compiler.

2- Runtime Errors:
• Causes the program to abort.

10
Programming Errors:
3- Logic Errors:
• Produces incorrect results.

11
Strings:
• string: A sequence of characters to be printed.
• Starts and ends with a " quote " character.
• The quotes do not appear in the output.
• Examples:
"hello"
"This is a string. It's very long!"

Restrictions:
• May not span multiple lines.
"This is not
a legal String."

• May not contain a " character.


"This is not a "legal" String either."12
Escape sequence:
• Escape Sequence: A special sequence of characters used
to represent certain special characters in a string.

\t tab character
\n new line character
\" quotation mark character
\\ backslash character

• Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");

• Output:
\hello
how are "you"?\\
13
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");

Answer:
a b c
\\
'
"""
C:
in he downward spiral
Questions
• Write a println statement to produce this output:
/ \ // \\ /// \\\

Answer:

System.out.println("/ \\ // \\\\ /// \\\\\\");


Questions
What println statements will generate this output?
This program prints a
quote from the Gettysburg Address.

"Four score and seven years ago,


our 'fore fathers' brought forth on
this continent a new nation."
Answer:

System.out.println("This program prints a");


System.out.println("quote from the Gettysburg Address.");
System.out.println();
System.out.println("\"Four score and seven years ago,");
System.out.println("our 'fore fathers' brought forth on");
System.out.println("this continent a new nation.\"");
Questions
• What println statements will generate this output?
A "quoted" String is
'much' better if you learn
the rules of "escape sequences."
Also, "" represents an empty String.
Don't forget: use \" instead of " !
'' is not the same as “

Answer:
System.out.println("A \"quoted\" String is");
System.out.println("'much' better if you
learn");
System.out.println("the rules of \"escape
sequences.\"");
System.out.println();
System.out.println("Also, \"\" represents an
empty String.");
System.out.println("Don't forget: use \\\"
instead of \" !");
System.out.println("'' is not the same as \"");
Comments:
• Comment: A note written in source code by the
programmer to describe or clarify the code.
• Comments are not executed when your program runs.

• Syntax:
// comment text, on one line
or,
/* comment text; may span multiple lines */
• Examples:
// This is a one-line comment.

/* This is a very long


multi-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

• Comments are useful for:


• Understanding larger, more complex programs.
• Multiple programmers working together, who
must understand each other's code.
Comments Example
/* Suzy Student, Fall 2022
This program prints lyrics about ... something.
*/

public class BaWitDaBa {


public static void main(String[] args) {
// first verse
System.out.println("Bawitdaba");
System.out.println("da bang a dang diggy
diggy");
System.out.println();

// second verse
System.out.println("diggy said the
boogy");
System.out.println("said up jump the
boogy");
}
}
print vs println:
println

print

You might also like