Getting Started
Everyone uses computers, mobile phones and many other gadgets in day to day life to access various applications. Different business domains like Education, Banking, E-Commerce, Healthcare, Insurance etc. use computer applications. These applications are built using programming. The knowledge of programming is essential to bring innovation. You need to know how to code to create value with your ideas.
What is Programming?
Now that we understand why programming, let’s see what is programming all about.
Let us assume that a customer wants to order food using an application. In order to do that, the customer can select the food items from the menu and place an order. If the order is placed successfully, the food gets delivered to the customer.
To develop such applications, we give instructions to the computing devices (computers, mobile phones, etc.). A set of such instructions is known as a program and the act of creating a program is known as programming. Programs are created to solve the problems, to automate the processes and to reduce repetitive and/or manual work.
To write these programs, you need a programming language. There are many programming languages; each one provides various features. Based on the requirements of your programs and features of the languages, you can choose appropriate language for writing your programs.
In this course, you will learn programming using Java language which is one of the popular programming languages
Representation of Algorithms
The first step to write a program is to create an algorithm. An algorithm is a process or set of rules to be followed by the computer while solving a problem. Algorithm should be represented into a form which others can easily understand. There are primarily two ways of representing an algorithm:
- Pseudo-code: represents the algorithm in a way that is in between a programming language and English statements
- Flowchart: represents the algorithm in a diagrammatic way
A pseudo-code looks as shown below.
Note: Here, the assumptions are:
- The customer buys only 1 food item at a time.
- The price of 1 unit of any food item is $10.
Like assignment operator, there are other operators also which can be used to perform various operations.
Arithmetic operators: Used for performing arithmetic operations
Relational operators: Also known as comparison operators, are used to compare values. Result of a relational operation is always either true or false.
Logical operators: are used to combine one or more relational operations.
If A and B are two relational expressions, say A = (Num1>2000), B= (Num2>100), the result of combining A and B using logical operator is based on the result of A and B as shown below:
Now, let’s have a look at flowchart. Flowchart uses graphical symbols to depict steps. The most commonly used symbols in a flowchart are as follows:
Introduction to Control Structures
In the pseudo-code given before, all the statements execute sequentially but this is not necessary in every case. There could be cases where some of the statements in a pseudo-code need not be executed, or the cases where a set of statements have to be executed repeatedly. This is achieved through control structures.
The statements which change the flow or order of execution of the instructions are called control structures.
There are three types of control structures:
- Sequential
- Selection
- Iteration
Sequential control structures are already discussed before. You will now see selection control structures, which are also known as decision control structures.
Selection/Decision Control Structure
Selection or decision making is comparable to how we make decisions in real life.
Let us assume that the man wants to travel to Washington from Los Angeles. He can choose to travel by train or flight. Now how will he make a decision? Which mode of transportation will he choose? Flight takes around 5 to 7 hours depending on direct flight or with hops, whereas train takes 2.5 days with hops to reach the place. Assume that he wants to reach the place within 8 hours and he is ready to spend around $450 for transportation. Which path will he choose? Will he go to airport or to railway station?
Similarly, in case of “SwiftFood”, the restaurant classifies the customers who place orders very frequently as “Regular Customers” and others as “Guests”. The “Regular Customers” are provided with 5% discount for their orders whereas “Guests” need to pay an additional delivery charge.
Such a decision-making process can be represented in a pseudo-code and/or flow chart using a selection control structure.
Selection control structures are implemented using if-else blocks. The if-else block in pseudo-code is written as:
if (condition) then
statements
else
statements
end-if
The pseudo-code for the scenario is shown below:
Input Food_Item, Quantity, Customer_Type
Unit_Price = 10
Total_Cost = Unit_Price * Quantity
if (Customer_Type == "Regular") then
Total_Cost = Total_Cost - (Total_Cost * 5 / 100)
Display "You are a regular customer and eligible for 5% discount"
else
Total_Cost = Total_Cost + 5
Display "You need to pay an additional delivery charge of $5"
end-if
Display "Order successfully placed for ", Food_Item
Display "Total cost is: ", Total_Cost
The flowchart is shown below:
Iteration Control Structures
Let’s have a look at Iteration Control Structure.
Let us consider another scenario where a customer wants to place an order for multiple food items and the customer has not decided the number of food items in the beginning. If the customer wants to add another food item, the entire process of accepting the food item, quantity and calculation of the total amount is repeated.
In this case, a set of statements need to be repeatedly executed. When you want to repeatedly execute a set of statements as long as a condition is met, you can use iteration control structures which are also known as Loops. One such iteration control structure is while loop. The while loop in pseudo-code is written as follows. It repeats the statements written inside while and end-while as long as the condition is true.
while (condition) do
statements
end-while
The pseudo-code for the given scenario can be represented as shown below. Here “==” is a comparison operator to check for equality.
Total_Cost = 0
Want_To_Add_Food_Item = "Yes"
while ( Want_To_Add_Food_Item == "Yes" ) do
Input Food_Item, Quantity
Total_Cost = Total_Cost + (Quantity * 10)
Display "Order placed successfully for ", Food_Item
Display "Total Cost so far: $", Total_Cost
Display "Do you want to add one more food item to the order?"
Input Want_To_Add_Food_Item
end-while
Display "Order placed successfully for all the food items"
Display "Total Cost: $", Total_Cost
The flowchart can be represented as shown below.
While loop is suitable when the number of iterations is not known in the beginning and the loop has to be executed as long as the given condition is satisfied. Sometimes, a set of statements have to be executed for a specific known number of times. In such cases, you can use for loop. The for loop in pseudo-code is written as:
for (initialization, condition, increment_or_decrement_step) do
statements
end-for
The steps of for loop execution are as follows:
- Execute “initialization”. This is executed only once, at the beginning of the for loop.
- Check the “condition”.
- If the condition is false, stop the execution of for loop.
- If the condition is true, execute the “statements” written inside for and end-for.
- Execute “increment_or_decrement_step”.
- Repeat from step 2.
Let us consider that the customer has already decided that he/she would be ordering 10 food items. The entire process of accepting the food item, quantity and calculation of the total amount has to be repeated for the number of food items to be ordered, in this case, 10.
The pseudo-code for this scenario is shown below.
Total_Cost = 0
Unit_Price = 10
Display "Enter the number of food items to be ordered"
Input No_Food_Items_To_Be_Ordered
for(Food_Item_No = 1, Food_Item_No <= No_Food_Items_To_Be_Ordered, Food_Item_No = Food_Item_No + 1) do
Display "Enter the food item"
Input Food_Item
Display "Enter the quantity"
Input Quantity
Total_Cost = Total_Cost + (Unit_Price * Quantity);
Display "Order placed successfully for: ", Food_Item
Display "Total Cost so far: $", Total_Cost
end-for
Display "Order placed successfully for all the food items"
Display "Total Cost: $", Total_Cost
The flowchart is shown below.
What is Java?
By now, you have understood how to represent algorithms in the form of pseudo-code and flowcharts to solve the given problem. But computers cannot understand and execute pseudo-code. To solve the given problem by computer, you need to write a program.
A program is a collection of instructions that performs a specific task when executed by a computer. A program must be written in a programming language. In this course, you will learn to write programs using Java as a programming language.
Java is one of the most popular programming languages. Some statistics about Java are given below:
- Java has been evolving since 1991 with different editions
- According to the TIOBE Programming Community Index, it has been one of the top 5 programming languages for the past several years
- It is being used by more than 10 million developers worldwide
- More than 15 billion devices are powered by Java technology
- More than 125 million Java-based TV devices have been deployed
- 97% of enterprise desktops run Java
The features and strengths of Java have made it suitable for applications throughout the history of Internet, and it continues to do so even for modern applications.
Have a glance at what is going on in the Java world.
Java Environment
Amazing! The program displays the output on clicking Execute button. But wondering how things work behind the scene?
We write programs in programming languages like Java which are called as High-level programming languages. But computers cannot understand high-level languages, they understand only binary language, i.e., 0’s and 1’s. This is the reason that every program that we write needs to get converted into binary form for the computer to understand.
This conversion is made possible using system software called compilers and interpreters.
Java Installation
You will now start the journey of learning programming using Java. To start writing programs in Java, you need to first install Java.
Please follow the below steps to check whether Java is installed on your computer.
Open command prompt and check if Java is installed in your computer using the following command
java -version
If Java is installed on your computer, you will get a message about the version installed on the command prompt window as shown below:
Eclipse IDE
After making sure that you have Java installed in your computer, you then need an IDE (Integrated Development Environment) to code Java programs.
An IDE is a software application that is required to write, execute and test programs. There are various IDEs that can be used to write Java programs. In this course, you will use Eclipse IDE.
First Java Program
Now that you have Java and Eclipse, you can start coding programs using Java.
Start your programming journey with a basic Java program:
class Welcome {
public static void main(String[] args) {
System.out.println("Hello World! Welcome to Java Programming!");
}
}
Every programming language has a set of rules to be followed while writing programs. This is called as syntax of the language. The above program is written by following the syntax of Java programming language. The program gets compiled and executed successfully if and only if it is written as per the syntax of the language.