Sunny Tuesday, May 26, 2020 COMPUTER PROGRAMMING METHODOLOGIES 1. Sequential Programming lines of code are written and executed one after the other “First do this, then do this, then do this, etc....” The disadvantage to this method is that you sometimes have to repeat the same code over and over again. 2. Procedural Programming In procedural programming lines of code are grouped together into a bundle to perform a specific task Java refers to the code as functions Other programming languages refer to the code as procedures When you want to perform that task, you simply direct the program to run the code in the function or procedure When a program runs a function (procedure) we refer to this as calling the function (procedure) Writing code in functions (procedures) is more efficient than sequential programming Once a function is written and then just call the function when you need it 3. Object-Oriented Programming (OOP) you create separate files called classes that defines the: o data type of a data structure and o types of operations or functions that can be applied to the data structure classes interact together to create a working program. In procedural programming, the basic building block is the function, but in OOP, the building block is the class (data and function). Your program creates an object from one of the classes. Custom classes can be written from scratch to produce specialized types of content such as a car in a racing program. Java also provides pre-made classes (native classes) that are used to manipulate basic types of information, such as numbers and text. Objects To understand the concept of objects, it often helps to think of a real-world object such as a dog. A dog could be said to have properties such as name, colour, breed, and age as well as behaviours such as barking, eating, and running. OOP objects also have properties and behaviours. Using object-oriented techniques, you can model a realworld object. Sunny Tuesday, May 26, 2020 Instances Continuing with the real-world analogy of a dog, consider that there are dogs with different names, colours, breeds, and ages and with different ways of eating and behaving. However, regardless of their individual differences, all dogs are members of a general type or class - the class of dogs. A class is a definition of a type, while an object is an instance of a class. In other words, the instance is the actual object that can be manipulated. The difference between classes and objects is critical. You cannot pet the class (the concept dog) but you can pet the object (the instance) of a dog. The act of creating an object is called instantiation. This means that they create an object of a given type as defined by the class definition. In OOP, a class defines a blueprint for a type of object. The characteristics and behaviours that belong to a class are jointly referred to as members of that class. The characteristics (in the dog example, the name, age, breed, and colour) are called properties of the class and are represented as variables; the behaviours (barking, eating, and running) are called methods of the class, and are represented as functions. Inheritance One of the primary benefits of OOP is that you can extend a class to create a subclass. Writing subclasses lets you reuse code. Instead of recreating all the code common to both classes, you can simply extend an existing class. When you extend a class, the subclass inherits all the properties and methods of the original class. The subclass usually defines additional methods and properties or overrides methods or properties defined in the superclass. For example, you could build a superclass called Animal, which contains common characteristics and behaviours of all animals. Next, you could build several subclasses that inherit from the Animal superclass and add characteristics and behaviours specific to that type of animal. Polymorphism The word ‘poly' means many and the word ‘morph' means form. The idea behind polymorphism is that an object can take many forms. Using polymorphism, classes can override methods of their superclasses and define specialized implementations of those methods. For example, you might start with a class called Mammal that has play() and sleep() methods. You then create Monkey and Dog subclasses to extend the Mammal class. The subclasses override the play() method from the Mammal class to reflect the habits of those particular kinds of animals. Monkey implements the play() method to swing from trees and Dog implements the play() method to fetch a ball. Because the sleep() functionality is similar among the animals, you would use the superclass implementation. Sunny Tuesday, May 26, 2020 Native Classes The following table lists some of Java's native classes. String textual data (e.g., a string of characters) Boolean logical states (e.g., true and false) double floating-point numbers (e.g., numbers with a decimal value) int integer numbers (e.g., no decimal value) Array an ordered list Error program errors (e.g., a problem in your code) Date a specific point in time Math common mathematical values and operations RegEx defines tools for searching and replacing text Function represents a reusable set of instructions that can be called. Object defines the basic features of every object in Java Example The Math class is an example of a pre-made class in Java. The Math class groups together math functions and constants. You can use the Math class to generate random numbers (Math.random), round numbers (Math.round), or to access constants like pi (Math.PI). Advantages of OOP a program is no longer in one big file. classes are separate files that can be worked on by different people at the same time classes are also generally reusable the more you program, the more classes you have to work with. When you start a new project, you are never starting from scratch, because you have a library of classes upon which to draw Sunny Tuesday, May 26, 2020 Assignment: A. Open the document from today entitled, “Getting To Know You” and post the completed version in the DropBox on D2L B. Answer the following questions in the Discussion Forum on D2L 1. Explain the benefits of object-oriented programming. Improved software-development productivity: Object-oriented programming is modular, as it provides separation of duties in object-based program development. Improved software maintainability: Since the design is modular, part of the system can be updated in case of issues without a need to make largescale changes. Faster development: Object-oriented programming languages come with rich libraries of objects, and code developed during projects is also reusable in future projects. 2. Discuss the difference between a class and an object. A class is a blueprint from which you can create the instance, i.e., objects, whereas an object is the instance of the class, which helps programmers to use variables and methods from inside the class. A class is used to bind data as well as methods together as a single unit, whereas an object acts as a variable of the class. The class must be declared only once, whereas objects can be declared several times depending on the requirement. Classes have logical existence, whereas Objects have a physical existence. 3. Discuss the difference between properties and methods of a class. The characteristics (in the dog example, the name, age, breed, and colour) are called properties of the class and are represented as variables; The behaviours (barking, eating, and running) are called methods of the class, and are represented as functions. 4. What is code reuse? How does it relate to inheritance? Sunny Tuesday, May 26, 2020 Code reusability defines the methodology you can use to use similar code, without having to re-write it everywhere. In inheritance, writing subclasses lets you reuse code. Instead of recreating all the code common to both classes, you can simply extend an existing class. When you extend a class, the subclass inherits all the properties and methods of the original class. The subclass usually defines additional methods and properties, or overrides methods or properties defined in the superclass. 5. What is polymorphism? How does it relate to methods? Polymorphism is the ability of an object to take on many forms. Using polymorphism, classes can override methods of their super classes and define specialized implementations of those methods.