Download Introduction to programming in java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Class (computer programming) wikipedia , lookup

Go (programming language) wikipedia , lookup

Library (computing) wikipedia , lookup

String literal wikipedia , lookup

Comment (computer programming) wikipedia , lookup

Structured programming wikipedia , lookup

C Sharp syntax wikipedia , lookup

Name mangling wikipedia , lookup

Object-oriented programming wikipedia , lookup

Java syntax wikipedia , lookup

Scala (programming language) wikipedia , lookup

Java (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Java performance wikipedia , lookup

Transcript
Introduction to programming in
java
Syllabus
• Input and output to screen with Java program
• Structure of Java programs
• Statements
• Conditional statements
• Loop constructs
• Arrays, character and string handling
• Functions
[email protected]
Lecture Outcomes
• Structure of Java programs
• Input and output to screen with Java program
• Statements (what is a statement)
[email protected]
Books & References
Introduction to Java and Object Oriented Programming (Volume 1)
1. Chapter 2.
After todays Lecture you should be able to complete all exercises
In Section 2.10, page 14.
2. Chapter 3
If you are confident with all the material in Chapter 2, then start
Reading Chapter 3.
3. Extra
More practise exercises are on page:
http://introcs.cs.princeton.edu/java/11hello/
Contents for Today’s Lecture
• Structure of Java programs
• Compiling and running the program
• Printing messages to the screen
[email protected]
Some Basics
Definition of a program?
A sequence of instructions that a computer can interpret and execute.
Why don’t we just use natural languages such as English?
A computer is not intelligent enough to understand natural languages.
[email protected]
Structure of Java Programs
“class-name.java”
class class-name {
public static void main(String args[]) {
statement1;
statement2;
…
…
}
}
[email protected]
A statement written in Java
println(“Hello World!");
every statement is
terminated with a ;
String hello = “Hello World!";
println(hello);
8
Example Program
“First.java”
Public class First {
public static void main(String args[]) {
System.out.println(“Hello World”);
}
statement
}
[email protected]
Creating and Compiling Programs
Create/Modify Source Code
• On command line
– javac file.java
Source Code
Compile Source Code
i.e. javac Welcome.java
If compilation errors
Bytecode
Run Byteode
i.e. java Welcome
Result
If runtime errors or incorrect result
10
Executing Applications
• On command line
– java classname
Bytecode
Java
Interpreter
on Windows
Java
Interpreter
on Linux
...
Java
Interpreter
on Sun Solaris
11
Example
javac Welcome.java
java Welcome
output:...
12
Compile and run java
command line
Compile
Run
javac file-name.java
java filename
Example: HelloWorld.java
Compile
javac HelloWorld.java
Run
java HelloWorld
[email protected]
Compiling & Running the Program
Compiling: is the process of translating source code written in a particular
programming language into computer-readable machine code that can be executed.
$ javac First.java
This command will produce a file ‘First.class’, which is used for running the program
with the command ‘java’.
Running: is the process of executing program on a computer.
$ java First
[email protected]
Example Program
“second.java”
class second {
public static void main(String args[]) {
System.out.println(“Hello World”);
}
}
Compile
Run
javac second.java
java second
[email protected]
Example Program
“HelloWorld.java”
class HelloWorld{
public static void main(String args[]) {
System.out.println(“Hello World”);
}
}
Compile
javac HelloWorld.java
Run
java HelloWorld
[email protected]
Compile Run a few example using
Command line
1.
HelloWorld java
2.
Welcome.java
3.
Myname.java
4.
MyDate ofBirth.java
[email protected]
About Printing on the Screen
1.
System.out.println(“Hello World”); – outputs the string “Hello World” followed
by a new line on the screen.
2.
System.out.print(“Hello World”); - outputs the string “Hello World” on the
screen. This string is not followed by a new line.
3.
Some Escape Sequence –
•
\n – stands for new line character
•
\t – stands for tab character
[email protected]
Java print() and println()
• Text can be printed on the screen using print() or
println().
• Using println() puts a new line at the end of the
text.
print("7*3");
println("=");
println(7*3);
This code prints:
7*3=
21
19
Example
Welcome.java
public class Welcome
{
public static void main(String args[])
{
System.out.print("Welcome ");
System.out.println("to");
System.out.println(“java!");
}
}
}
}
Output
Welcome to
java!
Example
Welcome3.java ( includes \n and \t)
public class Welcome
{
public static void main(String args[])
{
System.out.print("Welcome \n ");
System.out.print("to \t");
System.out.println(“java!");
}
}
}
}
Output
Welcome
to
java!
Some Tips About Programming
• Some common errors in the initial phase of learning programming:
- Mismatch of parentheses
- Missing ‘;’ at the end of statement
- Case sensitivity
• Writing programs on your own is the best way to learn how to program.
Comments in java
• There are two ways of commenting code.
• Comments starting with // and terminated by end of line
// Lahcen Ouarbya
// 1 October 2012
// Hello World
• Comments enclosed in /* */
/* Lahcen Ouarbya
1 October 2012
good to make several
Hello World
lines of comments stand
out in your program
*/
23
Concatenating output with +
print("I like programming in ");
println("Java");
This code prints: I like programming in Java
print("I like programming in “ + “Java” );
This code prints: I like programming in Java
println(“ square root of 4 = " 2 + " or " -2);
This code prints: square root of 4 = 2
or -2
24
Example
Concatenate.java
public class Concatenate
{
public static void main(String args[])
{
System.out.print("I like programming in ");
System.out.println(“java");
System.out.println("I like programming in “ + “java”);
System.out.println(“ square root of 4 = “+ 2 + " or “ +
}
{
}
}
Output
I like programming in java
I like programming in java
square root of 4 = 2
or -2
-2);
Example
Welcome.java
public class Welcome
{
public static void main(String args[])
{
System.out.print("Welcome ");
System.out.print("to ");
System.out.println("Java!");
System.out.println(“Welcome “ + "to “+ " Java!");
}
}
}
}Output
Welcome to java!
Welcome to java!
Some Assignments 
1.
Write a program which prints the following information about at least 5
persons:
`Full Name ‘
`Email-Address’
` Telephone Number’
use print and println and see the difference.
2.
Write a program that prints the time table of 5 and time table of 9.
will need to use concatenation.)
(you
End
Using Command line Arguments
public class TestMain
{
public static void main(String args[])
{
. . .
}
}
java TestMain arg0 arg1 arg2 … argn
}
}
[email protected]
Processing
Command-Line Parameters
The main method, get the arguments from
args[0], args[1], ..., args[n]
arg0,
arg1, ..., argn
}
}
[email protected]
Example
Argument.java
public class Welcome
{
public static void main(String args[])
{
System.out.print("Hi, ");
System.out.print(args[0] + " " );
System.out.println(". How are you?");
Java Argument Lahcen
}
}
}
}
Hi, Lahcen. How are you?
Example
Argument1.java
public class Argument1
{
public static void main(String args[])
{
System.out.print("Hi, ");
System.out.print(args[0] + " " );
System.out.print(args[1] + " " );
System.out.println(". How are you?");
Java Argument java programs
}
}
}
}
Hi, java programs. How are you?
Example
Argument2.java
public class Argument2
{
public static void main(String args[])
{
System.out.print("Hi, “+ args[0] + “ ”args[1] + (". How are you?");
}
}
}
Java Argument java programs
}
Hi, java programs. How are you?
summary
•
•
•
•
•
•
•
HelloWorld.java
Compile and run java programms.
print/println
“\n” new line
“\t” tab
Concatenation
Use of Arguments.
More practice exercises.
Introduction to Java and Object Oriented Programming (Volume 1)
1. Chapter 2.
After todays Lecture you should be able to complete all exercises
In Section 2.10, page 14.
2. Chapter 3
If you are confident with all the material in Chapter 2, then start
Reading Chapter 3.
3. Extra
More practise exercises are on page:
http://introcs.cs.princeton.edu/java/11hello/