Thursday, May 28, 2020
Active Comparison of Java and Python Programming Language - 1925 Words
Active Comparison of Java and Python Programming Language (Other (Not Listed) Sample) Content: COURSEWORK FOR PROGRAMMING PARADIGMSBy (Name)The Name of the Class (Course)Professor (Tutor)The Name of the School (University)The City and State where it is locatedThe DateIntroductionPython is a strong and powerful modern day computer programming language, it is seen to draw close similarities with earlier programming languages such as FORTRAN, however, it is much powerful than this early programming language. Moreover, python is a high-level, interactive, interpreted and object oriented language. Why so? First, its interpreted because it is processed at runtime by a given interpreter without the need of a compiler. Secondly, it is an interactive language that enables the user to interact with the interpreter as he/she writes the code. Thirdly, python is an object-oriented language since it supports object oriented techniques that encapsulate codes into objects (class). Finally, its a beginner-level programming language that can develop a wide range of applications (Tutorial points 2014).Nevertheless, python-like stated before holds several similarities and differences with other programming languages (both old and modern). For instance, its interpreted features are similar to those of PERL and PHP, which are also instances of object-oriented languages. However, this paper will analyze python as a programming language while comparing it with another OOP language, Java. Moreover, their syntax will be highlighted to provide a deeper understanding of the language, more so on inheritance (Doty 2008).Python vs. JavaBasic comparisonsLets start with the basic, a simple syntax of a Hello, World!Ã program that will help illustrate the two programs.Python: print Hello, World!Ã OR print (Hello, World!Ã ) #this is for python 3Java: public class HelloWorld{Public static void main (String [] args){System.out. println (HelloId!Ã );}}From this simple programs its clear to see the main differences between these two languages, for python, it is an ob ject-oriented language, however, not all of its instances are OOP. In fact, it is possible to write a program without any object-oriented techniques or concepts as seen above. On the other hand, Java is completely object-oriented i.e. it can only support OOP techniques as clearly shown above.Python is dynamically typed to produce the required results, a good illustration of this is the introduction of variables. For one to introduce a variable, a specific value must be assigned to it. Example: saidVariable = 100. Furthermore, the value assigned to the variable can be changed later (not fixed). Example: saidVariable = 100, this can change to saidVariable = Hello, World! On the other hand, Java is the exact opposite of python whereby its statically typed. Referring to the same example of a variable, a variable is first declared before assigning a value to it. Moreover, the variable is explicitly declared with the same type as the value its assigned. Example: int saidVariable; int said Variable = 100;. Finally, a variable declared as a certain type cannot be assigned another value that is different from the declared type (Yang 2016).Nevertheless, python is also similar to Java more so when considered from an ideology point of view. For one, they are both strong cross-platform languages that require runtime on a programmers system. Moreover, there is the obvious OOP implementation as well as collections similarities (sects, lists, and dicts). However, like seen above and in other many instances their syntax is significantly different but, when analyzed the python syntax and its features such as those of lists comprehensions are similar to those of Java.Inheritance in both Python and JavaInheritance in this scenario represents the ability of a class or even object to being defined as an extension of a different class or object. In python, all classes created technically use inheritance in that all classes are in fact subclasses of special classes called object(s). I n essence, when not explicitly defined (inheritance) a class in a python program will automatically inherit from the object defined (Voegele 2016). However, as seen in the syntax below one can state this notation openly in the defining code.Example: class MySubClass(object):PassThis simple description is inheritance at its core, a super class or otherwise known as parent class is the original class that another inherits. As stated before, in python if another superclass is not defined, a class automatically inherits from the object defined. A subclass, on the other hand, is the class that inherits from the parent class.Syntax example: from the basic class DerivedClass(ParentClass):body_of_the_derived_classclass polygon:def __init__(self, number_of_sides):self.n = number_of_sidesself.sides = [0 for i in range(number_of_sides)]def inputSides(self):self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]def dispSides(self):for i in range(self.n):print("Side",i+1 ,"is",self.sides[i])In this example, different attributes are provided that hold the data to store the number of sides (n). Moreover, the magnitudes of each side are given as a list (sides). From this base class, we can define another class e.g. triangle that can inherit from this base class.class Triangle(Polygon):def __init__(self):Polygon.__init__(self,3)def findArea(self):a, b, c = self.sidesAs shown above, the class triangle inherits the attributes of the parent class polygon i.e. n for the number of sides etc. In addition to this, we can also define another method that eventually calculates the area of the said triangle. Therefore, despite us not defining this method in the original base class, the final program will execute the code, giving the correct value. Python is able to search for unknown attributes or methods within a given class recursively until a match is found. This is the basis of multiple inheritances, a feature seen in python and not in Java as shown below.Howe ver, regardless of the availability of multiple inheritance i.e. multiple parent classes (and lack of) minimal instances of this feature are recommended as an over use could lead to inheritance problems such as the diamond problem. Diamond problem occurs when multiple inheritances is structured poorly and when a class inherits the wrong method or attribute of a different class. Consider the example below:class Sport:def athlete(self):passclass Football(Sport):def athlete(self):print("Ronaldo")class Basketball(Sport):def athlete(self):print("Lebron")class Champions(Football, Basketball):passg = Champions()g.athlete()Because python uses a resolution order method to find an attribute for a given situation, reversing Basketball with Football (class Champions(Basketball, Football)) would see the output of the first class as Lebron instead of Ronaldo. To solve such problems python uses implicit behaviour to structure its code unlike Java that completely disallows multiple inheritance (Hug unin 1997).Java inheritanceInheritance in java is similar to python, however, each new classes (inherited classes) are created from existing classes i.e. the superclass. Therefore, unlike python a parent class must exist for an inheritance to occur. Furthermore, one can reuse all the methods used in a given parent class as well as its fields. New methods and fields can be added to the existing system however, multiple inheritance features are completely absent.General syntax: class Subclass-name extends Superclass-name{//methods and the fields} //note the superclass must be explicitly defined.Extends is a keyword that is used to indicate that a new class is being derived from an existing superclass. Lets highlight a sample code that utilizes inheritance and analyze its features. Below a simple Java program having two classes; calculator and my_calculator.class Calculator{int z;public void addition(int x, int y){z=a+b;System.out.println("The sum of the given numbers:"+z);}public voi d Substraction(int x, int y){z=a-b;System.out.println("The difference between the given numbers:"+z);}}public class My_Calculator extends Calculator{public void multiplication(int x, int y){z=a*b;System.out.println("The product of the given numbers:"+z);}public static void main(String args[]){int x=20, y=10;My_Calculator demo = new My_Calculator();demo.addition(x, y);demo.Substraction(x, y);demo.multiplication(x, y);}}This program will output the sum, difference, and product of the two numbers provided. However, a deeper analysis of the code indicates the following: One, inherited classes have only one parent class. This observation is not a coincidence to this particular program but is the rule of Java inheritance, whereby a cla...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.