![Page 1: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/1.jpg)
Introduction to programming in java
![Page 2: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/2.jpg)
• Input and output to screen with Java program
• Structure of Java programs
• Statements
• Conditional statements
• Loop constructs
• Arrays, character and string handling
• Functions
Syllabus
![Page 3: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/3.jpg)
• Structure of Java programs
• Input and output to screen with Java program
• Statements (what is a statement)
Lecture Outcomes
![Page 4: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/4.jpg)
Books & References
Introduction to Java and Object Oriented Programming (Volume 1)
1.Chapter 2.After todays Lecture you should be able to complete all exercisesIn Section 2.10, page 14.
2.Chapter 3If 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/
![Page 5: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/5.jpg)
• Structure of Java programs
• Compiling and running the program
• Printing messages to the screen
Contents for Today’s Lecture
![Page 6: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/6.jpg)
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.
![Page 7: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/7.jpg)
class class-name {
public static void main(String args[]) {
statement1;
statement2;
…
…
}
}
Structure of Java Programs“class-name.java”
![Page 8: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/8.jpg)
8
A statement written in Java
println(“Hello World!");
String hello = “Hello World!";
println(hello);
every statement isterminated with a ;
![Page 9: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/9.jpg)
“First.java”
Public class First {
public static void main(String args[]) {
System.out.println(“Hello World”);
}
}
Example Program
statement
![Page 10: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/10.jpg)
10
Creating and Compiling Programs
• On command line– javac file.java
Source Code
Create/Modify Source Code
Compile Source Code i.e. javac Welcome.java
Bytecode
Run Byteode i.e. java Welcome
Result
If compilation errors
If runtime errors or incorrect result
![Page 11: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/11.jpg)
11
Executing Applications
• On command line– java classname
JavaInterpreter
on Windows
JavaInterpreter
on Sun Solaris
JavaInterpreteron Linux
Bytecode
...
![Page 12: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/12.jpg)
12
Examplejavac Welcome.java
java Welcome
output:...
![Page 13: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/13.jpg)
Compile and run java command line
Compile javac file-name.java
Run java filename
Compile javac HelloWorld.java
Run java HelloWorld
Example: HelloWorld.java
![Page 14: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/14.jpg)
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
Compiling & Running the Program
![Page 15: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/15.jpg)
“second.java”
class second {
public static void main(String args[]) {
System.out.println(“Hello World”);
}
}
Example Program
Compile javac second.java
Run java second
![Page 16: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/16.jpg)
“HelloWorld.java”
class HelloWorld{
public static void main(String args[]) {
System.out.println(“Hello World”);
}
}
Example Program
Compile javac HelloWorld.java
Run java HelloWorld
![Page 17: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/17.jpg)
1. HelloWorld java
2. Welcome.java
3. Myname.java
4. MyDate ofBirth.java
Compile Run a few example using Command line
![Page 18: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/18.jpg)
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
About Printing on the Screen
![Page 19: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/19.jpg)
19
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
![Page 20: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/20.jpg)
ExampleWelcome.java
public class Welcome
{
public static void main(String args[])
{
System.out.print("Welcome ");System.out.println("to");System.out.println(“java!");
}
}
}
}Welcome to java!Output
![Page 21: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/21.jpg)
ExampleWelcome3.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!");
}
}
}
}Welcome to java!Output
![Page 22: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/22.jpg)
• 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.
Some Tips About Programming
![Page 23: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/23.jpg)
23
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
Hello World
*/
good to make severallines of comments standout in your program
![Page 24: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/24.jpg)
24
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
![Page 25: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/25.jpg)
ExampleConcatenate.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 “ + -2);
}
{
}
}I like programming in javaI like programming in javasquare root of 4 = 2 or -2Output
![Page 26: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/26.jpg)
ExampleWelcome.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!");
}
}
}
}
Welcome to java!Welcome to java!Output
![Page 27: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/27.jpg)
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. (you will need to use concatenation.)
Some Assignments
![Page 28: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/28.jpg)
EndUsing Command line Arguments
public class TestMain
{
public static void main(String args[])
{
. . .
}
}
java TestMain arg0 arg1 arg2 … argn
}
}
![Page 29: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/29.jpg)
Processing Command-Line Parameters
The main method, get the arguments from
args[0], args[1], ..., args[n]
arg0, arg1, ..., argn
}
}
![Page 30: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/30.jpg)
ExampleArgument.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?");
}
}
}
} Hi, Lahcen. How are you?
Java Argument Lahcen
![Page 31: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/31.jpg)
ExampleArgument1.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?");
}
}
}
}Hi, java programs. How are you?
Java Argument java programs
![Page 32: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/32.jpg)
ExampleArgument2.java
public class Argument2
{
public static void main(String args[])
{
System.out.print("Hi, “+ args[0] + “ ”args[1] + (". How are you?");
}
}
}
}
Hi, java programs. How are you?
Java Argument java programs
![Page 33: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/33.jpg)
summary
• HelloWorld.java• Compile and run java programms.• print/println• “\n” new line• “\t” tab• Concatenation• Use of Arguments.
![Page 34: Introduction to programming in java. nlp-ai@cse.iitb Input and output to screen with Java program Structure of Java programs Statements Conditional statements](https://reader036.vdocuments.site/reader036/viewer/2022081504/5515c7da550346c6278b466a/html5/thumbnails/34.jpg)
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 exercisesIn Section 2.10, page 14.
2.Chapter 3If 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/