0% found this document useful (0 votes)
3 views25 pages

1 Introduction

The document introduces the basics of computer programming using Java, explaining the structure of a Java program, including classes and methods. It covers the execution of programs, the role of the Java Virtual Machine, and the use of Integrated Development Environments (IDEs) for coding. Additionally, it discusses common errors in programming, including compile-time and run-time errors, and provides hands-on exercises for practice.

Uploaded by

dongshengche2013
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)
3 views25 pages

1 Introduction

The document introduces the basics of computer programming using Java, explaining the structure of a Java program, including classes and methods. It covers the execution of programs, the role of the Java Virtual Machine, and the use of Integrated Development Environments (IDEs) for coding. Additionally, it discusses common errors in programming, including compile-time and run-time errors, and provides hands-on exercises for practice.

Uploaded by

dongshengche2013
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

CPSC130: Introduction to

Computer Programming I
Chapter 1: Introduction

Dongsheng Che
Computer Science Department
East Stroudsburg University
Computer
Programs
Computer Programs

• A Computer Program is a sequence of instructions and


decisions
• Computers execute very basic instructions in rapid succession
• Programming is the act of designing and implementing
computer programs

Page 3
Analyzing your First Program

1: Declares a ‘class’ HelloPrinter


-- Every Java program has one or more classes.
3: Declares a method called ‘main’
-- Every Java application has exactly one ‘ main’ method
-- Entry point where the program starts
5: Method [Link] outputs ‘Hello, World!’
-- A statement must end with a semicolon (;)
Page 4
Syntax: The Java Program
• Every application has the same basic layout
• Add your ‘code’ inside the main method

Page 5
Syntax of the Java Program

6
Getting to know println
• The println method prints a string or a number and
then starts a new line.
[Link]("Hello"); Hello
[Link]("World!”); World!

❑ println has a ‘cousin’ method named print that


does not print a new line.

[Link]("00"); 007
00
[Link](3+4);

A method is called by specifying


the method and its agruments
Page 7
Print statements

8
Program
Execution
Source Code to Running Program

• The compiler generates the .class file which


contains instructions for the Java Virtual machine
• Class files contain ‘byte code’ that you cannot edit
• D:\temp\hello>Type [Link]
• ╩■║╛ 2 ↔ ♠ ☼ ► ↕ ‼ ¶ § ▬☺ ♠<init>☺ ♥()V☺
♦Code☺ ☼LineNumberTable☺ ♦main▬([Ljava/lang/String;)V☺
• Hello, World! [Link]♀ ↨♀ ↑ ↓☺

Page 10
Editing, compiling, and executing

11
Java Virtual Machines

• Source code

• Portable ‘byte code’


• The compiler generates
byte code in a ‘class’
file which can be run on
any Java Virtual Machine

Page 12
Programming
Environment
Programming Environment
• There are many free programming tools available for Java

• Components of an Integrated Development Environment (IDE):


• Source code editor helps programming by:
• Listing line numbers of code
• Color lines of code (comments, text…)
• Auto-indent source code
• Output window
• Debugger

Page 14
An Example IDE

Editor

Output

• Many IDEs are designed specifically for Java programming


Page 15
Text Editor Programming
• You can also use a simple text editor such as Notepad to write
your source code
• Once saved as [Link], you can use a console
window to:
1) Compile the program
2) Run the program

Compile
Output Execute

Page 16
Hands-on Exercise (1)

• Write a Java program that prints “Welcome to CPSC130 Class!”,


and then run the program.

17
Hands-on Exercises (2)

• Write a Java program that uses the print statements to draw a


square shape with *, and then run the program.

18
Hands-on Exercises (3)

• Write a Java program that uses the print statements to draw a


square shape with *, and then run the program.

19
More Print statements

• Usage of println and


print
• concatenation of
strings
• concatenation
between string and
values

20
Errors
Errors
• The Two Categories of Errors:
1) Compile-time Errors
• Syntax Errors
• Spelling, Capitalization, punctuation
• Ordering of statements, matching of
braces/parenthesis…
• No .class file is generated by the compiler
• Correct first error listed, then compile again
2) Run-time Errors
• Logic Errors
• Program runs, but produces unintended results
• Program may ‘crash’

Page 22
Syntax Errors

• What happens if you


• Misspell a word: [Link]
• Don’t Capitalize a [Link]
• Leave out a word void
• Forget a Semicolon after ("Hello, World!")
• Don’t match a curly brace? Remove line 6
• Try it to see what error messages are generated
Page 23
Logic Errors

• What happens if you


• Divide by Zero
[Link](1/0);
• Mis-spell output ("Hello, Word!")
• Forget to output Remove line 5
• Programs will compile and run
• The output may not be as expected
Page 24
Hands-on Exercise

• Find out the errors in the following program.

25

You might also like