Chapter 1
Chapter 1
Chapter 1
Introduction to Java Programming
Subject
• Introduction
• Basic Syntax
• Conditional execution
• definition Loops
• Parameter and Objects
• File Processing
• Array
• Searching and Sorting
• GUI
2
Assesment
• UTS 20%
• UAS 35%
• Lab Activity, Home Works 10%
• Project 15%
• Quiz 20%
3
Tools
4
Standard Package
• java.lang
• java.io
• java.util
• java.net
• java.awt
5
What is programming?
• program: A set of instructions
to be carried out by a computer.
6
Programming languages
• Some influential ones:
– FORTRAN
• science / engineering
– COBOL
• business data
– LISP
• logic and AI
– BASIC
• a simple language
7
Basic Java programs with
println statements
Compile/run a program
1. Write it.
– code or source code: The set of instructions in a program.
2. Compile it.
• compile: Translate a program from one language to another.
– byte code: The Java compiler converts your code into a format
named byte code that runs on many computer types.
9
A 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!
• System.out.println("text");
Prints the given message as output.
• System.out.println();
Prints a blank line of output.
12
Names and identifiers
• You must give your program a name.
14
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 }
15
Syntax error example
1 public class Hello {
2 pooblic static void main(String[] args) {
3 System.owt.println("Hello, world!")_
4 }
5 }
• 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!
16
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."
– Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");
– Output:
\hello
how are "you"?\\
18
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");
19
Answers
• Output of each println statement:
a b c
\\
'
"""
C:
in he downward spiral
20
Questions
• What println statements will generate this output?
This program prints a
quote from the Gettysburg Address.
• 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. */
22
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
23
Comments example
/* Suzy Student, CS 101, Fall 2019
This program prints lyrics about ... something. */
// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}
24
Questions
• Write a Java program to print a face :
25
Questions
• Write a Java program to print the result of the following
operations
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
26