800 likes | 824 Views
Explore the fundamentals of programming languages, syntax, and semantics in the context of system development with Java. Gain a comprehensive understanding of syntax rules, valid words and punctuation, and the importance of semantics in writing correct programs. Discover the various families of programming languages, including procedural and object-oriented languages, and the advantages and disadvantages of using Java.
E N D
System development with Java Instructors: Rina Zviel-Girshin Interdiciplinary Center Herzlia School of the Computer Science Fall: 2001-2002 Rina Zviel-Girshin @ASC
Information Source • Course site : www.idc.ac.il/ • Course book: Java Software Solutions /Lewis & Loftus Thinking in Java / Bruce Eckle Rina Zviel-Girshin @ASC
Course Methodology • Non-Linearity Spiral model Hypermedia • CAI – Computer Assisted Instruction • Interactive learning • Labs and programming • Multi-approach: inductive-deductive trails Rina Zviel-Girshin @ASC
Programming Languages and Java • Programming languages • Java language • The basic program • Java syntax Rina Zviel-Girshin @ASC
Programming • The process of writing programs called a programming. • The process requires: • Programming language • A translator of the programming language into machine language • Programs are written in programming language. Rina Zviel-Girshin @ASC
Syntax • Syntax describes the grammatical rules of a language. • Valid words • Valid grammar constructions • Valid punctuation • Programs must be syntactically correct. Rina Zviel-Girshin @ASC
Semantics • Semantics gives the meaning of what you write with a language. • A programming language must precisely define the meaning of every statement that can be written with it. • Programs must be semantically correct. Rina Zviel-Girshin @ASC
Example • The following sentence is syntactically correct but semantically incorrect. “ Java programs are green and yellow.” • All words are correct words. • Punctuation is correct. • But logically the sentence makes no sense. Rina Zviel-Girshin @ASC
Programming languages • There are several families of the programming languages: • Procedural ( Pascal, C++, Java) • Logical ( Prolog) • Visual ( Visual Basic) • Document (HTML, LATEX) Rina Zviel-Girshin @ASC
Programming languages • Procedural languages can be divided into: • Machine languages • Low-level (Assembler ) • Intermediate-level (C) • High-Level (C++, Java) Rina Zviel-Girshin @ASC
Java • Java is high-level language. • Java is object oriented language. • In this course we will use the terms: • OOP, • OOA , • OOD. Rina Zviel-Girshin @ASC
OOA andOOD OOA- Object-Oriented Analysis is a way to analyze a problem. OOD- Object-Oriented Design is a way to design a solution. Both see the world as: • objects and • their relations (behavior, communication). Rina Zviel-Girshin @ASC
OOA andOOD (cont) • The way to analyze a problem is to define the objects relevant to it and their behavior. • The way to solve a problem is to realize the objects relevant and the behavior we need. • Both are useful as preliminary stages in OOP- Object Oriented Programming. Rina Zviel-Girshin @ASC
OOP Objects are the building blocks of: • the problem • the solution • the input source • the output target • the environment Rina Zviel-Girshin @ASC
Objects Objects include both: • data (fields, variables, state) • processing (behavior, methods, functions) Objects have two kinds of behavior: • outer – I/O, messages, relations with others • inner – processing (usually computing the messages) Rina Zviel-Girshin @ASC
Objects (cont) • Relations between objects are implemented using communication (messages). • Communication can have many forms. • Some forms are very different from the usual idea of a message. • Objects acquire structure using their relations. • The structure is usually tree-like. Rina Zviel-Girshin @ASC
High-level languages • Writing programs in machine language is possible but very difficult and time consuming. • Programs usually written in a more human readable language - high-level language. • Java is high-level language. • A program written in any computer language must be translated into a machine language in order to be executed. Rina Zviel-Girshin @ASC
Translators • Compiler • A compiler is a program that translates a source program (usually high level language) into target program (usually machine language program). • The resulting program can be executed many times. • Interpreter • An interpreter is a program that reads, translates and executes the source program statement by statement. • The translation is done each time the program runs. Rina Zviel-Girshin @ASC
Java • Java is a high-level, third generation programming language, like C, Fortran, Smalltalk. • Shares much of C's syntax. • Designed by a group at Sun MicroSystems. • Originally called Oak. Rina Zviel-Girshin @ASC
Java advantages • Platform independent “Write once, run anywhere”. • Improve robustness, remove unsafe language loopholes. • According to Sun: “ Java is simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high-performance, multithread, and dynamic language.” Rina Zviel-Girshin @ASC
Java disadvantages • The main Java disadvantage: Efficiency concerns. Looking to the future: VM performance will improve. Rina Zviel-Girshin @ASC
Java environments • Java has many environments. • The number grows as Java evolves. • Among them are: • Text applications • Beans • Applets • GUI Applications • Packages Rina Zviel-Girshin @ASC
Two main environments The two main environments: • Web Browser • Operating System Rina Zviel-Girshin @ASC
Web Browser • In the browser environment the browser acts as an intermediate between the program and the operating system. • The JVM resides inside the browser. • The program can be simpler. • The program has to work in graphical mode. • The program is called “Applet” (a small application). Rina Zviel-Girshin @ASC
Operating System • In the operating system environment the program is called “application”. • Application has a more rigid structure. • Application can be textual or graphical. • Application is less secure. Rina Zviel-Girshin @ASC
Learning Java The specific challenge is twofold: • Learning the language technical aspect: the large set of • new features • class libraries • classes • methods which are provided as part of the language. • Learning to maximize the benefits of the object-oriented paradigm. Rina Zviel-Girshin @ASC
Starting to Program • The basic idea of the course is to start programming as soon as possible. • At first you will have to take a lot of things on trust. • Promise: we will return to the details later. Rina Zviel-Girshin @ASC
Java programs All Java programs have three parts: • Auxiliary prescriptions • A class – an envelope around the other components • Code per se – • Optionally – other components which include the instructions themselves • Instructions to be executed Rina Zviel-Girshin @ASC
Writing a Java Program • Use an editor to type the program code. • Save the code file. • Compile the file with Java language compiler. • Fix the bugs! • Run the program. Rina Zviel-Girshin @ASC
A basic Java Program // Prints “How are you?” to the screen class My { public static void main (String[] args) { System.out.println(“How are you?”); } } Rina Zviel-Girshin @ASC
A basic Java Program // Prints “How are you?” to the screen class My { public static void main (String[] args) { System.out.println(“How are you?”); } } Rina Zviel-Girshin @ASC
Java program structure • A Java program is made up of one or more classes. • Classes have names. • The ‘My’ program consists of one class named ‘My’. Rina Zviel-Girshin @ASC
Class structure • A Java class contains one or more methods. • Methods have names too. • The program (the class) has to have a main method. • The method is called (surprisingly) – main. Rina Zviel-Girshin @ASC
Main • The main method has a standard syntax: public static void main(String args[]) { … // your code } • There can be only one main per file. • The main method contains the code to be executed when the program runs. Rina Zviel-Girshin @ASC
Method structure • A method contains one or more statements. • The main method of My program has a single statement: System.out.println(“How are you?”); Rina Zviel-Girshin @ASC
Stages of Writing and Executing Assumptions: • Using only the basic JDK software • Assuming the JDK was installed properly • In the WIN/DOS environment • Class name is My • File name is My.java • Directory (folder) name is c:\java Rina Zviel-Girshin @ASC
Stages of Writing and Executing • Open a text editor that can produce a plain text file (such as Notepad). • Create a source code file with the extension on the file name being .java . The file has to have the same name as the outer class. • Start a the DOS command environment by opening MSDOS prompt (using the Start->Programs->MSDOS Prompt series of choices). Rina Zviel-Girshin @ASC
Stages of Writing and Executing • Change directory to the directory containing the source file. • Compile the file, using the prompt command: javac My.java • The result is My.class in the same folder. • Run using the prompt command: java My Rina Zviel-Girshin @ASC
Applets The program has to import two packages: • applet.Applet • awt The program (the class) has to extend the Applet class, using the following syntax: public class My extends Applet { // your code } Rina Zviel-Girshin @ASC
Basic applet code The final code looks like this: import java.awt.*; import java.applet.*; public class My extends Applet { // your code } Rina Zviel-Girshin @ASC
Stages of Writing and Executing • Applet can’t be run directly. • Applet has to be run in Browser and by the Browser. • Web browser executes web pages. • Web page should call the applet code. Rina Zviel-Girshin @ASC
Call to applet The call to applet consist of: • the command APPLET • the name of the applet class • the dimensions of the panel in which the applet will run Rina Zviel-Girshin @ASC
Example • Assuming the name of the applet class is HelloWorld.class the call to applet looks like this: <APPLET CODE = "HelloWorld.class" WIDTH = 150 HEIGHT= 50> </APPLET> Rina Zviel-Girshin @ASC
HTML The call to applet has to be in HTML file. The file can look as follows: <HTML> <HEAD> <TITLE>Java applet test page</TITLE> </HEAD> <BODY> <APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=50> </APPLET> </BODY> </HTML> Rina Zviel-Girshin @ASC
Execution Assuming the file is called HelloWorld.html • The stages common to all, described above. • Open the file HelloWorld.html in the Browser. Rina Zviel-Girshin @ASC
Java Compiler • The Java compiler is called javac. • Java programs are compiled to byte code. Rina Zviel-Girshin @ASC
Byte code • The Java compiler translates Java program into a special representation called byte code. • Java byte code is a machine code for Java virtual Machine(JVM). • VM is a platform-specific tool to interpret the byte code and to translate it to commands for certain processor and OS. • The use of the byte code makes Java platform independent. Rina Zviel-Girshin @ASC
Compile-interpret-execute cycle Rina Zviel-Girshin @ASC
Compile-interpret-execute cycle Rina Zviel-Girshin @ASC
Errors • A program can have three types of errors: • Syntax and semantic errors – called compile-time errors • Run-time errors – occur during program execution • Logical errors Rina Zviel-Girshin @ASC