JavaProgrammingForBeginners-CourseBook-1-10
JavaProgrammingForBeginners-CourseBook-1-10
A Programming Beginner will be overwhelmed by this. I remember how I felt when I saw this almost 20 years back.
Stunned.
Why?
There are a number of keywords and concepts - package, public, class, static, void, String[] and a lot more..
What if the programmer makes a typo? Will he be able to fix it?
We believe that there has to be a better way to learn programming.
Why don't we learn programming step by step?
Why should it not be a lot of fun?
Why don't we solve a lot of problems and learn programming as a result?
This is the approach we took to writing this guide and develop our introductory programming courses for Java and
Python.
Do you know? The first 3 hours of our Java Course is available here.
Introduction to Programming with Print-Multiplication-Table
Step 01: First Challenge : The Print-Multiplication-Table (PMT-Challenge)
Learning to program is a lot like learning to ride a bicycle. The first few steps are the most challenging ones.
Once you use this stepwise approach to solve a few problems, it becomes a habit.
In this book, we will introduce you to Java programming by taking on a few simple problems to start off.
Having fun along the way is what we will aim to do.
Are you all geared up, to take on your first programming challenge? Yes? Let's get started then!
Our first programming challenge aims to do, what every kid does in math class: reading out a multiplication table.
The PMT-Challenge
. Compute the multiplication table for 5 , with entries from 1 to 10 .
. Display this table.
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
As part of solving the multiplication table problem, you will be introduced to:
JShell
Statements
Expressions
Variables
Literals
Conditionals
Loops
Methods
Summary
In this step, we:
Stated our first programming challenge, PMT-Challenge
Identified basic Java concepts to learn, to solve this challenge
Step 02: Introducing JShell
JShell is a programming tool, introduced in Java SE 9. JShell is a REPL interface. The term REPL refers to this:
'R' stands for Read; Read the input Java code
'E' means Eval; Evaluate the source code
'P' translates to Print; Print out the result
'L' indicates Loop; Loop around, and wait for the next input
How about starting off exploring Java? Are you game?
Snippet-1: Check the Java programming environment
You can use https://tryjshell.org/ to run the code for the first 25 steps. Or you can Install Java 12+. Here's the
troubleshooting section if you face problems.
Launch up command prompt or Terminal.
Let type in java -version on the terminal and press enter.
in28minutes$>java -version
java version "x.0.1"
Java(TM) SE Runtime Environment (build x.0.1+11)
Java HotSpot(TM) 64-bit Server VM (build x.0.1+11, mixed mode)
in28minutes$>
A successful execution displays the version of Java installed your system. You need to have atleast Java 9 to pursue
this book.
Snippet-2: Launch JShell
You can launch JShell by typing jshell at your terminal.
in28minutes$>jshell
When run, this command displays basic information about the installed JShell program. A jshell prompt then
appears, which waits for your input.
Snippet-3: Sample JShell input, using a built-in command
The JShell command /help , with a parameter intro , gives you basic guidelines on how you use the tool.
|
| intro
| The jshell tool allows you to execute Java code, getting immediate results.
| You can enter a Java definition (variable, method, class, etc), like: int x =8
| or a Java expression, like: x + x
| or a Java statement or import.
| These little chunks of Java code are called 'snippets'.
| There are also jshell commands that allow you to understand and
| control what you are doing, like: /list
|
| For a list of commands: /help
jshell>
jshell> 3 + 4
$1 ==> 7
jshell>
This was your first real REPL cycle! When you type in 3 + 4 , JShell evaluates it and prints the result.
The entity $1 is actually a variable name assigned to the result. We will talk about this later.
Snippet-5: Getting out of JShell
The /exit command terminates the JShell program, and we are back to the terminal prompt.
jshell> /exit
| Goodbye
in28minutes$>
in28minutes$> jshell
in28minutes$>
Summary
In this step, we learned:
How to launch JShell from our terminal, and run a few commands on it
How to run Java code on the JShell prompt
Step 03: Welcome to Problem Solving
Lets try to break down the PMT-Challenge problem to be able to solve it.
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
in28minutes$> jshell
You probably look at the symbol 'X' as a multiplier, remembering your school days.
Java does not identify ' X ' as the multiplication operator! Java supports multiplication, but only if you use its
predefined operator, * .
Let's type in code shown below:
jshell> 5 * 3
$1 ==> 15
jshell>
Success!
Let's look at some terminology:
5 * 3 is an expression.
5 and 3 are operands. They are also called literals or constant values.
* is an operator.
Java also has built-in operators to perform other numerical tasks, such as:
Addition: +
Subtraction: -
Division: /
Modulo arithmetic: %
The following examples illustrate how to use them.
jshell> 5 * 10
$2 ==> 50
jshell> 5 + 10
$3 ==> 15
jshell> 5 - 10
$4 ==> -5
jshell> 10 / 2
$5 ==> 5
jshell>
Your school math memories are still fresh, and the operators don't seem to disappoint either! + , - and / are
your bread-and-butter operators.
% is the modulo operator, which gives you the remainder when integer division is performed.
jshell> 9 % 2
$6 ==> 1
jshell> 8 % 2
$7 ==> 0
jshell>
jshell> 5 + 5 + 5
$8 ==> 15
jshell> 5 + 10 - 15
$9 ==> 0
jshell> 5 * 5 + 5
$10 ==> 30
jshell> 5 * 15 / 3
$11 ==> 25
jshell> 60 * 24
$1 ==> 1440
Solution 2
60 (seconds in a minute) multipled by 60 (minutes in an hour) multipled by 24 (hours in a day)
$jshell>60 * 60 * 24
$1 ==> 86400
jshell> 5 $ 6
| Error:
| ';' expected
| 5 $ 6
| ^
| Error:
| not a statement
| 5 $ 6
| ^
| Error:
| ';' expected
| 5 $ 6
| ^
jshell> 5 */ 6
| Error:
| Illegal start of expression
| 5 */ 6
| ^
jshell> 5 / 2
$1 ==> 2
jshell>
Surprise, Surprise! JShell seems to evaluate 5 / 2 to 2 instead of 2.5 . Where did we go wrong?
Read what follows, with the biggest magnifying lens you can find:
The result of an expression when evaluated, depends on the operator context. This context is determined by the
operands passed to it
There are two kinds of numbers typically used in programming : integer (1,2,3,...) and floating-point (1.1,2.3,
56.7889 etc). These values are represented by different types in Java. Integers are commonly of type int , and
the floating-point numbers are double by default.
In the expression 5/2 , both 5 and 2 are of type int . So, the result is also of type int .
Let's try with a few floating point numbers:
Both 5.0 and 2.0 are of type double , the result is double . We get a result of 2.5 , as expected.
Let's do a mixed operation - using a floating point number and integer.
jshell> 5.0 / 2
$3 ==> 2.5
jshell>
Among the types int and double , double is considered to be a wider type. When you perform a numeric
operation between two types, the result will be of the wider type.
Understanding Precedence of Operators
Let's look at few complex examples of expressions with more than one operator.
jshell> 5 + 5 * 6
$1 ==> 35
jshell> 5 - 2 * 2
$2 ==> 1
jshell> 5 - 2 / 2
$3 ==> 4
jshell>
Surprised with the results? You might expect 5 + 5 * 6 evaluate to 10 * 6 i.e. 60. Howeever, we got 35 !
We write English left-to-right, and carry this habit to calculations as well.
In expressions with multiple operators, the order of sub-expression evaluation depends on operator precedence.
The basic rules for operator precedence are actually quite simple (we will look at other rules a little later).
The operators in the set { * , / , % } have higher precedence than the operators in the set { + , - }.
In the expression 5 + 5 * 6 : 5*6 is evaluated first. So, 5 + 5 * 6 becomes 5 + 30 i.e. 35.
5 - 2 * 2 and 5 - 2 / 2 are evaluated by following the same rules.
jshell> (5 - 2) * 2
$4 ==> 6
jshell> 5 - (2 * 2)
$5 ==> 1
jshell>
Hmm! Error.
How do we print text?
Java has a built-in utility method called System.out.println() , that displays text on the console.
jshell> System.out.println(3*4)
12
We formed an expression, 3*4 , and passed it to System.out.println() , which is a built-in Java method.
System.out.println(3*4) is an example of a statement. It is a method call.
The syntax rules for method calls are quite strict, and all its parts are mandatory.
jshell> System.out.println3*4)
| Error:
| ';' expected
| System.out.println3*4)
|___________________^
| Error:
| cannot find symbol
| symbol: variable println3
| System.out.println3*4)
| ^---------------------^
What if we want to print an entry of the Multiplication Table, as part of our solution to PMT-Challenge? In other
words, how do we print the exact text 5 * 2 = 10 on the console?