Dasar-Dasar Pemrograman : Introduction to Programming

Dasar-Dasar Pemrograman : Introduction to Programming



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



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 : 
  1. Is easier to read and more maintainable 
  2. Is more flexible 
  3. Facilitates the practice of good program design 
  4. Allows modules to be used again in the form of code libraries

Object Oriented Programming



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.

Programming in Java



There are two approaches : 
  1. Early object : focus on object-oriented programming concepts 
  2. 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).


From Source Code To Running Program




Editor Application of Basic Programming




Source Code



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.


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:



Naming Convention for Variable





  1. Start with a lowercase letter 
  2. All internal words start with an uppercase letter 
  3. Give a meaningful name for a variable, but keep it shorts 
  4. One-character variable’s name should be avoided 
    • except for a temporary variable (e.g. counter)

Rule for Variable Names




  1. Start with a letter or the underscore (_) character, and the remaining characters must be letters, numbers, or underscores. 
  2. Cannot use these symbols: ? Or % 
  3. Spaces are not permitted inside names. 
  4. Case sensitive, be careful. "A" and "a" is different through some programming language its same thing and same value
  5. Cannot use reserved words


Primitive Data Types



Examples

  • int overtime; 
  • double basicSalary; 
  • double overtimeSalary; 

Exercise 1



Declare all the variables which are needed to solve these problems : 

  1. Womboy runs in constant speed for x hours. If his speed is y km/hours, calculate the distance that he travelled! 
  2. 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.

Syntax : 



Example : 

  • 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.


Example of Arithmetic Expression




Variable Initialization

Initialization: giving a value to a variable

Example : 

  • int overtime = 100; 
  • double basicSalary = 24891.6;

Exercise 2


Write a program to solve each problem below: 


  1. Womboy runs in constant speed for 10 hours. If his speed is 80 km/hours, calculate the distance that he travel in kms! 
  2. 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? 
  3. 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






Post a Comment

Lebih baru Lebih lama