Download Designing a Program & the Java Programming Language

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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.
Machine Language
Modern Programming
Languages
Examples of Programming
Languages

BASIC

JavaScript

FORTRAN

Perl

COBOL

Python

Pascal

Ruby

C

Visual Basic

C++

Java
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
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
BEFORE you write the Program,
you conceive:
The Algorithm
& sometimes a
Flowchart
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 program is written.
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
Flowchart Symbols
Gross Pay Flowchart
Start
Input Hours
Input Pay Per Hour
Calculate
Gross Pay
Output
Gross Pay
Start
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
Program Enhancements


White Space and Indentation – for
readability
Comments – for maintenance purposes
• /*comment area */
• // entire line
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: HelloWorld.java
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
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);
}
}
Some Java Vocabulary
Type of Element
Examples
Arithmetic operators
+ - * /
Assignment operator
=
Numeric literals
5.73
Programmer defined
variable name
name, pay_per_hour,
gross_pay
9
Invalid Program Syntax
• Answer = (F - 32) * / 9;
• Answer = )F - 32 ( * 5 / 9;
• Answer = F – 32) * 5 / 9;
• Answer = (F - 32) * 5.0 / 9.0
Semantics
Defines 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
Programmer-Defined Names
Variable 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);
}
}
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);
}
}
Syntax/Punctuation
Marks 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);
}
}
Lines and Statements
Programs are made up of lines and statements.
Lines
A 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);
}
}
Statements
A 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);
}
}
Variables
A 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);
}
}
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
The Compiler and the Java
Virtual Machine (JVM)
Integrated Development
Environments (IDE)
Software package
including:

Text Editor

Compiler

Debugger

Other utilities
Examples:

NetBeans

Eclipse

Komodo

WinDev
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.
The Programming Process
continued:
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