0% found this document useful (0 votes)
6 views

lect 2

The document is an introduction to problem solving and computer programming using Java, focusing on algorithms, programming languages, and the Java development environment. It covers the basics of writing and compiling Java programs, including the importance of algorithms and the structure of Java code. Additionally, it provides resources for further reading and learning about Java programming.

Uploaded by

wmy308.2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lect 2

The document is an introduction to problem solving and computer programming using Java, focusing on algorithms, programming languages, and the Java development environment. It covers the basics of writing and compiling Java programs, including the importance of algorithms and the structure of Java code. Additionally, it provides resources for further reading and learning about Java programming.

Uploaded by

wmy308.2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

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 3553, [email protected] COMP1022P (Spring 2020) 1 / 29
Part I

Problem Solving, Algorithms &


Computer Programming Languages

Rm 3553, [email protected] COMP1022P (Spring 2020) 2 / 29


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 3553, [email protected] COMP1022P (Spring 2020) 3 / 29
Algorithm and its Representations

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


I Flowcharts
I Pseudo codes
I Computer programs

Rm 3553, [email protected] COMP1022P (Spring 2020) 4 / 29


Example

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

Rm 3553, [email protected] COMP1022P (Spring 2020) 5 / 29


Algorithm Representation: Flowchart 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 3553, [email protected] COMP1022P (Spring 2020) 6 / 29


Algorithm Representation: Computer Program(s)
import java.util.Scanner;
public class DiscountProcess {
// Incomplete code Not a good
public static boolean isValid(int num) { representation of
// Validation algorithm as different
} programmers may use
public static void main(String[] args) { different programming
Scanner sc = new Scanner(System.in); languages to solve the
int cardNo = sc.nextInt(); problem
int itemPrice = sc.nextInt();
int price;
if(isValid(cardNo))
price = itemPrice * 0.8;
else
price = itemPrice;
sc.close();
}
} Computer program written in Java
Rm 3553, [email protected] COMP1022P (Spring 2020) 7 / 29
Computer Programs and Programming
import java.util.Scanner;
Once we come up an public class DiscountProcess {
algorithm, the next thing // Incomplete code
public static boolean isValid(int num) {
to do is to write a // Validation
computer program }
public static void main(String[] args) {
according to the steps in Scanner sc = new Scanner(System.in);
the algorithm int cardNo = sc.nextInt();
int itemPrice = sc.nextInt();
A computer program int price;
I if(isValid(cardNo))
Set of step-by-step price = itemPrice * 0.8;
instructions else
I Perform specific tasks to price = itemPrice;
sc.close();
solve a problem }
I Examples: }
F Computer games
F Word processors
F Web browsers
F ...

Computer programming is
the design and the
implementation of
computer programs
Rm 3553, [email protected] COMP1022P (Spring 2020) 8 / 29
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 there is a more systematic


way to learn computer programming and so it should be much easier
to learn, in my opinion ;) (Good news!)
Rm 3553, [email protected] COMP1022P (Spring 2020) 9 / 29
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 3553, [email protected] COMP1022P (Spring 2020) 10 / 29


Categorization of Programming Languages

Rm 3553, [email protected] COMP1022P (Spring 2020) 11 / 29


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 3553, [email protected] COMP1022P (Spring 2020) 12 / 29


Part II

Briefing on Java

Rm 3553, [email protected] COMP1022P (Spring 2020) 13 / 29


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
I Of course, it is also the name of a
programming language :D
History of Java programming language:
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 3553, [email protected] COMP1022P (Spring 2020) 14 / 29


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 13 (or J2SE 13).


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

Rm 3553, [email protected] COMP1022P (Spring 2020) 15 / 29


Platform Independence
Programs written in Java can be run on different platforms, e.g.
Windows, MacOS, Linux, etc.
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 3553, [email protected] COMP1022P (Spring 2020) 16 / 29
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 13


for this course )

https://www.oracle.com/
technetwork/java/javase/
downloads/
jdk13-downloads-5672538.
html

Rm 3553, [email protected] COMP1022P (Spring 2020) 17 / 29


Java Program Development Tools

2. Java Integrated Development


Environment (JIDE) (i.e. a
software with editor, Java compiler
& Java execution program)
I IntelliJ IDEA

https:
//www.jetbrains.com/idea/
I Eclipse
I BlueJ
I jGRASP
I NetBeans
I JBuilder

Please refer to the download section of our course website for JDK and
IntelliJ IDEA.

Rm 3553, [email protected] COMP1022P (Spring 2020) 18 / 29


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 3553, [email protected] COMP1022P (Spring 2020) 19 / 29


Part III

First Java Program


Let’s start writing the first Java program!

Rm 3553, [email protected] COMP1022P (Spring 2020) 20 / 29


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 3553, [email protected] COMP1022P (Spring 2020) 21 / 29
My First Java Program (Cont’d)
Step 2: To compile your program ”WelcomeStudents.java”
First, open up a command prompt
Hold “Windows Key” and press “R” , type
“cmd” in the textbox, and press “Enter”
Change directory to where your source file
located, assume your source file 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)
Java compiler (javac) translates the Java source file ”WelcomeStudents.java”
into bytecode and saves it to the file ”WelcomeStudents.class”
Rm 3553, [email protected] COMP1022P (Spring 2020) 22 / 29
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 3553, [email protected] COMP1022P (Spring 2020) 23 / 29
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 3553, [email protected] COMP1022P (Spring 2020) 24 / 29


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 3553, [email protected] COMP1022P (Spring 2020) 25 / 29


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 3553, [email protected] COMP1022P (Spring 2020) 26 / 29


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 3553, [email protected] COMP1022P (Spring 2020) 27 / 29


Further Reading
Read Section 1.2 - 1.9 of
”Introduction to Java Programming and Data Structures:
Comprehensive Version” textbook

Read the guide about how to download and install J2SE Development
Kit (JDK):
https://course.cse.ust.hk/comp1022p/jdk/

Read the guide about how to download and install IntelliJ IDEA:
https://course.cse.ust.hk/comp1022p/intellij-idea/

Rm 3553, [email protected] COMP1022P (Spring 2020) 28 / 29


That’s all!
Any questions?

Rm 3553, [email protected] COMP1022P (Spring 2020) 29 / 29

You might also like