Programs, and Java 1 Objectives To Know the basics To write a - - PowerPoint PPT Presentation

programs and java
SMART_READER_LITE
LIVE PREVIEW

Programs, and Java 1 Objectives To Know the basics To write a - - PowerPoint PPT Presentation

Chapter 1 Introduction to Computers, Programs, and Java 1 Objectives To Know the basics To write a simple Java program To know error types To Know basic syntax of a Java program 2 Programs Computer programs , known as software


slide-1
SLIDE 1

1

Chapter 1 Introduction to Computers, Programs, and Java

slide-2
SLIDE 2

2

Objectives

  • To Know the basics
  • To write a simple Java program
  • To know error types
  • To Know basic syntax of a Java program
slide-3
SLIDE 3

3

Programs

Computer programs, known as software, are instructions to the computer. They tell the computer what to do through programs. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages.

slide-4
SLIDE 4

4

Popular High-Level Languages

  • COBOL (COmmon Business Oriented Language)
  • FORTRAN (FORmula TRANslation)
  • BASIC (Beginner All-purpose Symbolic Instructional Code)
  • Pascal (named for Blaise Pascal)
  • Ada (named for Ada Lovelace)
  • C (whose developer designed B first)
  • Visual Basic (Basic-like visual language developed by Microsoft)
  • Delphi (Pascal-like visual language developed by Borland)
  • C++ (an object-oriented language, based on C)
  • C# (a Java-like language developed by Microsoft)
  • Java (We use in this course and textbook)
slide-5
SLIDE 5

5

Compiling (Java) Source Code

A program written in a high-level language is called a source

  • program. Since a computer cannot understand a source
  • program. Program called a compiler is used to translate the

source program into a machine language program called an

  • bject program (byte code). The object program is often then

linked with other supporting library code before the object code can be executed on the machine. JVM then converts byte code to machine/executable code.

Compiler Source Code File Object Code File Linker Executable File Library Code

slide-6
SLIDE 6

6

Operating Systems

The operating system (OS) is a program that manages and controls a computer’s hardware activities. For example, Windows 98, NT, 2000, XP, or ME. Application programs such as an Internet browser and a word processor cannot run without an

  • perating system.

User Application Programs Operating System Hardware

slide-7
SLIDE 7

7

Why Java?

The answer is that Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. The future of computing is being profoundly influenced by the Internet, and Java promises to remain a big part of that future. Java is the Internet programming language.

  • Java is a general purpose programming language.
  • Java is the Internet programming language.
  • Java is portable (machine-independent)
slide-8
SLIDE 8

8

Java, Web, and Beyond

  • Developed by James Gosling at Sun

Microsystems (May 20, 1995)

  • Java can be used to develop Web applications
  • Java Supports Applets
  • Java can also be used to develop applications for

hand-held devices such as Palm and cell phones

slide-9
SLIDE 9

9

JDK Versions

  • JDK 1.02 (1995) (Java Development Kit)
  • JDK 1.1 (1996)
  • JDK 1.2 (1998)
  • JDK 1.3 (2000)
  • JDK 1.4 (2002)
  • JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
  • JDK 1.6 (2006) a. k. a. JDK 6 or Java 6
  • JDK 1.7 (2010) a. k. a. JDK 7 or Java 7
  • JDK 1.8 (2014) a. k. a. JDK 8 or Java 8
  • JDK 1.9 (2017) a. k. a. JDK 9 or Java 9
slide-10
SLIDE 10

10

JDK Editions

  • Java Standard Edition (J2SE)
  • J2SE can be used to develop client-side standalone

applications or applets. (2 refers to Java 2 platform)

  • Java Enterprise Edition (J2EE)
  • J2EE can be used to develop server-side applications such as

Java servlets and Java ServerPages.

  • Java Micro Edition (J2ME).
  • J2ME can be used to develop applications for mobile devices

such as cell phones.

  • The textbook uses J2SE to introduce Java programming.
slide-11
SLIDE 11

11

Popular Java IDEs

  • NetBeans Open Source by Sun
  • Eclipse Open Source by IBM
  • JBuilder by Borland
  • MetroWerks CodeWarrior
  • BlueJ
  • JGRASP (we'll use this IDE in this course, download it

from http://www.jgrasp.org/)

slide-12
SLIDE 12

12

A Simple Java Program

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Listing 1.1: Welcome.java

slide-13
SLIDE 13

13

Syntax and Semantics

  • The syntax rules of a language define how we

can put together symbols, reserved words, and identifiers to make a valid program

  • The semantics of program statements define the

meaning/logic of the statements (program).

  • A program that is syntactically correct is not

necessarily logically (semantically) correct

  • A program will always do what we tell it to do,

not what we meant to tell it to do

slide-14
SLIDE 14

14

Errors

  • A program can have three types of errors
  • The compiler will find syntax errors and other basic

problems (compile-time errors or syntax errors)

  • If compile-time errors exist, an executable

version of the program is not created

  • A problem can occur during program execution,

such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors)

  • A program may run, but produce incorrect results,

perhaps using an incorrect formula (logical errors)

slide-15
SLIDE 15

15

Syntax Errors

  • Did you make any mistakes when you typed in the

examples?

– If you use the wrong case it won’t work > math.abs(-3)  Error: Undefined class 'math‘ – If you misspell something it won’t work > Mat.abs(-3)  Error: Undefined class 'Mat' > Math.ab(-3)  Error: No 'ab' method in 'java.lang.Math'

slide-16
SLIDE 16

16

Basic Program Development

Syntax errors Run-time errors Logical errors Ty Type pe, ed edit, an and sav ave e pr program

  • gram

Compile mpile program

  • gram

Ex Exec ecut ute e pr progra

  • gram

m an and ev eval aluat uate e res esult ults

slide-17
SLIDE 17

17

Anatomy of a Java Program

  • Class name
  • Main method
  • Statements
  • Statement terminator
  • Reserved words
  • Comments
  • Blocks
slide-18
SLIDE 18

18

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Class Name

Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome.

slide-19
SLIDE 19

19

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Main Method

Line 2 defines the main method. In order to run a class, the class must contain a method named main. The program is executed from the main method. A program/class will not run without a main method!

slide-20
SLIDE 20

20

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Statement

A statement represents an action or a sequence of

  • actions. The statement System.out.println("Welcome to

Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!".

slide-21
SLIDE 21

21

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Again, welcome to Java!"); } }

Statement Terminator

Every statement in Java ends with a semicolon (;).

slide-22
SLIDE 22

22

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Reserved Words

Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for

  • ther purposes in the program. For example, when the

compiler sees the word class, it understands that the word after class is the name for the class.

slide-23
SLIDE 23

23

Reserved Words

Java reserved words:

abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while

slide-24
SLIDE 24

24

Blocks

A pair of braces in a program forms a block that groups components of a program.

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

Class block Method block

slide-25
SLIDE 25

25

Special Symbols

Character Name Description

{} () [] // " " ; Braces Parentheses Brackets Double slashes quotation marks Semicolon Denotes a block to enclose statements. Used with methods. Denotes an array. Precedes a comment line. Enclosing a string (i.e., sequence of characters). Marks the end of a statement.

slide-26
SLIDE 26

26

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

{ … }

slide-27
SLIDE 27

27

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

( … )

slide-28
SLIDE 28

28

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

;

slide-29
SLIDE 29

29

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

// …

slide-30
SLIDE 30

30

// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

" … "

slide-31
SLIDE 31

31

Programming Style and Documentation

  • Appropriate Comments
  • Naming Conventions
  • Proper Indentation and Spacing Lines
  • Block Styles
slide-32
SLIDE 32

32

Appropriate Comments

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program.

slide-33
SLIDE 33

33

Naming Conventions

  • Choose meaningful and descriptive names.
  • Class names:

– Capitalize the first letter of each word in the name. For example, the class name ComputeExpression.

  • Method names:

– Lowercase name (one word) or capitalize the first letter of every other word in the name. For example, method main(String[] args) method computeAverageGarde(int[] grades)

slide-34
SLIDE 34

34

Proper Indentation and Spacing

  • Indentation

– Indent two spaces. Note: JGrasp has a function that does indentation automatically (called CSD style).

  • Spacing

– Use blank line to separate segments of the code.

slide-35
SLIDE 35

35

Block Styles

Use either style, just be consistent!

public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } } public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } }

End-of-line style Next-line style

slide-36
SLIDE 36

36

End of Chapter 1