L_01_Introduction to OOP and Java.pdf
Document Details
Uploaded by StainlessJungle
Full Transcript
Platform Independent Learning IT304021 Faculty of Information Technology University of Vocational Technology Ratmalana, Sri Lanka Ms. DMS Sathsara (Visiting Lecturer) Course Outline Introduction to Object-Oriented Programming (OOP) Intro...
Platform Independent Learning IT304021 Faculty of Information Technology University of Vocational Technology Ratmalana, Sri Lanka Ms. DMS Sathsara (Visiting Lecturer) Course Outline Introduction to Object-Oriented Programming (OOP) Introduction to Java Java Basics Inheritance and Interfaces Exception Handling AWT Components & Swing Course Outline Prerequisites: complete semester 1 and 2 Aim: To provide the concepts of object oriented programming using Java. To provide knowledge about developing real world projects with object oriented concepts. Assessment Criteria Continuous assessment :40% ○ In Class Test -20% ○ Mini Project - 20% End – Semester Examination (03 hours paper) :60% References W. Thomas, Introduction to Object-Oriented Programming with Java. McGraw-Hill Higher Education, 2005. K. Arnold, J. Gosling, and D. Holmes, The Java TM Programming Language. Artmed, 2006 T. Budd, Understanding Object-Oriented Programming with Java. Pearson Education, 2002. Introduction to object oriented Programming What is Object Oriented Programming(OOP)? Object-Oriented Programming is a programming paradigm that uses "objects" to design applications and computer programs. Objects can be real-world entities or abstract concepts, and they contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). A software design method that models the characteristics of real or abstract objects using software classes and objects. What is Object ? Objects can be real-world entities or abstract concepts, and they contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). As the name object-oriented implies, objects are key to understanding object-oriented technology. Real World Object Real-world objects share two characteristics: ○ State (what the objects have) ○ Behavior (what the objects do) PROPERTIES BEHAVIOUR Name Barking Color Eating Breed Playing Age Sleeping Real World Object PROPERTIES BEHAVIOUR Current speed Pedalling Gear Shifting gear Brake status Turning light on/off Color Pumping tire Tire pressure Light status Activity Look around and select a few objects in your immediate vicinity. For each selected object, What possible states can this object be in? What possible behaviors can this object perform? Try to cover a variety of objects to appreciate the diversity in complexity and functionality. Advantages of OOP 1. Provides a clear modular structure for programs 2. OOP makes it easy to maintain and modify existing code (new objects can be created with small differences to existing ones) 3. OOP provides a good framework for code libraries 4. Software components can be easily adapted and modified by the programmer. OOP Concepts ❖ Objects ❖ Classes ❖ Encapsulation ❖ Inheritance ❖ Polymorphism ❖ Data Abstraction Software Object Software objects are conceptually similar to real-world objects: They too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages) “An object is a software bundle of variables and related methods.” Class In the real world, you often have many objects of the same kind. For example, your bicycle is just one of many bicycles in the world. Using object-oriented terminology, we say that your bicycle object is an instance of the class of objects known as bicycles. “A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind.” Class Properties: Color,Name Methods: start(),break(),stop() Bicycles have some state (current gear, current cadence, two wheels) and behavior (change gears, brake) in common. However, each bicycle's state is independent of and can be different from other bicycles. Class, Object,State, and Behaviours Encapsulation Is the technique of making the fields in a class private and providing access to the fields via public methods Is also referred to as data hiding is the ability to modify our implemented code without breaking the code of others who use our code and Encapsulation gives maintainability, flexibility and extensibility to our code Inheritance In OOP, we could organize classes in hierarchy to avoid redundancy. We can extend a subclass (or derived class) from a superclass (or base class). The subclass inherits the members of the superclass, known as Inheritance. Polymorphism Generally, Polymorphism refers to the ability to appear in many forms. Abstraction Data abstraction refers to, providing only essential information to the outside world and hiding their background details, Introduction to Java Father of Java James Goseling What is JAVA? Java is a programming language created using C and C++ Is a programming language created by James Gosling from Sun Microsystems in 1991 Is a general-purpose, class-based, object-oriented programming language Is intended to let application developers “Write once, run anywhere” -(WORA) Java is a specification offered by Oracle What is Programming Language? is a standard communication technique for expressing instructions to a computer. Programming languages can be divided into two categories as Lowe level language(Assembly) and high level languages(C,C++,C#, Java, Python, PHP) Software: is a collection of program. Program is the way of telling something to do a computer Words → commands → Programs→ Software Characteristics of JAVA Java has the following properties Platform independent Object-oriented programming language Strongly-typed programming language Interpreted and compiled language Automatic memory management Characteristics of JAVA Java programming language consists Java Compiler: Java Compiler translates Java coding into byte-code Java virtual machine (JVM): Java virtual machine interprets this byte-code and runs the program Java class libraries: Java Class Library is a set of dynamically loadable libraries that Java applications can call at run time Java Virtual Machine (JVM) The Java Virtual Machine (JVM) is a crucial component of the Java Runtime Environment (JRE) that allows Java applications to run on any device or operating system. Java Virtual Machine (JVM) Architecture Specification and Implementations Specification (how something is done) implementation(object/product) Example made a cake(implementation) using cake recipe(specification) In Java ○ Specification: java ○ Implementation: JDK, Platform Independent Java is platform independent We can run class file in any platform(Linux, windows, macOS…) Tutorial 01 How to install Java Tutorial 01 Popular java editions : Notepad, notepad++,eclipse, NetBeans, and et. Tutorial 01 How to download and install java Download and install oracle JDK 7 Set the java path 1. In cmd :set path=C:\Program Files\Java\jdk1.7.0_80\bin 2. Properties → advanced system setting → environment variables → path →edit → new → paste C:\Program Files\Java\jdk1.7.0_80\bin Open editor Write a code and save using “.java” extension javac 1.java java Test Source Code → Byte Code(using compiler) → Run the byte code using JVM Tutorial 01 Example 01 Class Soap{ Public static void main(String[ ] args){ System.out.println(“Your Soap is ready!”); } } Tutorial 01 1. Write the above code using notepad 2. Save the file as “Test.java” 3. Open command prompt 4. Compile the code and create byte code(“Test.class”) a. Javac Test.java → compile the source code b. Java Test → create JVM and run the byte code 5. If these two are not executed well then you have compile time error or run time error Tutorial 01 Example 02 Class Student{ Public static void main(String[ ] args){ System.out.println(“Name: DMS Sathsara”); System.out.println(“Age: 26 years old”); System.out.println(“Degree:ICT”); } } Java Programming with IDE IDE: Integrated Design Environment Consist of ○ Source code editor ○ Compiler and/or interpreter ○ Build automation tools ○ Debugger ○ Construction of aGUI ○ etc. JAVA Naming Conventions Java naming convention is a rule to follow as you decide what to name your identifiers such as class,packages, variable, constant, methods etc. By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does. JAVA Naming Conventions Class Name : should start with uppercase letter and be a noun (ex: String, Color, Button, System, Thread,...) Interface Name: should start with uppercase letter and be an adjective (ex: Runnable, Remote, ActionListner, …) Method name: Should start with lowercase letter and be a verb ( Ex: print(), run(), main() Variable name: should be start with lowercase (Ex: firstName, middleName, …) Package name: should be in lowercase letter (Ex: java, lang, util,...) Constants name: should be in uppercase letter (Ex: RED, MAX, MIN,..) Remember, If name is combined with two words, second word will start with uppercase letter always when naming the class, interface,method and variable. JAVA - Keywords Java keywords are reserved words in Java that have predefined meanings and functions in the language They serve as the building blocks of Java programming syntax. JAVA - Comment Comments are nonexecuting statements Types of comments are: ○ Line Comments //line comment ○ Block Comment ○ Javadoc comments JAVA - Escape Sequence A character preceded by a backslash (\) is an escape sequence Has a special meaning to the compiler JAVA - Identifiers In Java, an identifier is the name given to a variable, method, class, package, or interface. Identifiers are tokens that help in identifying a particular element in the code, allowing programmers to refer to these elements by names. Understanding the rules and conventions for naming identifiers is crucial for writing clear and maintainable code. Rules for Java Identifiers Characters: Identifiers can include any combination of letters (A to Z or a to z), currency characters (such as the dollar sign $ or the Euro symbol €), underscore (_), or digits (0 to 9). However, the first character must not be a digit. Case Sensitivity: Java identifiers are case-sensitive. For example, myVariable, MyVariable, and MYVARIABLE would be considered distinct identifiers. Length: There is no limit to the length of an identifier, but it's advisable to keep it reasonable for the sake of readability. Rules for Java Identifiers Reserved Words: Java keywords cannot be used as identifiers. Keywords are reserved words that have a predefined meaning in the language syntax, such as class, void, public, etc. Conventions: While not enforced by the language syntax, following naming conventions for identifiers is considered best practice (e.g., using camelCase for variables and methods, PascalCase for class names). Identifiers Valid Identifiers Invalid Identifiers myVariable 123abc MAX_HEIGHT -myVar _tempValue class $currency a*b user1 %Percentage i Description 01 threadNumber Set JAVA - Literals In Java, literals represent fixed values that are embedded directly into the code. They are essentially the raw data that can be assigned to variables or used in expressions. Java supports several types of literals, including integer, floating-point, character, string, boolean, and more. Each type serves different purposes and follows specific syntax rules. JAVA - Literals Integer Literals: Represent whole numbers without fractional parts. ○ Example: `int decVal = 100; int hexVal = 0xFF; int octVal = 077; int binVal = 0b1010;` Floating-Point Literals: Represent numbers with fractional parts. ○ Example: `double d1 = 123.4; double d2 = 1.234e2; float f1 = 123.4f;` Character Literals: Represent single characters within single quotes. ○ Example: `char letter = 'A'; char newline = '\n';` JAVA - Literals String Literals: Represent sequences of characters enclosed in double quotes. ○ Example: `String greeting = "Hello, World!";` Boolean Literals: Represent truth values. Example: `boolean isJavaFun = true;` Null Literal: Represents the absence of a value or a non-existent object.Example: `String noValueYet = null;` JAVA - Data Types Primitive Data Types Data Types Meaning Memory size Range Default Value byte Whole numbers 1 byte -128 to +127 0 short Whole numbers 2 bytes -32768 to +32767 0 int Whole numbers 4 bytes -2,147,483,648 to 0 +2,147,483,647 long Whole numbers 8 bytes -9,223,372,036,854,775,808 to 0L +9,223,372,036,854,775,807 float Fractional numbers 4 bytes 0.0f double Fractional numbers 8 bytes 0.0d char Single character 2 bytes \u0000 boolean Unsigned char 1 bit 0(false) Thank you!