0% found this document useful (0 votes)
2 views32 pages

Lecture3 2024

The document outlines the programming principles covered in a computer science course, focusing on the programming process, environment, and the first Java program. It details learning outcomes, programming errors, and the use of tools like Visual Studio Code and CMDER for coding and executing Java programs. Additionally, it includes practical exercises for creating directory structures and compiling Java code.

Uploaded by

HAMO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views32 pages

Lecture3 2024

The document outlines the programming principles covered in a computer science course, focusing on the programming process, environment, and the first Java program. It details learning outcomes, programming errors, and the use of tools like Visual Studio Code and CMDER for coding and executing Java programs. Additionally, it includes practical exercises for creating directory structures and compiling Java code.

Uploaded by

HAMO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

COMPUTER

SCIENCE

CSI141
PROGRAMMING
h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l PRINCIPLES
Programming Principles T ALLMAN NKGAU

L ECTURE 3
CHAPTER 1.2 – 1.6
‣ Programming process
‣ Programming environment
‣ First Java program

http://horstmann.com/bjlo/index.html
L EARNING OUTCOMES
At the end of the lecture, you should be able

to:

‣ Describe the programming process

‣ Describe the various programming errors (syntax,

logic, run-time)

‣ Use CLI commands to create a folder structure


h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l
‣ Use VSCODE to type in a Java program

‣ Use CLI to compile and execute a given Java

program
‣ Programming process
‣ Programming environment
‣ First Java program

h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l
REVIEW - POP QUIZ
❑ The algorithm

MYSTERY(n)
Input: An integer n > 0
Output: You find out!
b ← 0
while n > 0 do //read as “while n is greater than 0 do”
d ← n % 10 // remainder when dividing by 10
b ← b + d
n ← n / 10 // integer division

return b

Called block of
statements
Page 5
Programming process
❑ Systematic steps involved in solving a computational programming
problem. Start

Define
Problem

Program Bugs
Analysis/
Maintenance
Algorithm Design
Bugs

Coding
Page 6
Programming process
❑ Defining the problem
・ State the problem concisely
・ A lot of software systems fail due to vague problem definition.
・ Usually specified by Management and/or Systems Analyst.
・ In this course, the lecturers will specify the problem.

❑ Analysis / Algorithm Design


・ Understand the problem completely.
– The requirements (input, output, processing)
– Data representation for data to be processed
・ Design an algorithm to solve the problem (specify it using pseudocode)
・ Check the algorithm for correctness (test data and/or mathematical
analysis)

Page 7
…Programming process

❑ Coding
・ Implement your algorithm using an appropriate language
・ Fix all compile-time errors (like syntax errors, class not found, etc.)
・ Properly document your source code (so others have no difficulty
understanding your code)
❑ Usage & Maintenance
・ Use the program / Execute the program
・ Fix all run-time errors (like logical errors, etc.)
・ If requirements change, adapt the program

Page 8
Java programming process

Page 9
Java Compilation
HelloWorld.class

javac java

HelloWorld.java
Java Compilation
javac HelloWorld.java

Compiler 1
(IBM)
Editor
Compiler 2
Java Source Code File (Sun) Java Bytecode
(HelloWorld.java) (HelloWorld.class)
Compiler 3
(Apple)

Input Data Bytecode Interpreter


java HelloWrold

Machine Instructions
Output Data
(Execution)
Programming errors

❑ Syntax Error
・ It is a grammatical mistake in a program – breaking grammar rules.
・ The compiler is used to check for syntax errors.

❑ Logic Error
・ It is an error introduced by using faulty logic in a program.
・ Program produces incorrect results.

❑ Run-time Error
・ An error issued by the computer when a program is executed.
・ Program usually crashes with some sort of an error message.

Page 12
‣ Anatomy of a computer
‣ Data representation
‣ Programming process
‣ Programming environment
‣ First Java program
h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l
Programming Environment - JDK

❑ Java Development Kit (JDK)


・ This is software that allows us to code in Java (compile and execute)
・ It contains the JVM (Java Virtual Machine) and JRE (Java Runtime
Environment – for executing Java programs).
・ Download JDK LTS version 21 here.
– https://adoptium.net/
– Ideally you should install the JDK first.

Page 14
Programming Environment - vscode

❑ Code Editor
・ This is software that allows us to type in our source code (our Java
programs)
・ Source code is plain text (so really any text editor will do)
・ For this course we will be using Visual Studio Code
– https://code.visualstudio.com/
– Its totally free!
– Can be used both on Windows and Linux operating systems

Page 15
Visual Studio Code (vscode) - Code
Editor

Page 16
Programming Environment

❑ Compiling and Execution


・ We use terminal emulation software aka CLI
・ This is software that allows us to compile and execute our Java
programs from a command line interface - CLI
・ For this course we will be using CMDER
– https://cmder.app/
– Its Windows OS only and totally free!
– Superior to the CMD software that comes with Windows
– Download the “Full” version

Page 17
CMDER – Terminal (CLI)

Page 18
Programming Environment

❑ Folder/directory structure for the course

・We make use of the following commands (in CMDER)


– md or mkdir to create folders/directories
– dir or ls to list/show the files in a directory/folder
– pwd to show the current folder we are working in
– cd to change the directory you are working in
– touch to create an empty text file

To come as the course progresses


– rm or rmdir to remove/delete files or folders
– more or cat to view contents of a file, screen-full at a time
– copy to copy contents of one file to another

Page 19
CLI crash course
❑ We will create the following directory/folder structure
• Z: is the drive letter
• CSI141-2024 is a folder

files

Directory = Folder

directories

Page 20
1. Launch CMDER

– Double-click the icon

– Type Z: and press enter key to change to your home directory


– Drive letters can be lowercase
Page 21
2. List the files and folders in your Z drive

– Type dir and press enter key

Represents
Current directory

Represents the
parent directory

This
means
directory

Page 22
3. Create CSI141-2024 directory

– Type mkdir CSI141-2024 and press enter key

Creating directory.
Notice no visual
feedback!

Listing the
files/directory
shows that our
directory was
created

Page 23
4. Change into the directory CSI141-2024

– Type cd CSI141-2024 and press enter key

Notice the change in the path

– Type pwd and press enter


The p resent
w orking d irectory is
displayed – pwd

Page 24
5. List the files/directories inside CSI141-2024

– Type dir and press enter key

• We only have
the . and ..
directories.
• In fact, every
directory has
them!

Page 25
6. Create lab1 directory and its files

Create directory lab1

Change into directory lab1

Create file data.txt

Create file
MyFirstProgram.java
List the files in current
directory – lab1

Page 26
‣ Anatomy of a computer
‣ Data representation
‣ Programming process
‣ Programming environment
‣ First Java program
h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l
Our first Java program
Java class name

public class HelloWorld


{
public static void main(String[] args)
{
System.out.printf(“Hello, World!\n”);
}
}

body of main() main method

Source code file named


HelloWorld.java Page 28
Compile and Execute – A taste of what's to
come
E:\Downloads\cmder\CSI141-2024\examples
λ javac -version
javac 21.0.2 Compiler version
E:\Downloads\cmder\CSI141-2024\examples
λ java -version JVM version
openjdk version “21.0.2" 2024-01-16
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS,
mixed mode, sharing)
λ dir
Volume in drive E is hd
Volume Serial Number is D081-884A
Directory of E:\Downloads\cmder\CSI141-2024\examples
09/18/2020 08:22 PM <DIR> .
09/18/2020 08:22 PM <DIR> ..
09/18/2020 08:21 PM 132 HelloWorld.java
1 File(s) 132 bytes
2 Dir(s) 456,416,174,080 bytes free Compile Java
λ javac HelloWorld.java program
E:\Downloads\cmder\CSI141-2024\examples Run/Execute Java
λ java HelloWorld program
Hello World!
E:\Downloads\cmder\CSI141-2024\examples Page 29

λ
Review exercise - CLI
Create the following folder structure.

Typing in the command


tree /f physical-features
Should give you the same structure.
Review exercise - TheDuke
Type in the following Java program. Compile and execute it. You
should name the source code file TheDuke.java
Compile with: javac TheDuke.java
Execute with: java TheDuke

import java.net.URI;

Import java.net.URL;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

public class TheDuke

public static void main(String[] args) throws Exception {

URI imageLocation = new URI("https://horstmann.com/java4everyone/duke.gif");

JOptionPane.showMessageDialog(null, "Hello", "Title",

JOptionPane.PLAIN_MESSAGE, new ImageIcon(imageLocation.toURL()));

}
Summary

❑ Familiarize yourselves with our code editor – VSCODE


❑ Familiarize yourselves with our terminal emulator – CMDER
❑ Throughout the course, we will code, compile, execute our Java
programs - practice makes perfect

Page 32

You might also like