













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














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.
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
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?
where length(name) > 5 order by name d) Functional Programming Functional programs deal with state by avoiding it as much as possible. Solutions are defined as a series of functions that pass values to one another, leading to a series of transformations. Parts of the program dealing with state, if any, tend to be isolated. A goal of functional programming is predictability, based primarily on the fact that functions, if given a certain input, should always return the same output. Tips x. Control flow is expressed by combining function calls, rather than by assigning values to variables. xi. The real power of this paradigm comes from passing functions to functions (and returning functions from functions). xii. With functional programming; (1) Code is much shorter, less error-prone, and much easier to prove correct (2) There is more inherent parallelism, so good compilers can produce faster code (3) Evaluate an expression and use the resulting value for something (4) Example let( f, fun( people, if(equals(people, emptylist), emptylist, if(greater(length(name(head(people))), 5), append(to_upper(name(head(people))), f(tail(people))), f(tail(people))))), sort(f(people)))
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.
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;
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.
iii. It results in programs that are clearer, more reliable, and more easily maintained. Problems with Structured Programming No matter how well the structured programming approach is implemented, large programs become excessively complex. i. First, functions have unrestricted access to global data. I, e. Global data can be accessed by any function in the program. ii. Second, unrelated functions and data. The basis of the procedural paradigm, provide a poor model of the real world. In a large program, there are many functions and many global data items. This leads to an even larger number of potential connections between functions and data, as shown in the following figure This large number of connections causes problems in several ways. i. It makes a program’s structure difficult to conceptualize. ii. It makes the program difficult to modify. A change made in a global data item may necessitate rewriting all the functions that access that item. The more important problem with the procedural paradigm is that its arrangement of separate data and functions does a poor job of modeling things in the real world. In the physical world we deal with objects such as people and cars. Such objects aren’t like data and they aren’t like functions. Complex real-world objects have both attributes and behavior. Examples of attributes (sometimes called characteristics) are, for people, eye color and job title; and, for cars, horsepower and number of doors.
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.
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.
III. Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation. More generally, the concept of polymorphism is often expressed by the phrase “one interface, multiple methods.” It is the compiler’s job to select the specific action (that is, method) as it applies to each situation.
Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1. [J2SE]). The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications. The new J2 versions were renamed as Java SE, Java EE, and Java ME respectively. Java is guaranteed to be Write Once, Run Anywhere. Java is a full-fledged and powerful language that can be used in many ways. It comes in three editions: i. Java Standard Edition (Java SE) to develop client-side applications. The applications can run standalone or as applets running from a Web browser. ii. Java Enterprise Edition (Java EE) to develop server-side applications, such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF). iii. Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell phones. Java is −
JVM Architecture
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.
Generally, Java source code is compiled into Java bytecode and Java bytecode is interpreted by the JVM. Your Java code may use the code in the Java library. The JVM executes your code along with the code in the library. To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a JVM, which is an interpreter. It translates the individual instructions in the bytecode into the target machine language code one at a time rather than the whole program as a single unit. Each step is executed immediately after it is translated. The following command runs the bytecode for the above java code java Welcome Caution
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.