Designing a Program & the Java Programming Language Mrs. Butera.

32
Designing a Program & the Java Programming Language Mrs. Butera

Transcript of Designing a Program & the Java Programming Language Mrs. Butera.

Page 1: Designing a Program & the Java Programming Language Mrs. Butera.

Designing a Program &

the Java Programming Language

Mrs. Butera

Page 2: Designing a Program & the Java Programming Language Mrs. Butera.

What is a Program?

A program is a set of instructions a computer follows in order to perform a task. A programming language is a special

language used to write computer programs.

Page 3: Designing a Program & the Java Programming Language Mrs. Butera.

Machine Language

Page 4: Designing a Program & the Java Programming Language Mrs. Butera.

Modern Programming Languages

Page 5: Designing a Program & the Java Programming Language Mrs. Butera.

Examples of Programming Languages

BASIC

FORTRAN

COBOL

Pascal

C

C++

Java

JavaScript

Perl

Python

Ruby

Visual Basic

Page 6: Designing a Program & the Java Programming Language Mrs. Butera.

History of Java

Developed by Sun Microsystems in 1991

Goal: Develop a language that could be processed by all the devices it controlled – not dependent upon the processor each with its own machine language

Used in:

Stand-alone applications

Applets for the Internet and devices

Page 7: Designing a Program & the Java Programming Language Mrs. Butera.

Security

Can Java applets corrupt your computer? NO!

Web browsers run Java applets in a secure environment within your computer's memory and do not allow them to access any of your computer's resources

Page 8: Designing a Program & the Java Programming Language Mrs. Butera.

BEFORE you write the Program, you conceive:

The Algorithm

& sometimes a

Flowchart

Page 9: Designing a Program & the Java Programming Language Mrs. Butera.

What is an Algorithm?

The set of well-defined steps that are given to the computer to

perform a task or solve a problem.

The algorithm is conceived before The algorithm is conceived before the program is written.the program is written.

Page 10: Designing a Program & the Java Programming Language Mrs. Butera.

Example: Gross Pay Algorithm Display: “How many hours did you work?”

Allow user to input hours worked – store the number in memory

Display: “How much do you get paid per hour?”

Allow user to input pay per hour and store the number in memory

Calculate Gross pay = Hours_Worked x Pay_Per_Hour

Display the Gross Pay

Page 11: Designing a Program & the Java Programming Language Mrs. Butera.

Flowchart Symbols

Page 12: Designing a Program & the Java Programming Language Mrs. Butera.

Gross Pay FlowchartStart

Input HoursInput Pay Per Hour

Calculate Gross Pay

OutputGross Pay

Start

Page 13: Designing a Program & the Java Programming Language Mrs. Butera.

What is a Program Made of?

Vocabulary – the set of all words and symbols in the language.

Syntax – rules for combine words into sentences or statements.

Semantics – define the rules for interpreting the meaning of statements

Page 14: Designing a Program & the Java Programming Language Mrs. Butera.

Program Enhancements

White Space and Indentation – for readability

Comments – for maintenance purposes• /*comment area */

• // entire line

Page 15: Designing a Program & the Java Programming Language Mrs. Butera.

Sample First Java Program “Hello World”

// This is the classic first Java program

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

}

}

Save As: HelloWorldHelloWorld.java .java

Page 16: Designing a Program & the Java Programming Language Mrs. Butera.

Key Words/Reserved Words

Every programming language has reserved words that cannot be used as variable names

When you compile your program, an error will be returned if you use a reserved word incorrectly

Page 17: Designing a Program & the Java Programming Language Mrs. Butera.

Key Words/Reserved Words

public class Payroll{

public static void main(String[] args){

Int hours = 40;double grossPay, payRate = 25.0;

grossPay = hours * payRate;System.out.println(“Your gross pay is $” + grossPay);}

}

Page 18: Designing a Program & the Java Programming Language Mrs. Butera.

Some Java Vocabulary

Type of Element Examples

Arithmetic operators + - * /

Assignment operator =

Numeric literals 5.73 9

Programmer defined variable name

name, pay_per_hour, gross_pay

Page 19: Designing a Program & the Java Programming Language Mrs. Butera.

Invalid Program Syntax

• Answer = (F - 32) * / 9;

• Answer = )F - 32 ( * 5 / 9;

• Answer = F – 32) * 5 / 9;

• Answer = (F - 32) * 5.0 / 9.0

Page 20: Designing a Program & the Java Programming Language Mrs. Butera.

SemanticsDefines the rules for interpreting the

meaning of statements

Answer = (F - 32) * 5.0 / 9.0;

Means “go into the parentheses first, subtract 32.0 from the variable quantity indicated by F, then multiply the result by 5.0, and finally divide the whole thing by 9.0” & store result in the variable (storage area) Answer

Page 21: Designing a Program & the Java Programming Language Mrs. Butera.

Programmer-Defined NamesVariable are the names of memory locations that may hold data.

public class Payroll{

public static void main(String[] args){

Int hours = 40;double grossPay, payRate = 25.0;

grossPay = hours * payRate;System.out.println(“Your gross pay is $” + grossPay);}

}

Page 22: Designing a Program & the Java Programming Language Mrs. Butera.

Operators

public class Payroll{

public static void main(String[] args){

Int hours = 40;double grossPay, payRate = 25.0;

grossPay = hours * payRate;System.out.println(“Your gross pay is $” + grossPay);}

}

Page 23: Designing a Program & the Java Programming Language Mrs. Butera.

Syntax/PunctuationMarks the end of a complete sentence/statement.

public class Payroll{

public static void main(String[] args){

Int hours = 40;double grossPay, payRate = 25.0;

grossPay = hours * payRate;System.out.println(“Your gross pay is $” + grossPay);}

}

Page 24: Designing a Program & the Java Programming Language Mrs. Butera.

Lines and Statements

Programs are made up of lines and statements.

Page 25: Designing a Program & the Java Programming Language Mrs. Butera.

LinesA line is just a single line as it appears in the body of a program.

public class Payroll{

public static void main(String[] args){

Int hours = 40;double grossPay, payRate = 25.0;

grossPay = hours * payRate;System.out.println(“Your gross pay is $” + grossPay);}

}

Page 26: Designing a Program & the Java Programming Language Mrs. Butera.

StatementsA statement is a complete instruction that causes the computer to

perform some action.

public class Payroll{

public static void main(String[] args){

Int hours = 40;double grossPay, payRate = 25.0;

grossPay = hours * payRate;System.out.println(“Your gross pay is $” + grossPay);}

}

Page 27: Designing a Program & the Java Programming Language Mrs. Butera.

VariablesA variable is a named storage location in the computer's memory.

public class Payroll{

public static void main(String[] args){

Int hours = 40;double grossPay, payRate = 25.0;

grossPay = hours * payRate;System.out.println(“Your gross pay is $” + grossPay);}

}

Page 28: Designing a Program & the Java Programming Language Mrs. Butera.

The Compiler and the Java Virtual Machine (JVM)

Source Code = the program is typed in a text editor. Source file example: Payroll.java

javac Payroll.java – compiles the program -(converts source code to byte code – 3b 00 8h 01 84 ff ......)

syntax errors may be returned

java Payroll - executes the program

Page 29: Designing a Program & the Java Programming Language Mrs. Butera.

The Compiler and the Java Virtual Machine (JVM)

Page 30: Designing a Program & the Java Programming Language Mrs. Butera.

Integrated Development Environments (IDE)

Software package including:

Text Editor

Compiler

Debugger

Other utilities

Examples:

NetBeans

Eclipse

Komodo

WinDev

Page 31: Designing a Program & the Java Programming Language Mrs. Butera.

The Programming Process

1.Clearly define what the program is to do (requirements).

2.Visualize the program running on the computer.

3.Use design tools to create a model of the program (algorithm, flowchart).

4.Check the model for logical errors.

Page 32: Designing a Program & the Java Programming Language Mrs. Butera.

The Programming Processcontinued:

5. Enter the code and compile it.

6. Correct any errors found during compilation. Repeat steps 5 & 6 as many times as necessary.

7. Run the program with test data for input (valid & invalid data). Have someone else test it.

8. Correct any runtime errors found while running the program. Repeat steps 5 & 6 as many times as necessary.

9. Validate the results of the program.