Вы находитесь на странице: 1из 30

COMP1022P Introduction to Computing with Java

Topic 1: Introduction to Problem Solving and


Computer Programming
Dr. Desmond Tsoi

Department of Computer Science & Engineering


The Hong Kong University of Science and Technology
Hong Kong SAR, China
Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 1 / 30
Part I

Problem Solving, Algorithms &


Computer Programming Languages

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 2 / 30


Problem Solving
There are tones of real-world
problems need to be solved in
order to make our life easier
These problems are typically
solved using computers, because
they can perform operations
much faster than human do
But computers are intelligent
only if humans are able to
instruct / teach them to
perform relevant tasks
To do so, a step-by-step
description of how to
accomplish a task is needed
I We call this description an
”algorithm”
Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 3 / 30
Algorithm and its Representations

A step-by-step description can be represented in many different ways


I Flowcharts
I Pseudo code
I Computer programs

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 4 / 30


Example - Algorithm and its Representation

Example: Calculate discount-rate for customers in a retail store

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 5 / 30


Representation: Flowcharts and Pseudo Code

READ Membership Card No.


READ ItemPrice
IF Membership Card No. is valid THEN
price = ItemPrice * 0.8;
ELSE
price = ItemPrice;
END IF

Pseudo code Flowchart


Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 6 / 30
Representation: Computer Program(s)
import java.util.Scanner;
public class DiscountProcess {
public static boolean isValid(int num) {
// Validation
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cardNo = sc.nextInt();
int itemPrice = sc.nextInt();
int price;
if(isValid(cardNo))
price = itemPrice * 0.8;
else
price = itemPrice;
}
} Computer program written in Java

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 7 / 30


Computer Programs and Programming
import java.util.Scanner;
public class DiscountProcess {
public static boolean isValid(int num) {
// Validation
A computer program }
public static void main(String[] args) {
I Set of step-by-step Scanner sc = new Scanner(System.in);
instructions int cardNo = sc.nextInt();
int itemPrice = sc.nextInt();
I Perform specific tasks int price;
if(isValid(cardNo))
to solve a problem price = itemPrice * 0.8;
I Examples: else
price = itemPrice;
F Computer games }
}
F Word processors
F Web browsers
F ...
Computer programming
is the design & the
implementation of
computer programs

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 8 / 30


Computer Programming
Learning computer programming is just like learning a natural
language such as English, Japanese, Korean, etc.
Although they are similar, it doesn’t mean that they are exactly the
same

Don’t worry. Actually this is good. Since computer programming is


more systematic and should be much easier to learn, in my opinion ;)
(Good news!)
Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 9 / 30
Programming Languages
Computer programs are written in programming languages
Different to those human languages, a programming language defines
A SET OF INSTRUCTIONS in SPECIFIC FORMAT that can be
given to a computer
Two important issues on writing programs:
1. Program syntax – Is the grammar of the instructions correct?
2. Program logic – Is the program able to solve the problem?

Will talk more about these in the next set of notes! :)

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 10 / 30


Categorization of Programming Languages

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 11 / 30


Compilers vs. Interpreters
A compiler is a computer program (or a set of programs), which
translates high-level language into machine language (or object code).
The job is done offline
I We say, it does the job at compilation time

A interpreter actually does the same job as compiler except that the
job is done at the moment when the program is run
I We say, it does the job at run time

Question:
Compiled code can be executed faster! Why?
Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 12 / 30
High-Level Languages

High-level languages can mainly


be divided into two categories:
I Procedural or Structured
Programming Languages
I Object-Oriented Programming
(OOP) Languages
Procedural or Structured
Programming Languages
I Examples: COBOL, Pascal, C,
etc.
Object-Oriented Programming
Languages
I Examples: Java, C++, etc.

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 13 / 30


Part II

Briefing on Java

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 14 / 30


A Brief History about Java

Java
I It is a name of an island of Indonesia
I It is also an informal name of a type
of brewed coffee :P
History:
I It is invented by a group people
working in Sun Microsystems in 1991
I One of the major contributor is James
Gosling
I Initially the language is named Oak.
Then it is changed to Java after
visiting a local coffee shop
I Now, it is one of the most important
general purpose OOP language

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 15 / 30


Java 2 Platform

Different Java editions for different purposes


Java 2 Platform, Standard Edition (J2SE)
I Used for developing client side standalone applications or applets
Java 2 Platform, Enterprise Edition (J2EE)
I Used for developing client side applications or programs involving
servers
Java 2 Platform, Micro Edition (J2ME)
I Used for developing applications for mobile devices
I Not popular now

We use Java 2 Standard Edition Version 8.0 (or J2SE 1.8).


A Java Development Kit 8.0 (also known as JDK 1.8) will be used.

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 16 / 30


Platform Independence

Java compiler
converts Java
program into
bytecode which is
platform independent
Java bytecode is
then interpreted and
translated by JI
(Java Interpreter) to
hardware-specific
machine code
JVM or VM = Java
Virtual Machine (It is
actually a program)

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 17 / 30


Java Program Development Tools
What do you need in order to write Java programs?

Two components.

1. Machine with Java Development


Kit (JDK) installed

( We use the JDK version 1.8 for


this course )

http://www.oracle.com/
technetwork/java/javase/
downloads/
jdk8-downloads-2133151.
html

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 18 / 30


Java Program Development Tools
2. Java Integrated Development
Environment (JIDE) (i.e. a
software with editor, Java
compiler & Java execution
program)
I Eclipse (We use Eclipse IDE

for Java Developers)


https://eclipse.org/
downloads/packages/
eclipse-ide-java-developers/
lunasr2
I BlueJ
I jGRASP
I NetBeans
I JBuilder

All these software are available at the download section of our course
website.
Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 19 / 30
Development Cycle of a Java Program
3 Steps
1. Write Java source
code* using an editor
and save the code to a
file with extension
.java
2. Compile source file
(.java) into bytecode
file (.class) [ If any
syntax error, go back
to step 1! ]
3. Run the bytecode file
(i.e .class)

* Source code means a collection of computer programming language


instructions

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 20 / 30


Part III

First Java Program


Let’s start writing the first Java program!

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 21 / 30


My First Java Program
Step 1: Write Java source code using an editor and save the code to a file
with extension .java
/* First Java Application Program to print a text
"Welcome to COMP1022P!" on the screen */
// Filename: WelcomeStudents.java

public class WelcomeStudents {


public static void main(String[] args) {
System.out.println("Welcome to COMP1022P");
}
}

Observation: The filename MUST BE


the same as the class name!
Note: Java is a CASE-SENSITIVE
LANGUAGE, i.e. it treats lower-case
and upper-case differently!
Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 22 / 30
My First Java Program (Cont’d)
Step 2: To compile your program ”WelcomeStudents.java”
First, opening up a command prompt
Click start → Run... and type ”cmd” in the
textbox
Change directory to where your source file
located, assume your location is:
C:\Documents and Settings\Desmond\Desktop\sample
Type
"cd C:\Documents and Settings\Desmond\Desktop\sample"

Then type:
javac WelcomeStudents.java
in the command prompt (where javac is
JDK Java compiler)
The compiler translates the Java source file ”WelcomeStudents.java” into
bytecode and saves it to the file ”WelcomeStudents.class”
Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 23 / 30
My First Java Program (Cont’d)
Step 3: To run the byte code with the Java interpreter
Type:
java WelcomeStudents
where java is JDK Java interpreter

The cursor is moved to the beginning of the next line, since println is used.
It will be explained more in the next lecture!
Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 24 / 30
Question

Am I supposed to compile and run Java programs using commands?

Actually you can do the same


things using some Java
Integrated Development
Environments (JIDEs) that we
introduced you earlier by simply
clicking on the icons.
But you are encouraged to also
learn how to do it using
commands, since not all
machines with JIDE(s) installed!

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 25 / 30


Key Terms

Algorithms Object-Oriented
Pseudo Code Programming
Computer Programs J2SE (Java 2 Standard
Edition)
Computer Programming
Bytecode
High Level Language
JI (Java Interpreter)
Assembly Language
JVM (Java Virtual Machine)
Machine Language
JIDE (Java Integrated
Compiler
Development Environment)
Interpreter
Case-Sensitive Language
Assembler
Procedural / Structured
Programming

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 26 / 30


Review Questions
1 // Filename: WelcomeAll.java
2 public class WelcomeStudents {
3 public static void main(String[] args) {
4 System.out.println("Welcome to COMP1022P");
5 }
6 }

Is there any error in the program? Write your answer(s).

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 27 / 30


Review Questions
Fill in the blanks in each of the following sentences about the Java
environment

1. The command from the J2SE Development Kit executes a


Java application.
2. The command from the J2SE Development Kit compiles a
Java program.
3. A Java program file must end with the file extension.
4. When a Java program is compiled, the file produced by the compiler
ends with the file extension
5. The file produced by the Java compiler contains that are
executed by the Java Virtual Machine (JVM).

Answer: 1. Java, 2. javac, 3. .java, 4. .class, 5. bytecodes

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 28 / 30


Further Reading
Read Section 1.4, 1.7 - 1.10, 2.1 - 2.4 & 4.2 - 4.3 of
”Java – How to program” textbook

Read the ”Guide about how to setup Eclipse for Java Developers” –
Available at our course website
https://course.cse.ust.hk/comp1022p/web_preview/
eclipse/index.html

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 29 / 30


That’s all!
Any questions?

Rm 3521, desmond@cse.ust.hk COMP1022P (Spring 2017) 30 / 30

Вам также может понравиться