Programming Paradigm
A programming paradigm is a model of programming based on distinct concepts that shapes the way programmers design, organize and write programs.
Some types of programming paradigm :
- Procedural programming
- Object oriented programming
Procedural programming can be defined as a subtype of imperative programming as a programming paradigm based upon the concept of procedure calls, in which statements are structured into procedures (also known as subroutines or functions). Procedure calls are modular and are bound by scope. A procedural program is composed of one or more modules. Each module is composed of one or more subprograms.
Modules may consist of procedures, functions, subroutines or methods, depending on the programming language. Procedural programs may possibly have multiple levels or scopes, with subprograms defined inside other subprograms. Each scope can contain names which cannot be seen in outer scopes.
Procedural programming offers many benefits over simple sequential programming since procedural code :
- Is easier to read and more maintainable
- Is more flexible
- Facilitates the practice of good program design
- Allows modules to be used again in the form of code libraries
Object-oriented programming can be seen as an extension of procedural programming in which programs are made up of collections of individual units called objects that have a distinct purpose and function with limited or no dependencies on implementation. For example, a car is like an object; it gets you from point A to point B with no need to know what type of engine the car uses or how the engine works.
Object-oriented languages usually provide a means of documenting what an object can and cannot do, like instructions for driving a car.
Object Oriented Programming also an object is composed of members and methods. The members (also called data members, characteristics, attributes, or properties) describe the object. The methods generally describe the actions associated with a particular object.
Think of an object as a noun, its members as adjectives describing that noun, and its methods as the verbs that can be performed by or on that noun.
Object Oriented Programming also a "blueprint" that lets us build our sports car object is called a class. A class does not tell us how fast our sports car goes, or what color it is, but it does tell us that our sports car will have a member representing speed and color, and that they will be say, a number and a word, respectively. The class also lays out the methods for us, telling the car how to park and drive, but these methods can not take any action with just the blueprint - they need an object to have an effect.
There are two approaches :
- Early object : focus on object-oriented programming concepts
- Late object : focus on procedural programming concepts
Java Programming Language
Java programs are distributed as instructions for a virtual machine, making them platform-independent. Java has a very large library. Focus on learning those parts of the library that you need for your programming projects. To program in Java, you need to install Java Development Kit (JDK).
Editor Application of Basic Programming
The line public class Main indicates the declaration of a class called Main. Every Java program consists of one or more classes. Classes are the fundamental building blocks of Java programs.
The word public denotes that the class is usable by the “public”. You will later encounter private features. In Java, every source file can contain at most one public class, and the name of the public class must match the name of the file containing the class. For example, the class Main must be contained in a file named Main.java.
The construction public static void main(String[] args){} declares a method called main. A method contains a collection of programming instructions that describe how to carry out a particular task. Every Java application must have a main method. Most Java programs contain other methods besides main
Sometimes a class defines methods that are not invoked on an object. Such a method is called a static method. However, in object-oriented programming, static methods are not very common.
The word public denotes that the class is usable by the “public”. You will later encounter private features. In Java, every source file can contain at most one public class, and the name of the public class must match the name of the file containing the class. For example, the class Main must be contained in a file named Main.java.
The construction public static void main(String[] args){} declares a method called main. A method contains a collection of programming instructions that describe how to carry out a particular task. Every Java application must have a main method. Most Java programs contain other methods besides main
Sometimes a class defines methods that are not invoked on an object. Such a method is called a static method. However, in object-oriented programming, static methods are not very common.
Variables Declaration
Every program uses variables. A variable must be declared to be used in computer program. We need to declare the type and the name of variable.
Java syntax to declare a variable:
Java syntax to declare a variable:
Naming Convention for Variable
- Start with a lowercase letter
- All internal words start with an uppercase letter
- Give a meaningful name for a variable, but keep it shorts
- One-character variable’s name should be avoided
- except for a temporary variable (e.g. counter)
Rule for Variable Names
- Start with a letter or the underscore (_) character, and the remaining characters must be letters, numbers, or underscores.
- Cannot use these symbols: ? Or %
- Spaces are not permitted inside names.
- Case sensitive, be careful. "A" and "a" is different through some programming language its same thing and same value
- Cannot use reserved words
Examples
- int overtime;
- double basicSalary;
- double overtimeSalary;
Declare all the variables which are needed to solve these problems :
- Womboy runs in constant speed for x hours. If his speed is y km/hours, calculate the distance that he travelled!
- Excluding stoppages, the speed of a bus is 54 kmph and including stoppages, it is 45 kmph. For how many minutes does the bus stop per hour?
Assignment Operator
Assignment statements can be recognized by the presence of an assignment operator “=“. Assignment statements store the value represented by the right-hand side of the statement in the variable named on the left.
- double total = 0;
- total = 100;
Is it possible that the left-hand side's type of the assignment statement different from the right-hand side's ?
- int x = 10.0; (error)
- float a = 10;
- float b = 10.0;
- double c = 10.0f
A widening primitive conversion does not lose information about the overall magnitude of a numeric value.
Arithmetic Operator
The arithmetic operator works with integer and floating point type. Unary operator : takes one operand
- + (positive value) : +b
- – (negates an expression): –b, –3
Binary operator : takes two operands
- + (addition) : c = 1 + 2 , b = 3 + c
- – (substraction) : d = b – c + 2
- * (multiplication) : e = d * b + c
- / (division) : f = 10 / d , g = 10.0 / (d * 1.0)
- % (remainder) : h = 10 % 3
Increment (++) and decrement operator (– –)
- num++ is the same as num = num + 1
- num– – is the same as num = num – 1
Arithmetic Expression
The combination of variables, literals, operators, and/or method calls is called an expression. For example, (a + b) / 2 is an arithmetic expression. Parentheses are used just as in algebra: to indicate in which order the parts of the expression should be computed.
Variable Initialization
Initialization: giving a value to a variable
Example :
- int overtime = 100;
- double basicSalary = 24891.6;
Write a program to solve each problem below:
- Womboy runs in constant speed for 10 hours. If his speed is 80 km/hours, calculate the distance that he travel in kms!
- Excluding stoppages, the speed of a bus is 54 kmph and including stoppages, it is 45 kmph. For how many minutes does the bus stop per hour?
- Three families share a garden. They usually clean the garden together at the end of each week, but last week, family C was on holiday, so family A spent 5 hours, family B spent 4 hours and had everything done. After coming back, family C is willing to pay $90 to the other two families. How much should family A get? You may assume both families were cleaning at the same speed
Posting Komentar