Lesson 1 - Introduction to Java PDF
Document Details
Uploaded by UserReplaceableSurrealism
Polytechnic University of the Philippines
Marie Andrea Zurbano
Tags
Summary
This lesson introduces object-oriented programming concepts using Java. It covers topics such as different programming paradigms, encapsulation, abstraction, inheritance, and polymorphism. The document also includes a summary of Java and its applications.
Full Transcript
OBJECT ORIENTED PROGRAMMING LESSON 1 : Introduction to Object Oriented Programming using Java Prepared by: Marie Andrea Zurbano IT Instructor TWO TYPES OF PROGRAMMING PARADIGM 1. Structured Programming - A programming p...
OBJECT ORIENTED PROGRAMMING LESSON 1 : Introduction to Object Oriented Programming using Java Prepared by: Marie Andrea Zurbano IT Instructor TWO TYPES OF PROGRAMMING PARADIGM 1. Structured Programming - A programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures and for and while loops. - Also known as procedural programming that uses the principle of Top-Down Design Principle first created by Dr. Edsger W. Dijkstra in 1960s. - It uses the following control structure: a. Sequence b. Selection / Condition c. Iteration / Loops TWO TYPES OF PROGRAMMING PARADIGM 2. Object Oriented Programming - A programming paradigm based on the concept of “objects”, which are data structure that contain data, in the form of fields, often known as attributes, and code, in the form of procedures, often known as methods. - In OO programming, computer programs are designed by making them out of objects that interact with one another. - There is significant diversity in object-oriented programming, but most popular languages are class-based, meaning that objects are instances of classes, which typically also determines their type. BASIC CONCEPT OF OBJECT ORIENTED PROGRAMMING Objects are the basic runtime entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program may handle. When a program is executed, the objects interact by sending messages to one anther. Objects can interact without having to know the details of each other’s data or code. It is sufficient to know the type of message accepted and the type of response returned by the objects. A class may be thought of as a ‘data type’ and an object as a ‘variable’ of that data type. Once the class has been defined, we can create any number of objects belonging to that class. A class is a collection of objects of similar type. Classes are user-defined data types and behave like the built-in types of a programming language. REPRESENTATION OF AN OBJECT Object PERSON Name Data BasicPay Salary() Methods Tax() ENCAPSULATION The wrapping up of data and methods into a single unit (called class) is known as encapsulation. The data is not accessible to the outside world and only those methods, which are wrapped in the class, can access it. This insulation of the data and direct access by the program is called data hiding. Encapsulation makes it possible for objects to be treated like ‘black boxes’, each performing a specific task without any concern for internal implementation. Encapsulation is one of the three OOP principles, the other two being inheritance and polymorphism. ENCAPSULATION – OBJECTS AS “BLACK BOXES” Data Information “out” Information “in” and Method ABSTRACTION Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight, and cost, and methods that operate on these attributes. The concept of data encapsulation and abstraction can be well understood by considering the simple example of a mobile phone. INHERITANCE Inheritance is the process by which objects of one class acquire the properties of objects of another class. Inheritance supports the concept of hierarchical classification. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. In Java, the derived class is known as ‘subclass’. Note that each sub class defines only those features that are unique to it. Without the use on inheritance, each class would have to explicitly include all of its features. PROPERTY INHERITANCE POLYMORPHISM Polymorphism is another important OOP concept. Polymorphism means the ability to take more than one form. Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface. Polymorphism is extensively used in implementing inheritance. For example, consider the operation addition. For two numbers, the operation will generate sum. If the operands are strings, the operation would produce third string by concatenation. POLYMORPHISM Shape Draw() Circle Object Box Object Triangle Object Draw(Circle) Draw(Box) Draw(Traingle) BENEFITS OF OOP Through inheritance, we can eliminate redundant code and extend the use of existing classes. We can build programs from standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving development time and higher productivity. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. It is possible to have multiple objects to coexist without any interference. It is easy to partition the work in a project based on objects. Object-oriented system can be easily upgraded from small to large systems. Software complexity can be easily managed. APPLICATIONS OF OOP Object-oriented technology is certainly changing the way software engineers think, analyze, design and implement systems today. Real-time systems Simulation and modeling Object-oriented databases AI and expert systems Neural Networks and parallel programming Decision support and office automation systems CAD System SUMMARY Java is an object-oriented language. It enables us not only to organize our program code into logical units called objects but also to take advantage of encapsulation, inheritance, and polymorphism. Basic concepts of object-oriented programming includes: a. Encapsulation b. Inheritance, and c. Polymorphism REVIEW QUESTIONS 1. What do you think are the major issues facing the software industry today? 2. What is object-oriented programming? How is it different from procedure- oriented programming? 3. What are the unique advantages of an object-oriented programming paradigm? 4. Describe Inheritance as applied to OOP. STATE WHETHER THE FOLLOWING STATEMENTS ARE TRUE OR FALSE: 1. In conventional, procedure-oriented programming, all data are shared by all fucntions. 2. The main emphasis of procedure-oriented programming is on algorithm rather than on data. 3. One of the striking features of object-oriented programming is the division of programs into objects that represent real-world entities. 4. Inheritance means the ability to reuse the data values of one object by other objects. 5. Polymorphism is extensively used in implementing inheritance. 6. Object-oriented approach cannot be used to create databases. 7. Object-oriented systems can scape up better from small to large. INTRODUCTION TO JAVA WHAT IS JAVA? Java programming language is one of the most popular and widely used programming language, both in education and in the industry. Developed by Dr. James Gosling and his colleagues in early 1990’s at Sun Microsystem. Java was released in 1995 as part of Sun Microsystem’s Java platform. Java is an open source language that is freely available over the Internet to download and use. One of the interesting features of Java language is that it is an object- oriented language that makes software development much easier to design and implement. BRIEF HISTORY OF JAVA James Gosling, Mike Sheridan, and Patrick Naughton initiated Java language project in June 1991. Java, from Java coffee. Gosling designed Java with a C/C++ style syntax that system and application programmers would find familiar. Java 1.0 was released by Sun Microsystems in 1995 “Write Once, Run Anywhere” (WORA), providing no-cost-run-times on popular platforms. Fairly secure and featuring configurable security, it allowed network and file-access restrictions. Major web browsers soon incorporated the ability to run Java applets within web pages, and Java quickly become popular. On 13 November, 2006, Sun released much of Java as free and open-source software. Oracle Corporation acquired Sun Microsystems in 2009-2010 Java software runs on everything from laptops to data centers, games console to scientific supercomputers. TYPES OF JAVA APPLICATIONS 1. Java Applet is usually stored on a website and is downloaded and runs on a client computer from within a web server. 2. Application can only run on the computer. If online, it has to be downloaded before being run. 3. JAR (Java Archive) File is used to package java files together into a single file (Almost exactly like a.zip file.) 4. Servlet runs on a web server and helps to display web pages. 5. Swing application is used to build an application that has a GUI (windows, buttons, menus, etc.) 6. EJB (Enterprise Java Beans) runs on a web server and is used to develop large, complex websites. CHARACTERISTICS OF JAVA 1. Java is simple. 2. Java is object-oriented. 3. Java is distributed. 4. Java is interpreted. 5. Java is robust. 6. Java is secure. 7. Java is architecture-neutral. 8. Java is portable. 9. Java is multithreaded. 10. Java is dynamic. BENEFITS OF JAVA 1. Java is commonly used to teach students how to program as a first language, yet is still used by professionals. 2. Java requires each method that declares a return type, always return a value. This also prevents bugs. 3. Java comes with a large set of classes and methods, the Java API that can be used without having to develop as much code “from scratch”. 4. Code compiled on one Java platform can be run on other platforms that support Java without modification of either the source-code nor the byte- code. SAMPLE JAVA PROGRAM Sample program output. Hello world. End of Program. Class Hello_World{ public static void main(String[] args){ System.out.println(“Hello World.”); System.out.println(); System.out.print(“End of Program.”); } } ANALYZING THE PROGRAM The filename of the program is Hello_World.java and the class name is Hello_World. They should be the same in every program you write in java to avoid error during compilation. The first keyword is class. A class is fundamental unit of program structure in Java. All Java program are classes. The second line is public static void main(String[] args). It is the starting point of program execution of Java program. The keyword “public” means any variables or value that is being declared in the method can be used anywhere in our program. The next statement System.out.println(“Hello World.”); will display the message Hello World. on the screen. TOOLS NEEDED IN WRITING AND WRITING YOUR JAVA PROGRAM To write a Java program, you need the following: 1. Java SE Development Kit 8 (JDK 8) 2. Text Editor - Any text editor will do as long as you can save your source code in plain text. - Example of text editors is Notepad, Eclipse, Netbeans, and Notepad++. JAVA VARIABLE DECLARATION A variable is a value that can be change depending on conditions or on information passed to the program. A variable can be reused through out your program, instead of having to type out the actual value all over and over again. In Java, you can define a variable with the following form: Data Type Variable Name and Value: Ex: String User_Name = “Jake R. Pomperada” RULES USING VARIABLES A variable can contain: 1. Any unique character that is a letter (including numeric letters like Roman numerals) or a digit. 2. Currency sign (such as $). 3. Connecting punctuation character (such as _ ). RULES USING VARIABLES A variable cannot: 1. Variable names are case-sensitive 2. A variable’s name can be any legal identifier. 3. It can contain unicode letter, digits of two special characters such as underscore and dollar sign. 4. Length of Variable name can be any number. 5. It is necessary to use alphabet at the start 6. Some auto generated variables may contain ‘$’ sign. But try to avoid using dollar sign. 7. White space is not permitted. 8. Special characters are not allowed. 9. A digit at the start is not allowed. 10. Subsequent character may be letters, digits, dollar sign, or underscore characters. 11. Variable name must not be a keyword or reserved word. CONSTANT VARIABLES Constant variables refers to those variables whose value does not change during program execution. This type of variable is very useful if we want to assign value to a variable that is fixed. Sample program IDENTIFIERS Identifiers are names given to various program elements such as variables, functions and arrays. The following are the guidelines in creating identifiers in Java. 1. It consists of letters and digits without space, in any other, except that the first character must be a letter. 2. Uppercase and lowercase are permitted but they are not interchangeable. As an exception, an underscore may be used as the first character of the identifier. 3. Reserved words and standard methods must not be used. EXAMPLE OF IDENTIFIERS Valid Identifiers Invalid Identifiers Salary_Per_Month 3rd Age BIR TAX Grade23 %price _area_circle while PERSONS_NAME Y Users_Name DATA TYPE This is a set of data that specifies the possible range of values of the set, the operations that can be performed on the values, and the way in which the values are stored in memory. Understanding this data type enables the computer to manipulate it appropriately. Type Name Value Range byte integer -128 through + 127 short integer -32,768 through + 32,767 int integer -2,147,483,648 through +2,147,483,648 long integer -9,223,372,036,854,775,808 through +9,223,372,036,854,775,807 float floating point number +-1.401298E+45 through +-3.402823E+38 double floating point number +-4.94065645841246E-324 through boolean Boolean True or false char UTF-16 code unit ‘\u0000’ through ‘\uFFF’ JAVA OPERATOR Java provides a rich set of operators environment. Java operators can be divided into the following categories: 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators ARITHMETIC OPERATORS Arithmetic operators are used in mathematical expression in the same way that are used in algebra. OPERATOR DESCRIPTION + Adds two operands - Subtract second operators from first * Multiply two operand / Divide numerator by denumerator % Remainder of division ++ Increment operator increases integer value by one -- Decrement operator decreases integer value by one RELATIONAL OPERATORS The following table shows all relational operators supported by Java. OPERATOR DESCRIPTION == Check if two operand are equal != Check if two operand are not equal > Check if operand on the left is greater than operand on the right < Check operand on the left is greater than or equal to right operand >= Check if left operand is greater than or equal to right operand