Program Development The mechanics of developing a program






















![Another Simple Application Program public class Test 2 { public static void main(String args[]){ Another Simple Application Program public class Test 2 { public static void main(String args[]){](https://slidetodoc.com/presentation_image_h2/da3cf735132ecdc5c3fa157ae531776d/image-23.jpg)






- Slides: 29
Program Development • The mechanics of developing a program include several activities – writing the program in a specific programming language (such as Java) – translating the program into a form that the computer can execute – investigating and fixing various types of errors that can occur • Software tools can be used to help with all parts of this process 10/21/2021 CS 101 - Algorithms & Programming I 1
Language Levels • There are four programming language levels: – machine language – assembly language – high-level language • Each type of CPU has its own specific machine language • The other levels were created to make it easier for a human being to read and write programs 10/21/2021 CS 101 - Algorithms & Programming I 2
Programming Languages • Each type of CPU executes only a particular machine language • A program must be translated into machine language before it can be executed • 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 • The Java approach is somewhat different 10/21/2021 CS 101 - Algorithms & Programming I 3
Java Programming Language • A programming language specifies the words and symbols that we can use to write a program • A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program statements • The Java programming language was created by Sun Microsystems, Inc. • It was introduced in 1995 and it's popularity has grown quickly since 10/21/2021 CS 101 - Algorithms & Programming I 4
Java Translation • The Java compiler translates Java source code into a special representation called bytecode • Java bytecode is not the machine language for any traditional CPU • Another software tool, called an interpreter, translates bytecode into machine language and executes it • Therefore the Java compiler is not tied to any particular machine • Java is considered to be architecture-neutral 10/21/2021 CS 101 - Algorithms & Programming I 5
Java Translation (cont. ) Java source code Java compiler Java bytecode Bytecode interpreter Bytecode compiler Machine code 10/21/2021 CS 101 - Algorithms & Programming I 6
Development Environments • There are many programs that support the development of Java software, including: – Sun Java Development Kit (JDK) – JCreator we are going to use this one – – – Sun Net. Beans IBM Eclipse Borland JBuilder Blue. J j. GRASP • Though the details of these environments differ, the basic compilation and execution process is essentially the same 10/21/2021 CS 101 - Algorithms & Programming I 7
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 10/21/2021 CS 101 - Algorithms & Programming I 8
Errors • A program can have three types of errors • The compiler will find syntax errors and other basic problems (compile -time 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) 10/21/2021 CS 101 - Algorithms & Programming I 9
Program Development Edit and save program syntax errors Compile program run-time errors logical errors Execute program and evaluate results 10/21/2021 CS 101 - Algorithms & Programming I 10
Preparing a Java Program • We are going to use JCreator environment • JCreator environment is simple to use and free. • You can download JCreator environment for your own computer from the website http: //www. jcreator. com • Editing: – Create a file containing a Java program. – This file will be. java extension (Ex: Simple. Appl. java ) • Compiling: – Use Java compiler to compile the Java program. – Java compiler will create a file with. class extension. This file will contain the Java byte-codes of your Java program ( Simple. Appl. class ). You can run this file in different platforms. – The other compilers produce executable files. • Executing: – Execute the byte-codes of your Java program by a Java interpreter. 10/21/2021 CS 101 - Algorithms & Programming I 11
A Simple Console Application Program /** * Simple application which prints "Hello, World" * * Ilyas Cicekli * Version 1. 00 06/09/29 */ public class Simple. Appl { /* * Main method of the console application */ public static void main(String[] args) { // print “Hello, World” System. out. println("Hello, World"); } } // end of main // end of class 10/21/2021 CS 101 - Algorithms & Programming I 12
A Simple Console Application Program (cont. ) • We put this application program into the file Simple. Appl. java • To compile: – creates Simple. Appl. class file (if there is no syntax errors) • To run: – java interpreter will interpret the byte-codes in Simple. Appl. class file. – As a result, we will see Hello, World on the screen 10/21/2021 CS 101 - Algorithms & Programming I 13
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 10/21/2021 CS 101 - Algorithms & Programming I 14
Java Program Structure /** * Simple application which prints "Hello, World" * * Ilyas Cicekli * Version 1. 00 06/09/29 */ public class Simple. Appl { /* class header * Main method of the console application */ public static void main(String[] args) { // print “Hello, World” System. out. println("Hello, World"); } } // end of main method header method body // end of class 10/21/2021 class body CS 101 - Algorithms & Programming I 15
Java Programs • Our Java program defines a class. – We use class keyword to declare a class. – The name of a class is an identifier. Uppercase and lowercase letters are different. – The name of the class must be same as the file holding that class Simple. Appl. java • Our class contains only one method. – – Every console application program should contain method. It may contain other methods too. The method main should be declared using public and static keywords. Our main method contains only one executable statement • This is a method invocation statement (println statement) 10/21/2021 CS 101 - Algorithms & Programming I 16
Simple Java Program • What do we have in our simple Java program? – Identifiers – Simple. Appl, args, System, out, . . . – Reserved Words – public, class, static, . . . – Literals – “Hello, World” – Operators --. – Delimeters -- { } ( ) [ ] ; – Comments -- // end of class • When these parts are combined according to the certain rules (the syntax of Java), a syntactically correct Java program is created. 10/21/2021 CS 101 - Algorithms & Programming I 17
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: // /*. . . */ single line comments this comment runs to the end of the line multi line comments this comment runs to the terminating symbol, even across line breaks /**. . . */ javadoc comments this comment is similiar to multi-line comments • We can use as much as white spaces between words to make easier to read programs. 10/21/2021 CS 101 - Algorithms & Programming I 18
Identifiers • We make up words for class names, method names, and variables. • These words are called identifiers. • For example, – Simple. Appl, main, System, out, are identifiers in our simple program. – We made up Simple. Appl and main(!); and others are defined in Java API (Application Programming Interface). • An identifier can be composed of any combination of letters, digits, the under score character, and the dollar sign; but it cannot start with a digit. • We can use both upper case letters and lower case letters in identifiers. But Java is case sensitive. Identifiers Val, val and VAL are all different variables. • Some Legal Identifiers: x val count_flag Test 1 $amount val 1 stock. Item • Some Illegal Identifiers: 1 val x# x-1 x+ 10/21/2021 CS 101 - Algorithms & Programming I 19
Identifiers (cont. ) • Although we can choose any legal identifier to be used, but it is nice to follow certain style guidelines when make up an identifier. – Choose meaningful names (not too long, not too short, descriptive words) – First character of a class name should be an uppercase letter. – First character of a method name and a variable should be a lower case letter. – All characters of constants should be uppercase • Sometimes we choose identifiers ourselves when writing a program (such as Simple. Appl) • Sometimes we are using another programmer's code, so we use the identifiers that he or she chose (such as println) • We cannot use reserved words that already have a predefined meaning in the language as identifiers. A reserved word cannot be used in any other way 10/21/2021 CS 101 - Algorithms & Programming I 20
Reserved Words • Reserved words are identifiers that have a special meaning in a programming language. • For example, – public, void, class, static are reserved words in our simple program. • In Java, all reserved words are lower case identifiers (Of course we can use just lower case letters for our own identifiers too) • We cannot use the reserved words as our own identifiers (i. e. we cannot use them as variables, class names, and method names). 10/21/2021 CS 101 - Algorithms & Programming I 21
Literals • Literals are explicit values used in a program. • Certain data types can have literals. – String literal – “Hello, World” – integer literals -12 3 77 – double literals – 12. 1 3. 45 – character literals – ‘a’ ‘ 1’ – boolean literals -true false 10/21/2021 CS 101 - Algorithms & Programming I 22
Another Simple Application Program public class Test 2 { public static void main(String args[]){ // print the city and its population. System. out. println("The name of the city is " + "Ankara"); System. out. println("Its population is " + 2500000); // Different usage of + operator System. out. println("Sum of 5+4: " + (5+4)); // Different output method – print System. out. print("one. . "); System. out. print("two. . "); System. out. println("three. . "); System. out. print("four. . "); } // end of main } // end of class 10/21/2021 CS 101 - Algorithms & Programming I 23
Another Simple Application Program (cont. ) • The operator + is a string concatenation operator. – “abc”+”de” “abcde” – 2500000 is converted to a String (“ 2500000”) , and this string is concatenated with the string literal “Its population is”. • The operator + is also a regular add operator for numeric values. + in (5+4) is a regular add operator for numeric values. • In other words, the + operator is overloaded. • println prints its argument and moves to the next line. • prints its argument and it does not move to the next line. • The output of our program will be: The name of the city is Ankara Its population is 2500000 Sum of 5+4: 9 one. . two. . three. . four. . 10/21/2021 CS 101 - Algorithms & Programming I 24
Character Strings • A string of characters can be represented as a string literal by putting double quotes around the text: • Examples: "This is a string literal. " "123 Main Street" "X" • Every character string is an object in Java, defined by the String class • Every string literal represents a String object 10/21/2021 CS 101 - Algorithms & Programming I 25
The println Method • The System. out object represents a destination (the monitor screen) to which we can send output System. out. println ("Whatever you are, be a good one. "); object 10/21/2021 method name information provided to the method (parameters) CS 101 - Algorithms & Programming I 26
The print Method • The System. out object provides another service as well • The print method is similar to the println method, except that it does not advance to the next line • Therefore anything printed after a print statement will appear on the same line System. out. print(“one-”); System. out. print(“two-”); 10/21/2021 CS 101 - Algorithms & Programming I 27
String Concatenation • The string concatenation operator (+) is used to append one string to the end of another "Peanut butter " + "and jelly" • It can also be used to append a number to a string • A string literal cannot be broken across two lines in a program • The + operator is also used for arithmetic addition • The function that it performs depends on the type of the information on which it operates • If both operands are strings, or if one is a string and one is a number, it performs string concatenation • If both operands are numeric, it adds them • The + operator is evaluated left to right, but parentheses can be used to force the order 10/21/2021 CS 101 - Algorithms & Programming I 28
Escape Sequences • What if we wanted to print a the quote character? • The following line would confuse the compiler because it would interpret the second quote as the end of the string System. out. println ("I said "Hello" to you. "); • An escape sequence is a series of characters that represents a special character • An escape sequence begins with a backslash character () System. out. println ("I said "Hello" to you. "); • Some Java escape sequences t tab n newline r carriage return ” double quote ’ single quote \ backslash 10/21/2021 CS 101 - Algorithms & Programming I 29