Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You...

29
Chapter 1 Programming Languages

Transcript of Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You...

Page 1: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Chapter 1 Programming Languages

Page 2: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Application Development: Top 10 Programming Languages to Keep You Employed

1. Java 2. C#3. C++4. JavaScript5. Visual Basic 6. Php7. Objective C8. Peral9. Python10. Ruby

If you want to make money in programming what are the best languages to learn?

What do you think are the top programming languages?

http://www.eweek.com/c/a/Application-Development/Top-10-Programming-Languages-to-Keep-You-Employed-719257/

Page 3: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Where is Java?

Page 4: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Where is Java?

Page 5: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Java Life Video

Page 6: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

What is Java technology and why do I need it?

• Java is a programming language and computing platform first released by Sun Microsystems in 1995. Sun Microsystems was acquired by Oracle.

• It is the underlying technology that powers state-of-the-art programs including utilities, games, and business applications.

• Java runs on more than 850 million personal computers worldwide, and on billions of devices worldwide, including mobile and TV devices.

Page 7: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Where is Java?

• 1.1 billion desktops run Java• 930 million Java Runtime Environment downloads

each year• 3 billion mobile phones run Java• 31 times more Java phones ship every year than

Apple and Android combined• 100% of all Blu-ray players run Java• 1.4 billion Java Cards are manufactured each year• Java powers set-top boxes, printers, Web cams,

games, car navigation systems, lottery terminals, medical devices, parking payment stations, and more.

Page 8: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

8

Programming Language Levels

• Different Programming language levels:

1. machine language 1s and 0s (What the computer understands – binary code)

2. assembly language - low level step above machine. Still hard to code and understand.

3. high-level language – use English words for code. Easy to use.

• Each type of CPU has its own specific machine language that interprets the 1s and 0s.

• The other levels were created to make it easier for a human being to read and write programs so we can use English words to code. 1s and 0s were too hard!

Page 9: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

9

Programming Languages

• A program must be translated into machine language before it can be executed on a particular type of CPU

• This can be accomplished in several ways

• A compiler is a software tool which translates source code into a specific target language

• Often, that target language is the machine language for a particular CPU type

Page 10: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

10

Java is Unique

• The Java compiler translates Java source code into a special

representation called bytecode

• Java bytecode is not the machine language for a particular CPU.

• Therefore the Java compiler is not tied to any particular machine or platform.

• An interpreter then coverts the bytecode to the language the machine understands.

• Therefore, Java is considered to be architecture-neutral and can run on PCs, Macs, Linux, anywhere.

Page 11: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

IDE• Programming languages must have a text editor called an

Integrated Development Environment used to type the source code for the program.

• There are many integrated development environments for writing code.

– Sun NetBeans– Borland JBuilder– MetroWerks CodeWarrior– Microsoft Visual J++– IBM Eclipse– Monash BlueJ– Dr. Java

Page 12: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

How does it work

When we download Java, we don’t see it. It is a considered a virtual machine. To communicate we use an integrated development environment (in our case Dr. Java) which is a text editor where we type our code.

Our code has to converted to 1s and 0s. Java has a compiler that converts code to bytecode. This is why it can be used on any platform (windows, linux, mac). Then the interpreter will convert it to the 1s and 0s for that particular platform.

Steps:

1. Write a program using an IDE in Java source code.

2. Compile it (Java has a compiler which will convert it to bytecode which is platform independent.)

3. An interpreter then converts it to 1s and 0s for the platform it is on. (when we hit run)

When you write, compile and run you have 3 files. Source file, java file, class file. Only the source file will open. The others are for execution by the compiler and interpreter.

Page 13: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Summary for IDE

• Integrated Development Environment is used to type java source code. It contains an editor, compiler and an interpreter and debugger in one package.

• The source code is compiled (by a compiler) into code called bytecode.

• A java interpreter converts the bytecode into instructions for that particular CPU.

Page 14: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Dr. Java as our IDE

• Dr. Java is a simple editor tool for entering program text and interaction space so that you can try things out in Dr. Java and crate new programs.

• Free under the DrJava Open Source License.

Page 15: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

DrJava Features

• Works with multiple files– Files pane

• Color coded editor– Definitions pane

• Interpretation of Java code– Interactions pane

• Integrated debugger

Filespane

Interactions pane

Definitions pane

Page 16: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

How to use the console pane

• If you click on the console tab– You will see the

console pane

• It shows just the items that you have printed to the console– Using

System.out.println or– System.out.print

Page 17: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Open Dr. Java

Files Pane – List the open files in Dr. Java

Definitions pane – This is where you write your classes. Simply a text editor.

You compile all current files open in the files pane b y clicking on the compile all

Interactions Pane – You can type commands and it will execute but not save.

Page 18: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Java

• A programming language specifies the words and symbols that we can use to write a program called

Syntax

Page 19: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Java Program Structure

• In the Java programming language:– A program is made up of one or more classes– A class contains one or more methods– A method contains program statements

• These terms will be explored in detail throughout the course

• A Java application always contains a method called main

• See Lincoln.java (page 27)

Page 20: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

20

Java Program Structure

public class MyProgram

{

}

// comments about the class

class header

class body

Comments can be placed almost anywhere

Page 21: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

21

Java Program Structure

public class MyProgram

{

}

public static void main (String[] args)

{

}

// comments about the class

// comments about the method

method headermethod body

Page 22: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

22

Comments• Comments in a program are called inline documentation

• They should be included to explain the purpose of the program and describe processing steps

• They do not affect how a program works

• Java comments can take three forms:

// this comment runs to the end of the line

/* this comment runs to the terminating symbol, even across line breaks */

/** this is a javadoc comment */

They do not want you to use comments on your AP Exam

Page 23: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

23

Identifiers – Identify yourself!• Identifiers are the words a programmer uses in a program. Java looks for

those words.

• An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign

• Identifiers cannot begin with a digit. They can only begin with an _ or a letter.

• Java is case sensitive - Total, total, and TOTAL are different identifiers

• By convention, Java programmers use different case styles for different types of identifiers, such as

– title case for class names - Lincoln

– upper case for constants (things that won’t change)- MAXIMUM

Page 24: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Reserved words as Identifiers

• Sometimes we choose identifiers ourselves when writing a program (such as Lincoln)

• Sometimes we are using identifiers Java has created for special purposes. Those have special meaning so we can’t use those for our identifiers that we create.

• The identifiers reserved by Java are associated with methods and data created by Java that we don’t have to know about. (encapsulation). We just use them and they work.

Page 25: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

25

Reserved Words

• The Java reserved words:

abstractassertbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodouble

elseenumextendsfalsefinalfinallyfloatforgotoifimplementsimportinstanceofint

interfacelongnativenewnullpackageprivateprotectedpublicreturnshortstaticstrictfp

superswitchsynchronizedthisthrowthrowstransienttruetryvoidvolatilewhile

Page 26: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

26

White Space

• Spaces, blank lines, and tabs are called white space

• White space is used to separate words and symbols in a program

• Extra white space is ignored

• A valid Java program can be formatted in many ways

• Programs should be formatted to enhance readability, using consistent indentation

• See Lincoln2.java (page 33)

• See Lincoln3.java (page 34)

Page 27: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

27

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 a program statement define what that statement means (its purpose or role in a 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

Page 28: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Errors could be syntax or semantic

• Syntax – rules of a language– Java will not compile programs with syntax errors

• Semantics – How the program is put together. What will happen when it is executed. – Java will compile and run but results will not be

correct.

Page 29: Chapter 1 Programming Languages. Application Development: Top 10 Programming Languages to Keep You Employed 1. Java 2. C# 3. C++ 4. JavaScript 5. Visual.

Three kind of errors

• Compile time error – syntax error. Will not compile the program. Wrong syntax

• Runtime error – Will compile but will not run. It crashes during execution. Try to divide by 0 or index outbound in arrays

• Logical error – Will compile, Will run and print but produces the wrong output. (program is not put together correctly). Used wrong equation or steps in solving the solution.