CS0070 Object-Oriented Programming Module 1 PDF
Document Details
Uploaded by RobustEllipse3241
null
null
null
Tags
Related
- OOP - JAVA Object-Oriented Programming PDF
- Object Oriented Programming (Java) PDF
- Object Oriented Programming (2nd Year 1st Sem) PDF
- Introduction to Java Programming and Data Structures (2019) by Y. Daniel Liang - PDF
- VILLASAN Object-Oriented Programming PDF
- Introduction To Object Oriented Programming PDF
Summary
This document provides an introduction to Java technology and object-oriented programming. It covers the history and features of Java, including its simple object-oriented design. It also details the steps in creating a basic Java application.
Full Transcript
CS0070 MODULE 1: Introduction to Java Technology A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Module 1: Introduction to Java Technology History and Features...
CS0070 MODULE 1: Introduction to Java Technology A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Module 1: Introduction to Java Technology History and Features of Java Features of Java 1. Simple, object-oriented, and familiar History 2. Robust and secure Java language, originally called the ‘Oak,’ was developed 3. Architecture neutral and portable by James Gosling at Sun Microsystems, which is now a 4. High performance subsidiary of Oracle Corporation, and released in 1995 as a core component of Sun Microsystems’ Java platform. 5. Interpreted, threaded, and dynamic It has derived much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java A general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. The Java source code files (files with.java extension) are compiled into a format called bytecode (files with.class Java Editions extension), which can be executed by a Java interpreter. J2SE - Java 2 Standard Edition It was intended for use in Sun’s project research to work on a programming software to control electronic devices. J2EE - Java 2 Enterprise Edition It was modified to take advantage of the World Wide Web. J2ME - Java 2 Micro Edition The WebRunner, later named as HotJava, was the very first web browser created in Java. In 1995, the Netscape Navigator internet browser was also developed to support Java. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 1 1 CS0070 MODULE 1: Introduction to Java Technology A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Java Virtual Machine (JVM) Java is a portable language that could run on any platform. The language was able to do this by generating intermediate code for a hypothetical computer called a virtual machine. The Java Virtual Machine (JVM) is a program that runs on all computers. The JVM creates a software simulation of a CPU and memory and handles all communication between the Java program and the underlying operating system and hardware. The Java source code (.java file) is normally compiled to produce the bytecode file (.class file) which is normally interpreted by the Java virtual machine (JVM). Java 2 Software Development Kit (J2SDK / JDK) The Java 2 Software Development Kit contains: - java - the loader for Java applications - javac - the compiler. - javadoc - the documentation generator. - Other tools. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 1 2 CS0070 MODULE 1: Introduction to Java Technology A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Steps in Creative a Java Application Exploring Netbeans IDE 1. Create the source code Introduction to IDE Create the source code using any text editor. An Integrated Development Environment (IDE) is a Save the file with a “.java” extension. computer software to help computer programmers - Example: HelloJava.java develop software. The Leaders: 2. Compiling the source code - NetBeans - Microsoft Visual Studio Compile the source code to create the byte code using - Eclipse javac tool from the J2SDK. What does an IDE consist of: Open an MS-DOS console, browse for the directory where - Source code editor you saved the source code. - Compiler and/or interpreter - Build automation tools Type the command javac HelloJava.java and press ENTER. Optional Tools: - Debugger Verify that the HelloJava.class has been created. - Version control system - Various tools to simplify the construction of a GUI. 3. Running the program Tools for Object-Oriented Design: - Object inspector To execute the program, type the command: - Class browser java HelloJava and press ENTER. - Class hierarchy diagram Why Do We Need An IDE IDE abstracts the configuration necessary to piece together various utilities in one unit, which could ease the learning of a language, and increases developer productivity. Most IDEs today have GUI modeling utilities that simplify the development of UIs, which is critical for commercial software today. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 1 3 CS0070 MODULE 1: Introduction to Java Technology A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Web Services Development: wizards for creating web NetBeans services and web services clients, providing the basic (java/wsdl) code needed, and easy to use testing tools of History of NetBeans existing web services. Java 2 Platform, Micro Edition (J2ME) MIDP It all started as a student project called Xelfi. development: visual design editor with end-to-end The Goal was to write a Delphi-like Java IDE in Java for support for enterprise applications. the first time. Code Editor: Syntax highlighting for Java, XML, HTML, The original plan was to develop network-enabled CSS, JSP and IDL, full support of new JDK 1.5 features, live JavaBeans components, hence the name. but coming parsing/error marking, popup javadoc, code completion, out of the spec for enterprise changed the plans. and fast class importing. Sun decided it needs a more powerful Java development Refactoring: renaming, changing and moving of various tool, and the rest is history. objects, field encapsulation and usage finding. Award Winning Debugger: Language independent About NetBeans debugger core, variable modification and watches, various breakpoints and “Fix and Continue” mechanism. A fast fully-featured Integrated Development GUI Builder: fully WYSIWYG (What You See Is What You Environment (IDE) with support for Java. Get) designer with "Test Form" feature, extensible Compliant applications for accelerating development Component Palette pre-installed Swing and AWT across all major OS platforms. components, showing a components tree and properties, Provides an open source, high performance, modular, automatic code generation and full JavaBeans support. extensible, multi-platform Java IDE for GUI, mobile tools, Version control Support: supports command-lined vcs, Web, and Desktop applications. supplying merging and diff tools and containing a built- in Written in java and therefore runs on every operating CVS client. XML: XML, DTD and CSS Text Editor and XML system that supports Java VM (Virtual Machine). Productivity Tools Wizards to help user generate codes. NetBeans Features NetBeans Extras Environment: easily configured user interface and a NetBeans Profiler: provides information about the modular architecture extensible with additional plugins. runtime behavior of applications. Allows developers to monitor the thread state, CPU performance, and memory Project System: support for multiple source roots, easy usage of their applications. makes it easy to track down management of libraries, easily ported to other performance problems and memory leaks. environments, all based on Apache Ant. NetBeans Platform: provides the services common to Web Development: Web Application project type, almost all large desktop applications such as: window, Supports the J2EE 1.3 and 1.4 standards with web menu, settings management and storage, file access and application build support based on Apache Ant. more. Enterprise Java Beans (EJB) Development: easy to create and deploy and import java beans. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 1 4 CS0070 MODULE 1: Introduction to Java Technology A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO NetBeans Mobility Pack: used to write, test, and debug NetBeans Matisse - Advantages applications for the Java Micro Edition platform (J2ME) technology-enabled mobile devices. It integrates support For the first time, there’s an intelligent way to build GUI for for the Mobile Information Device Profile (MIDP) 2.0, the Java (unlike eclipse), that can actually compete with the Connected, Limited Device Configuration (CLDC) 1.1. Visual Studio Gui builder. The mobility pack allows for the unique “On-Phone” Supports internationalization, and industrial look-and-feel debugging mode. rules, which is very important for large scale application meant to be spread world wide. NetBeans Matisse Builds also GUI web applications, HTML, JSP, etc. The biggest improvement from the previous version and NetBeans Matisse - Disadvantages the feature with the biggest impact is the new GUI- Builder, Matisse. No built-in support for Drag-n-Drop or double-click The Goal: to take the best features from OSX and VS events. designers and allow the same possibilities for Java Programmers. Matisse’s code is protected so customizing is not very easy and not always possible. In order to reach that goal there was a need to develop a new layout manager to support all the needed Not all applications are easily built. For instance, an MDI functionalities. Project is not that trivial to build. Matisse provides a simple and intuitive layout of GUIs without having to understand the complexities of Swing layout managers. As you drag and drop components into a form, the IDE automatically suggests alignment, spacing, and resizing constraints. By simply right clicking a UI Object you can add an event handler with a method waiting to be implemented without knowing too much about the surrounding of this object. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 1 5 CS0070 MODULE 2: Fundamentals of Java Language A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Module 2: Fundamentals of Java Language Basic Structure of a Java Program Basic Structure of a Java Class class HelloWorld – defines a class, a template for an object of derived type HelloWorld. Java Comments public – access specifier/modifier, the main method is Are of three (3) types: declared public so that it is accessible as part of the public - A single-line comment starting with // interface of the program. - A multi-line comment enclosed within static – state of the method, it is static because it must - A documentation or javadoc comment is enclosed be called before the class that hosts the method is between. These comments can be used to instantiated. generate HTML documents using the javadoc utility, void – It returns void because the Java interpreter does which is part of Java language comment. not expect to receive or process any output from the class. System.out.println() – The println method is one of Java Data Types dozens of methods in the System class. The System class is a part of the core Java language package of the Primitive Data Types: Application Programming Interface (API). Java Language Fundamentals Identifiers Are names that are given by the programmer as name Derived Data Types of variables, methods or functions, classes etc. - String The name used as an identifier must follow the following - Integer rules in Java™ technology: - Double - Each character is either a digit, letter, underscore or - Long currency symbol. - Float - First character cannot be a digit. - The identifier name must not be a reserved word. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 2 6 CS0070 MODULE 2: Fundamentals of Java Language A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Variables Basic Input and Output Variable Declaration Java Output - Syntax: ; [= value;] - Example: In Java, you can simply use: - System.out.println(); - System.out.print(); - System.out.printf(); System is a class. Variable Declaration - Syntax: = value; out is a public static field: it accepts output data. - Example: Wrapper Classes Are used in converting one data type (such as a String) Difference between println(), print() and printf() into another data type (such as int or double). - print() - It prints string inside the quotes. It is also used in wrapping a primitive value into an object. - println() - It prints string inside the quotes similar like print() method. Then the cursor moves to the beginning of the next line. - printf() - It provides string formatting (similar to printf in C/C++ programming). CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 2 7 CS0070 MODULE 2: Fundamentals of Java Language A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Java User Input (Scanner) The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 2 8 CS0070 MODULE 3: Operators A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Module 3: Operators Java Operators Arithmetic Operators There are some general guidelines to consider when Unary Arithmetic Operators creating arithmetic expressions: - Use parentheses to alter precedence. Increment and decrement operators have two formats: - Consider the size of resulting values and possible loss postfix and prefix. of precision. Sign Operators: + and - - Apply arithmetic operators’ precedence level. Increment: ++ - When using negative numbers with modulus Decrement: -- calculation, drop the negative signs from either operand and calculate the result. The sign of the left operand is the sign of the result. - The (+) operator can be used to add numbers and String objects. - Dividing integer by zero results in the throwing of ArithmeticException Bitwise Operators Works on integer values, by manipulating its bit-pattern equivalent. Basic Arithmetic Operators Addition: + Subtraction: - Multiplication: * Division: / Modulo DIvision: % CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 3 9 CS0070 MODULE 3: Operators A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Shift Operators Cast Operators Performs bit manipulation on data by shifting the bits of Used in converting (casting) one type into another type or its first operand right or left. an object into another object. - Syntax: (Cast type) Value; This table summarizes the shift operators available in the - Example: Java programming language. Assignment Operators Relational Operators Used to assign values to a variable Shorthand operator if combined with other operators. Compares two values and determines the relationship between them. Example: Logical Operators Ternary Operators Often used with relational operators to construct more An operator that deals with three operands. complex decision-making expressions. - Syntax: variable = condition ? valueTrue : valueFalse; - Example: CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 3 10 CS0070 MODULE 4: Control Structures and Arrays A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Module 4: Control Structures and Arrays Control Structures while Statement if-else Statement The while loop executes a given statement or block of statements repeatedly as long as the value of the expression is true The if statement enables your program to selectively execute other statements, based on some criteria. Syntax: while (condition / expression) { The else statement performs a different set of statements //codes to execute if condition is true if the expression is false. } Syntax: if (condition / boolean expression) { // codes to execute if condition is true do-while Statement } else { The do-while loop facilitates evaluation of condition or // codes to execute if condition is false expression at the end of the loop to ensure that the } statements are executed at least once. Syntax: switch Statement do { //codes to execute if condition is true The switch statement is used to conditionally perform } while (condition / expression) statements based on an integer expression. Syntax: for Statement switch (varName) { The for loop executes a given statement or a block of case const1: statements for a definite number of times. codes here; break; Syntax: case const2: for (initialization; condition; altering list) { codes here; // codes to execute if condition is true break; } default: codes here; break; } The break statement causes the program flow to exit from the body of the switch construct. Each case label takes only a single argument, but when execution jumps to one of these labels, it continues downward until it reaches a break statement CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 4 11 CS0070 MODULE 4: Control Structures and Arrays A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO foreach Statement label Statement The foreach loop is for traversing items in a collection. The label identifies any valid statement to which control foreach is usually used in place of a standard for must be transferred statement. Syntax: It usually maintain no explicit counter: they essentially say [label:] statements; "do this to everything in this set", rather than "do this x times". Arrays Syntax: for (initialization; [collection/array]) { Array // codes to execute if condition is true } An array is a structure that holds multiple values of the Example: same type. The length of an array is established when the array is created (at runtime). After creation, an array is a fixed-length structure. break Statement Syntax: The break statement causes the program flow to exit type arrayName[] = new type[length]; prematurely from the body of the loop statement. Example: Syntax: continue [label]; continue Statement The continue statement causes the program flow to skip over and jump to the end of the loop body, and then return the control to the loop control statement. Syntax: continue [label]; CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 4 12 CS0070 MODULE 4: Control Structures and Arrays A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Array Initialization Syntax: type arrayName[] = {value, value,...}; Example: Storing Values in an Array Syntax: arrayName[index] = value; Example: Multi-dimensional Array Syntax: type[][] = new type[row][column]; Example: CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 4 13 CS0070 MODULE 5: Introduction to Object-Oriented Programming A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Module 5: Introduction to Object-Oriented Programming Object-Oriented Programming Concepts Programming Model Procedural Programming Is a technique of solving a problem and breaking it down into smaller parts and solving each of the smaller Concept of OOP problems. This model of software development is process-centric or Object-Oriented Programming procedural since it concentrates on the procedures in a system. It uses a collection of objects interacting with each other. Object-Oriented Programming One of the principal advantages of using OOP technique is that it enables programmers to create modules that It describes the task to be performed on objects. need not be changed when a new type of object is added. These objects that have to be created and stored in the computer memory, contain data and instructions to Benefits of OOP: perform tasks specific to that object. Reduced software complexities and realistic modelling Procedural vs OOP through the use of various techniques: - Abstraction – the process of picking out the common The focus of procedural programming is to break down a features of objects and procedures. This process focuses programming task into a collection of variables, data on the essential characteristics of an object. structures, and subroutines - Encapsulation – the process of hiding the implementation details of an object. Whereas in object-oriented programming, it is to break down a programming task into objects that expose Software reusability by implementing inheritance. behavior (methods) and data (members or attributes) Programs are built that can be used by several other using interfaces. applications. - Inheritance – allows an object to extend its The most important distinction is that while procedural characteristics to another object. programming uses procedures to operate on data structures, object-oriented programming bundles the two Resilience to change with the use of polymorphism. together, so an "object", which is an instance of a class, - Polymorphism – ability to process objects differently operates on its "own" data structure. depending on their data type or class. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 5 14 CS0070 MODULE 5: Introduction to Object-Oriented Programming A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Class Object Relationships A class is a template or blueprint that defines an object’s Inheritance attributes and operations and that is created at design time. Referred to as the “kind of” or “is a” relationship. Object An object is a running instance of a class that consumes memory and has a finite lifespan Superclass (Base Class) The parent class from which properties are inherited by another class. Subclass (Derived Class) A new class with properties taken from a parent class. Abstract Class A well-encapsulated conceptual class. The objects of this class do not exist in real world. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 5 15 CS0070 MODULE 5: Introduction to Object-Oriented Programming A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Unified Modeling Language (UML) Declaring Classes Unified Modeling Language (UML) Attributes A general-purpose notational language used for Declared either at the start or end of a class definition. specifying and visualizing complex software, usually These variables identify the data stored in the object. large object-oriented project. It uses several types of diagrams such as the case diagram, Constructor class diagram, and state chart diagram. A method that is automatically executed when an Class Diagram object is created. This method is used to initialize the attributes. Used to describe the attributes and behaviours of objects in the system. Standard Methods UML Access Modifier Symbols Used to change or access the private data in an object. Mutator (Setter) – a method that is used to change the + - public data. - - private Accessor (Getter) – a method that is used to access or # - protected retrieve data. ~ - package “ “ - no symbol indicates default access Custom Methods italics - represents abstract class method Implement the business rules for which the application is underline – represents static method or attribute being developed. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 5 16 CS0070 MODULE 5: Introduction to Object-Oriented Programming A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Steps in Declaring Classes this keyword Step 1: Define the ClassName Contains a reference to the current object being constructed. Step 2: Declare the attributes It represents an instance of the class in which it appears. Step 3: Create constructor It can be used to access class variables and methods. Step 4: Create standard methods (setters and getters) Step 5: Create custom methods Usage of this keyword 1. can be used to refer current class instance variable. 2. this() can be used to invoke current class constructor. 3. can be used to invoke current class method (implicitly) 4. can be passed as an argument in the method call. 5. can be passed as argument in the constructor call. 6. can also be used to return the current class instance. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 5 17 CS0070 MODULE 5: Introduction to Object-Oriented Programming A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Access Modifiers Modifiers/Qualifiers private final - Variable: a variable that is only accessible within a - Class: a class that can never be sub-classed. class where it is declared. - Variable: constant variable. - Method: a method that is only accessible within the - Method: a method that cannot be overridden. class where it is declared. static public - Variable: used in defining a class variable. - Variable: a variable that is accessible from all classes. - Method: used in defining a class method. - Method: : a method that is accessible from all classes. abstract default (no keyword) interface - The default modifier is accessible only within the package. Classification of Variables - If you don't use any modifier, it is treated as default by default. Instance Variables (non-static fields) protected. - The protected access modifier is accessible within Variables stored in each object of a class, usually and outside the package but through inheritance only. referred to as the non-static member fields of a class. - It can be applied on the data member, method and constructor. It can't be applied on the class. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 5 18 CS0070 MODULE 5: Introduction to Object-Oriented Programming A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Class Variables (static fields) Variables stored in the class and are available to all objects of a class or objects of other classes if access is permitted, usually referred to as the static members of the class. Local Variables (method variables or local data) Variables stored in the class and are available to all objects of a class or objects of other classes if access is permitted, usually referred to as the static members of the class. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 5 19 CS0070 MODULE 6: Encapsulation and Inheritance A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Module 6: Encapsulation and Inheritance Encapsulation Encapsulation To make sure that "sensitive" data is hidden from users. To achieve this, you must: - declare class variables/attributes as private (only accessible within the same class) - provide public setter and getter methods to access and update the value of a private variable Get and Set private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public getter (accessor) and setter (mutator) methods. The get method returns the variable value, and the set method sets the value. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 6 20 CS0070 MODULE 6: Encapsulation and Inheritance A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Why Encapsulation? Superclasses and Subclasses Better control of class attributes and methods. Superclass Class variables can be made read-only (if you omit the set method), or write-only (if you omit the get method). The class from which another class inherits properties. Flexible: the programmer can change one part of the code This is a class that is on top of a hierarchy. without affecting other parts. Subclass Increased security of data. A class that inherits all the non-private attributes and Inheritance methods, except constructors from a superclass. This class has the ability to override methods of the Inheritance superclass. The technique of deriving new class definitions from an existing class definition. The following are the benefits of using class inheritance in OOP: - Re-use of predefined and well-tested classes. - Standardization of behaviors across a group of classes. - Ability to use members of a family of classes interchangeably in methods. Java does not support multiple inheritance, however, Java provides same effects and benefits of multiple inheritance through the use of interface. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 6 21 CS0070 MODULE 6: Encapsulation and Inheritance A.Y 2024-2025 — FIRST SEMESTER ︱ PROF. HADJI TEJUCO Types of Inheritance this and super keyword Single Inheritance this Subclasses are derived from a single superclass. Contains a reference to the current object being constructed. The derived class inherits the data members and methods of the superclass. It represents an instance of the class in which it appears. It can be used to access class variables and methods. super Contains a reference to the parent class (superclass) Multiple Inheritance object. It is used to access members of a class inherited by the Subclasses are derived from more than one superclass. class in which it appears. The derived class inherits the data members and It explicitly call a constructor of its immediate methods of its superclasses. superclass. A super constructor call in the constructor of a subclass will result in the execution of relevant constructor from the superclass, based on the arguments passed. Override A subclass can override a method defined in its superclass by providing a new implementation for that method. final Methods and final Classes In Java, it is also possible to declare classes that can no longer be subclassed. These classes are called final classes. CS0070: OBJECT-ORIENTED PROGRAMMING MODULE 6 22