Lecture 02 - 2 Datatype, Variables, Operators, Expressions
Lecture 02 - 2 Datatype, Variables, Operators, Expressions
Lecture Title:
Introduction Java Programming
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.
4
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
4
Agenda
Hello, world!
Compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:3: ';' expected
}
^
2 errors
Examples:
"hello"
"This is a string. It's very long!"
Is used in method println
Restrictions:
May not span multiple lines.
"This is not
a legal String."
May not contain a " character.
"This is not a "legal" String either."
Output:
\hello
how are "you"?\\
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
22
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");
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. */
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
/
______
\ Run it to verify the output.
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
\ /
\______/ Divide the main method into static methods
+--------+
based on this structure.
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
______
/ \ This structure can be represented by methods:
/ \
| STOP | egg
\ /
teaCup
\______/
stopSign
______
/ \ hat
/ \
+--------+ Instructor: Dao, Nguyen Thi Anh
Professor: Lynn Robert Carter
Duy Tan University
37
Program version 2
public class Figures2 {
public static void main(String[] args) {
egg();
teaCup();
stopSign();
hat();
}
public static void egg() {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println();
}
public static void teaCup() {
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println("+--------+");
System.out.println();
}
...
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
38
Program version 2, cont'd.
...
public static void stopSign() {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("| STOP |");
System.out.println("\\ /");
System.out.println(" \\______/");
System.out.println();
}
public static void hat() {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
System.out.println("+--------+");
}
}
______
/ \
/ \
+--------+
eggBottom
______
/ \ line
/ \
+--------+
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
42
Program version 3
// Prints several figures, with methods for structure and redundancy.
public class Figures3 {
public static void main(String[] args) {
egg();
teaCup();
stopSign();
hat();
}
// Draws the top half of an an egg figure.
public static void eggTop() {
System.out.println(" ______");
System.out.println(" / \\");
System.out.println("/ \\");
}
// Draws the bottom half of an egg figure.
public static void eggBottom() {
System.out.println("\\ /");
System.out.println(" \\______/");
}
// Draws a complete egg figure.
public static void egg() {
eggTop();
eggBottom();
System.out.println();
}
Example:
public static void printWarning() {
System.out.println("This product causes cancer");
System.out.println("in lab rats and humans.");
}
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
47
Calling a method
Executes the method's code
Syntax:
methodName();
You can call the same method many times if you like.
Example:
printWarning();
Output:
This product causes cancer
in lab rats and humans.
Output:
Now this is the story all about how
My life got flipped turned upside-down
Answer:
Shaq is 7'1
The string "" is an empty message.
\'"\\"
*****
*********
*************
*****
*********
*************
* | | | | |*
*************
*****
*********
*************
*****
*********
*************
*****
* | | | | |*
* | | | | |*
*****