Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object Oriented Programming, Thesis of Java Programming

It hold the First unit to the course with examples

Typology: Thesis

2018/2019

Uploaded on 01/25/2019

Alazar_TIlahun
Alazar_TIlahun 🇪🇹

2 documents

Partial preview of the text

Download Object Oriented Programming and more Thesis Java Programming in PDF only on Docsity! 1 Chapter One: - Introduction Programming paradigm overview A programming paradigm is a philosophy, style, or general approach to writing code. Most definitions of the term are so broad as to be fairly useless—the term tends to make more sense when discussing specific paradigms. Here, we’ll be comparing three specific paradigms: Imperative, Structured, Declarative, Functional, Logical, Event-driven and Object-oriented. The main programming paradigms; a) Imperative Programming If you’ve done programming in Python or C, you’ve used imperative programming. Imperative programming defines the solution to a problem as a series of steps—first do this, then do that, then do the next thing, and so on. The computer steps through each line of code, executing it and moving on to the next step. Programs written in the imperative style often resemble recipes—first crack the eggs, then mix in the flour, then add water. Imperative programs often change the state of the program on each line, assigning new variables and referring to or changing old ones. Though intuitive for solving small problems, imperative programs quickly become unmanageable as they become larger. i. Control flow in imperative programming is explicit: ii. Commands show how the computation takes place, step by step. iii. Each step affects the global state of the computation. iv. First do this and next do that v. Sample code 2 b) Structured Programming is (sometimes known as modular programming) a kind of imperative programming where the control flow is defined by nested loops, conditionals, and subroutines, rather than via gotos. It enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. Certain languages such as Ada, Pascal, and dBASE are designed with features that encourage or enforce a logical program structure. Program flow follows a simple hierarchical model that employs looping constructs such as "for," "repeat," and "while." Use of the "Go To" statement is discouraged. In this kind of programming variables are generally local to blocks. Example; result = []; for (i = 0; i < length(people); i++) { p = people[i]; if length(p.name)) > 5 { addToList(result, toUpper(p.name)); } } return sort(result); What are the main features of Structural Programming language? 1. Division of Complex problems into small procedures and functions. 2. No presence of GOTO Statement 3. The main statement includes – If-then-else, Call and Case statements. 4. Large set of operators like arithmetic, relational, logical, bit manipulation, shift and part word operators. 5. Inclusion of facilities for implementing entry points and external references in program. What is difference between Structured and Unstructured (Imperative) Programming Language? 1. The main difference between structured and unstructured programming language is that a structured programming language allows a programmer to dividing the whole program into smaller units or modules. But in unstructured programming language, the whole program must be written in single continuous way; there is no stop or broken block. 2. Structured Programming language is a subset of Procedural Programming language. But in unstructured Programming language no subset exists. 5 e) Logic programming Logic programming is a way of writing computer programs using languages that are based on formal logic. Logic is the study of how truth is defined, and how we prove that certain statements are true or false. One way to look at a program is as a series of statements about the world with the goal of determining whether they are true. In that sense, it is an example of declarative programming: instead of instructing the computer exactly how to transform data, you provide the rules that govern the world described by your data. These rules are often given by premises (things assumed to be true) and consequences (things that happen if the premises are true). Logic programming languages (like Prolog) use a notation that replicates many aspects of First-order logic, in which we establish relations between known facts (thus creating a knowledge base) and later write rules that the interpreter or compiler can translate to a plan for execution in the Warren Abstract Machine, a virtual machine that understands and resolves logical statements. Logic programming has a number of advantages and disadvantages, and here are some of them: Advantages: ▪ Logic programming can be used to express knowledge in a way that does not depend on the implementation, making programs more flexible, compressed and understandable. ▪ It enables knowledge to be separated from use, ie the machine architecture can be changed without changing programs or their underlying code. ▪ It can be altered and extended in natural ways to support special forms of knowledge, such as meta-level or higher-order knowledge. ▪ It can be used in non-computational disciplines relying on reasoning and precise means of expression. Disadvantages: ▪ Initially, due to insufficient investment in complimentary technologies, users were poorly served. 6 ▪ In the beginning, poor facilities for supporting arithmetic, types, etc. had a discouraging effect on the programming community. ▪ There is no adequate way of representing computational concepts found in built-in mechanisms of state variables (as is usually found in conventional languages). ▪ Some programmers always have, and always will prefer the overtly operational nature of machine operated programs, since they prefer the active control over the 'moving parts'. Tips ▪ Consists of a set of facts and rules. ▪ A knowledge base is built up about a specific subject and an inference engine uses the knowledge base to answer queries which are presented in the form of a goal, often used for artificial intelligence systems. ▪ Answer a question via search for a solution f) Event-driven programming i. refers to your standard Windows Form idea, the program waits in a loop until an event(e.g. the click of a button, or a keystroke). ii. The program then runs the code associated with this event and then returns to its loop, providing that the code did not instruct it to close. iii. If more than one event occurs, code is queued by the program and run in the order the events were triggered. Example private Sub doSomething(Optional ByVal message As String = "") MessageBox.Show( "The username or password you entered is incorrect","Login Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop) End Sub 7 g) Object Oriented Programming Object-oriented programming deals with state by designating certain functions that operate specifically on that state. Objects in object-oriented programming are a combination of state, or data, with functions that work specifically on that data. Rather than isolate state from the rest of the program, the object-oriented approach allows only certain parts of the program to operate on certain pieces of data. i. OOP is based on the sending of messages to objects. ii. Objects respond to messages by performing operations. iii. Messages can have arguments, so "sending messages" looks a lot like calling subroutines. iv. “Send messages between objects to simulate the temporal evolution of a set of real-world phenomena” Example result = [] for p in people { if p.name.length > 5 { result.add(p.name.toUpper); } } return result.sort; Overview of OO principles Object-Oriented Programming (OOP) is the term used to describe a programming approach based on objects and classes. The object-oriented paradigm allows us to organize software as a collection of objects that consist of both data and behavior. This is in contrast to conventional functional programming practice that only loosely connects data and behavior. Why has object-oriented programming become the preferred approach for most software projects? i. OOP offers a new and powerful way to cope with complexity. ii. Instead of viewing a program as a series of steps to be carried out, it views it as a group of objects that have certain properties and can take certain actions. 10 The public, private, protected, and default access modifiers define the scope of the field in the application. The Java Language Specification (JLS) defines the rules of visibility of data for all fields. These rules define under what circumstances you can access the data in these fields. • Methods Methods are procedures associated with a class. Like a field, a method can be declared as static, in which case it can be called globally. If a method is not declared as static, it means that the method is an instance method and can be called only as an operation on an object, where the object is an instance of the class. Similar to fields, methods can be declared as public, private, protected, or default access. This declaration defines the scope in which the method can be called. Objects The fundamental idea behind object-oriented languages is to combine into a single unit both data and the functions/methods that operate on that data. Such a unit is called an object, it is an instance of a class, and it is just a thing, an item, or a noun. For example, a car, a house, an employee of a company, a window that opens on the computer screen, or a TCP/IP socket connection between two computers. An object consists of two major components: i. The attributes of an object are what the object consists of, and ii. The behaviors of the object are what the object does I. Inheritance Inheritance is a fundamental principle of object-oriented programming. It allows a class to "inherit" (behavior or characteristics) of another, more general class. For example, a lion belongs to the biological family of cats (Felidae). All cats that have four paws, are predators and hunt their prey. This functionality can be coded once in the Felidae class and all its predators can reuse it – Tiger, Puma, Bobcat, etc. Inheritance is described as is-kind-of relationship, e.g. Tiger is kind of Animal. Object-oriented programming allows you to define new classes from existing classes. This is called inheritance. The principle in this sort of division is that each subclass shares common characteristics with 11 the class from which it’s derived. E.g. Cars, trucks, buses, and motorcycles all have wheels and a motor; these are the defining characteristics of vehicles. In addition to the characteristics shared with other members of the class, each subclass also has its own particular characteristics: For instance: Buses have seats for many people, while trucks have space for hauling heavy loads. An example of inheritance hierarchy among different types of students. II. Encapsulation Encapsulation is the idea that the attributes of an entity are enclosed in that entity. This gives context to attributes. This also allows the programmer to restrict access to those attributes so that those attributes are modified and/or used only in ways that the programmer intends to use them. In Java, the basis of encapsulation is the class. When you create a class, you will specify the code and data that constitute that class. Collectively, these elements are called members of the class. Specifically, the data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods. Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding the complexity of the implementation inside the class. Each method or variable in a class may be marked private or public. • The public interface of a class represents everything that external users of the class need to know, or may know. 12 • The private methods and data can only be accessed by code that is a member of the class. Therefore, any other code that is not a member of the class cannot access a private method or variable. Abstract Data Types Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of value and a set of operations. The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. It is called “abstract” because it gives an implementation independent view. The process of providing only the essentials and hiding the details is known as abstraction. The user of data type need not know that data type is implemented, for example, we have been using int, float, char data types only with the knowledge with values that can take and operations that can be performed on them without any idea of how these types are implemented. So, a user only needs to know what a data type can do but not how it will do it. We can think of ADT as a black box which hides the inner structure and design of the data type. Thus, the terms information hiding, ADT, and encapsulation all involve the same general idea. 15 Bytecode and JVM A Java compiler translates the Java source program into what is known as bytecode, and it stores the generated bytecode in a file with the extension .class. The bytecode consists of the instructions for a “pseudo CPU.” (a CPU that does not exist in reality) The JVM provides the runtime environment for a Java executable (bytecode). It also provides a bytecode interpreter and a verifier that confirms the bytecode’s validity before translating and running it on a real CPU. A JVM is essentially a machine (as its name suggests) that is capable of running a Java executable. It subjects the loaded classes to verification to ensure that they do not contain any undefined instructions for the pseudo CPU. If the bytecode of your application program contains an invalid instruction, the JVM rejects its execution and unloads it from memory. Figure describes Java Component Structure Having a technical definition for the JVM is useful, and there's also an everyday way that software developers think about it. Let's break those down: • Technical definition: The JVM is the specification for a software program that executes code and provides the runtime environment for that code. • Everyday definition: The JVM is how we run our Java programs. We configure the JVM's settings and then rely on it to manage program resources during execution. When developers talk about the JVM, we usually mean the process running on a machine, especially a server, that represents and controls resource usage for a Java app. Contrast this to the JVM specification, which describes the requirements for building a program that performs these tasks. 16 JVM Architecture • Class Loader: Class loader loads the Class for execution. • Method area: Stores pre-class structure as constant pool. • Heap: Heap is in which objects are allocated. • Stack: Local variables and partial results are store here. Each thread has a private JVM stack created when the thread is created. • Program register: Program register holds the address of JVM instruction currently being executed. • Native method stack: It contains all native used in application. • Executive Engine: Execution engine controls the execute of instructions contained in the methods of the classes. • Native Method Interface: Native method interface gives an interface between java code and native code during execution. • Native Method Libraries: Native Libraries consist of files required for the execution of native code. 17 Difference between JDK and JRE Java Runtime Environment (JRE): The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications. Java Development Kit (JDK) The JDK also called Java Development Kit is a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications. 20 Compiling, and Executing programs. i. Java source code is translated into bytecode. ii. Java bytecode can be executed on any computer with a Java Virtual Machine. 21 Sample Screen Output Hello out there. I will add two numbers for you. Enter two whole numbers on a line: 12 30 The sum of those two numbers is 42 Exercise 1. Define the terms class, object, inheritance, encapsulation and polymorphism 2. What is the Java source filename extension, and what is the Java bytecode filename extension? 3. What are the input and output of a Java compiler? 4. What is the command to compile a Java program? 5. What is the command to run a Java program? 6. What is the JVM? 7. Can Java run on any machine? What is needed to run Java on a computer? 8. If a NoClassDefFoundError occurs when you run a program, what is the cause of the error? 9. If a NoSuchMethodError occurs when you run a program, what is the cause of the error?