Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Object Oriented Programming (2) Java Programming (1) Lecturer: Eng. Mohamed Adam Isak PH.D Researcher in Computer Science, Sudan M.Sc. and B.Sc. of Information Technology Engineering, Libya Lecturer in Imam Shafi’i, University of Somalia and Mogadishu university Website: WWW.ENGMAISAK.COM BlogSpot: WWW.ENGMAISAK.BLOGSPOT.COM E-mail: [email protected]. Tel:0615648915 Book Chapters – Chapter 1 The history and evolution of JAVA – Chapter 2 An overview of Java – Chapter 3 Data Types, variables and arrays – Chapter 4 Operators – Chapter 5 Control Statements – Chapter 6 Introducing classes – Chapter 7 a closer look at methods and classes 3 Book Chapters, cont. – Chapter 8 Inheritance – Chapter 9 Packages and interfaces – Chapter 10 Exception handling – Chapter 16 Strings – Chapter 17 Event handling And more others 4 Chapter 1 The history and evolution of JAVA AN OVERVIEW OF COMPUTERS & PROGRAMMING LANGUAGES GOALS • To understand the activity of programming • To learn about the architecture of computers • To learn about level of programming languages • To become familiar with your computing environment and your compiler What is Programming? • Computer programming – the art and science of designing and writing computer programs. • Computer program – a sequences of instructions written in a programming language to achieve a task/to solve a problem. What is a computer? • Hardware – the physical, tangible parts of a computer – keyboard, monitor, disks, wires, chips, etc. • Software – programs and data – a program is a series of instructions • A computer requires both hardware and software • Each is essentially useless without the other Computer Organization Computer System Organization Hardware •CPU •Main Memory (RAM) •Secondary Storage •I/O Device Software •System Programs •Application Programs Hardware Components of a Computer Hardware Components of a Computer – motherboard adapter cards processor chip memory chips memory slots Expansion slots for adapter cards motherboard Hardware Components of a Computer CPU Brain of the computer, most expensive, the faster computer. CPU components– control unit, program counter, register instruction, arithmetic logic unit, accumulator. RAM Temporary memory, volatile, directly connected to the CPU, using memory cells unit. Hardware Components of a Computer SECONDARY STORAGE •Provides permanent storage for information. •Examples of secondary storage: •Hard disks •Floppy disks •Zip disks •CD-ROMs •Tapes. RAM vs. Secondary Storage • Primary memory – – – – – volatile Fast Expensive Low capacity Works directly with the processor • Secondary Storage – – – – – Nonvolatile Slow Cheap Large capacity Not connected directly to the processor Hardware Components of a Computer INPUT DEVICES Sheet-fed Hardware Components of a Computer OUTPUT DEVICES CPU and Main Memory Central Processing Unit Primary storage area for programs and data that are in active use Synonymous with RAM Main Memory Chip that executes program commands E.g.. Intel Pentium 4 Sun ultraSPARC III Secondary Memory Devices Secondary memory devices provide long-term storage Hard disks Floppy disks USB drives Writable CDs Writable DVDs Tapes Central Processing Unit Information is moved between main memory and secondary memory as needed Hard Disk Main Memory Floppy Disk Input / Output Devices Monitor Keyboard Monitor screen Keyboard Mouse Joystick Bar code scanner Touch screen Central Processing Unit I/O devices facilitate user interaction Hard Disk Main Memory Floppy Disk Hardware • Imagine that you are using a computer to write a letter. – Where is the is the word processor program that you are running located? – Where are the characters you have typed? Software Categories • System Software – Systems programs keep all the hardware and software running together smoothly – The most important system software is the operating system (OS) – controls all machine activities – provides the user interface to the computer – manages resources such as the CPU, memory & I/O – Windows XP, Unix, Linux, Mac OS • Application Software – generic term for any other kind of softwares – word processors, Spreadsheets, Web browsers, games Digital Information • Computers store all information digitally: – – – – – – numbers text graphics and images video audio program instructions • In some way, all information is digitized broken down into pieces and represented as numbers Representing Text Digitally • For example, every character is stored as a number, including spaces, digits, and punctuation • Corresponding upper and lower case letters are separate characters Hi, Heather. 72 105 44 32 72 101 97 116 104 101 114 46 American Standard Code for Information Interchange (ASCII) The ASCII data set 128 characters (0 until 127) Character A in ASCII 01000001 p. 4.15 Fig. 4-16 Next Binary Numbers • Once information is digitized, it is represented and stored in memory using the binary number system • A single binary digit (0 or 1) is called a bit • A single bit can represent two possible states, like a light bulb that is either on (1) or off (0) • Permutations of bits are used to store values Memory 9278 9279 9280 9281 9282 9283 9284 9285 9286 Main memory is divided into many memory locations (or cells) Each memory cell has a numeric address, which uniquely identifies it 26 Storing Information 9278 9279 9280 9281 9282 9283 9284 9285 9286 10011010 Each memory cell stores a set number of bits (usually 8 bits, or one byte) Large values are stored in consecutive memory locations 27 Storage Capacity • Every memory device has a storage capacity, indicating the number of bytes it can hold • Capacities are expressed in various units: Unit Symbol Number of Bytes kilobyte KB 210 = 1024 megabyte MB 220 (over 1 million) gigabyte GB 230 (over 1 billion) terabyte TB 240 (over 1 trillion) 28 Language Levels • Levels of 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 29 Programming Languages Machine language 101101100110 011011010 Assembly language iload intRate bipush 100 if_icmpgt intError High-level language if (intRate > 100) . . . Programming Languages • Each type of CPU executes instructions only in 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 from high level language into a specific machine language • An assembler translates from assembly language into a specific machine language 31 Programming Languages Language Description Examples Interpreter Machine Instruction in 0 and 1 bits 0111000011000 None 001 Assembly Instruction in mnemonic code LOAD 3 STOR 4 ADD High-level Similar to human sum = 4 + 3; language, FORTRAN, COBOL, Pascal, C, C++, Java… assembler Pengkompil &/ interpreter Java’s Lineage (Family) • Java is related to C++, which is a direct descendant of C. • Much of the character of Java is inherited from these two languages. • From C, Java derives its syntax. Many of Java’s object oriented features were influenced by C++ The Birth of Modern Programming: C • The creation of C was a direct result of the need for a structured, efficient, high-level language that could replace assembly code when creating systems programs. • As you probably know, when a computer language is designed, trade-offs are often made, such as the following: – Ease-of-use versus power – Safety versus efficiency – Rigidity versus extensibility C++: The Next Step • During the late 1970s and early 1980s, C became the dominant computer programming language, and it is still widely used today. • Since C is a successful and useful language, you might ask why a need for something else existed. • The answer is complexity. Throughout the history of programming, the increasing complexity of programs has driven the need for better ways to manage that complexity. C++ is a response to that need. • Another reason was: Approaches to programming have changed dramatically since the invention of the computer The C# Connection • The reach and power of Java continues to be felt in the world of computer language development. Many of its innovative features, constructs, and concepts have become part of the baseline for any new language. The success of Java is simply too important to ignore. • Perhaps the most important example of Java’s influence is C#. Created by Microsoft to support the .NET Framework, C# is closely related to Java. For example, both share the same general syntax, support distributed programming, and utilize the same object model. There are, of course, differences between Java and C#, but the overall “look and feel” of these languages is very similar How Java Changed the Internet • The Internet helped catapult Java to the forefront of programming, and Java, in turn, had a profound effect on the Internet. • In addition to simplifying web programming in general, Java innovated a new type of networked program called the applet that changed the way the online world thought about content. The Java Programming Language • Created by Sun Microsystems, Inc. introduced in 1995 and it's popularity has grown quickly since Rich library Platform-independent ("write once, run anywhere") or architecture-neutral The Creation of Java • Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. • It took 18 months to develop the first working version. This language was initially called “Oak,” but was renamed “Java” in 1995. • Between the initial implementation of Oak in the fall of 1992 and the public announcement of Java in the spring of 1995, many more people contributed to the design and evolution of the language. • Bill Joy, Arthur van Hoff, Jonathan Payne, Frank Yellin, and Tim Lindholm were key contributors to the maturing of the original prototype. Characteristics of Java (1) • Java is simple (2) • Java is architecture-neutral • Java is object-oriented • Java is portable • Java is distributed • Java’s performance • Java is interpreted • Java is dynamic • Java is secure 40 JDK Editions • Java Standard Edition (J2SE) – J2SE can be used to develop client-side standalone applications or applets. • Java Enterprise Edition (J2EE) – J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. • Java Micro Edition (J2ME). – J2ME can be used to develop applications for mobile devices such as cell phones. This book uses J2SE to introduce Java programming. 41 Java IDE Tools • Forte by Sun MicroSystems • Borland JBuilder • Microsoft Visual J++ • WebGain Café • IBM Visual Age for Java • Netbeans • Eclips NB. To learn how java codes can be ran using 42 command prompt is better Becoming Familiar with your Computer to use Java • • • • • • • Understand files and folders/directories Locate the Java compiler/ Install J2SE Set path & Java class path Write a simple program (later) Save your work Compile & run Use Dos Command Prompt or IDE hello_example.java public class hello_example { public static void main (String [] args) { System.out.println ("Amazing Hello from your special friend Mohamed Adam Isak !"); } } Hello_example Executing using DOS Command Java Program Elements • A Java program is made up of class definitions. • A class definition must contains a header and a body. • A class contains zero or more methods • A method is a named section of code that also has a header & body – A method contains program statements • Single-line (starts with //) and multi-line (enclosed by /* and */) comments are used to document the code Java Program Structure // comments about the class public class Hello { class header class body //Comments can be placed almost anywhere } 47 Java Program Structure // comments about the class public class MyProgram { // comments about the method public static void main (String[] args) { method body method header } } 48 Compiling and Running Type program into text editor Save (file name must be similar to class name) Open Dos Window Change directory to saved file directory Compile into byte codes javac hello_example.java Execute byte codes java hello_example From Source Code to Running Program Hello_example.java Hello_example.class javac java The Edit-Compile-Test Loop 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 52 Java Translation Java source code Java compiler Java bytecode Bytecode interpreter Machine code 53 Types of Java Code Errors 1. Syntax errors System.our.print("..."); System.out.print("Hello); Detected by the compiler 1. Logic errors System.out.print("Hell"); Detected (hopefully) through testing 2. Run-time errors Occur during program running How to Write , compile and Run java Program via Command Prompt(CMD) in Windows 1. Download latest JDK 64bit or 86bit (jdk1.7.0_25 x64 OR jdk1.7.0_25 x86) & install it. 2. Right click on Computer icon(desktop) and click properties click Advance System Settings. click Environment Variables In the system variables box click on Path ----Click Edit In the variable value just ADD the following: C:\Program Files\Java\jdk1.7.0_25\bin. ((it must be explored and assured whether the installed folder of java is there or not)), Then, save every thing you've done by clicking OK. 3. Now open CMD(windows key +R and type cmd & Enter) 4. Open the notepad program 5. Write your java program in the notepad and save it in your Home Directory. i.e. C:/Users/usrername(currently you have logged in) 6. Now you can compile your java program by javac<space><filename.java> and by typing java<space>filename you can run your java program. **This process is also applicable for 86bit windows(7&8). Change is that you have to download latest jdk 32bit. All the rest is same Available jobs for JAVA developers Example 1.1 Example 1.2 Example 1.3 Example 1.4 www.engmaisak.com