Java Programming For Kids Ages 12 - 18
Java Programming For Kids Ages 12 - 18
Java Programming For Kids Ages 12 - 18
PROGRAMMING
FOR
KIDS
AGES 12 - 18
What is Java?
What is JDK?
What is JVM?
JVM Architecture
Java files are saved with a .java extension. When we compile the .java file,
.class file is generated and this .class file contains byte code. JVM handles
the .class file and generates the desired output of the Java program.
What does compilation mean in Java?
Java compilation is the process of converting a .java file (which contains
readable text Java code) into a .class file (which contains byte code).
1. Class Loader
2. JVM Memory
3. Execution Engine
Class Loader
It is responsible for loading the .class file to the JVM memory.
JVM Memory
It is further divided into:
Stack Area – It stores all the local variables and the results of the
methods (local variables explained in chapter 3).
Execution Engine
It is further divided into:
JIT Compiler – JIT stands for Just in Time compiler and its
main task is to increases the performance and efficiency of
Interpreter.
We have successfully installed JDK in our machine. Now let’s download and
install Eclipse IDE.
Object
I know it all sounds extremely complicated, let’s try to simplify a little bit
below.
Let us consider a class room containing three students, John, Ram and Katy.
These three students have few things in common and they are as follows:
Student is a class.
John, Ram and Katy are objects which belong to class Student.
Name, Gender, Age, Student_ID, Address are variables of class
Student.
If a package contains two Java class files of the same name, it will lead
to name conflict and error in the Java project may occur in future. In
order to prevent that from happening, separate packages should be
created for storing the class file which has the same name.
Now let’s code..
3.6: Examples
Example 1
Launch Eclipse IDE and create a new Java project.
Right click on the src folder -> Click New -> Package (we are
creating a new package).
Default src folder is the source folder which contains the source code or the
main code of our project.
Click Finish
Right click on the hello_world package -> click New -> Click Class
(we are creating a new Java class)
Give the Class name (I gave Hello) starting with a capital letter -> check on
public static void main(String[] args) box -> click Finish.
In Hello.java, write one line of code highlighted in the screen shot
below and execute the program by clicking on the run button.
Code explanation:
Example 2
Let’s create another Java class.
Right click on hello_world package -> New -> Class (I named my
class file students)
Please Note: Always remember Java objects must be declared only after
typing this very important line of code.
With the help of dot (.) operator, we access the variable name from
class Students.
At line 13, 14, 15, we pass values or data into the name variable of
each object.
4.1: Constructor
We learnt about Java object syntax in chapter 3, section 3.1 and we learnt
that the new keyword is followed by call to a constructor so what is Java
constructor?.
Java constructor’s name must match with the Java class name.
Java constructor does not have a return type (return type discussed
in section 4.2).
Default constructor
Default constructor does not contain any parameter.
Parameterized constructor
Parameterized constructor contains parameters.
What is Parameter?
Example
➢ Launch Eclipse IDE -> create a new class ( I named my class Multiply)
Code explanation:
At line 4, instance variable (instance variable discussed in chapter
3, section 3.2) value1 and value 2 are declared.
At line 16, method multiply is declared and this method will return
the multiplication result.
At line 21, Java main method is declared (we have discussed about
this very important line of code in chapter 3 and we will discuss more
about this method as we proceed further).
4.2: Method
A Java method is a block of code performing some task.
For example: Let us consider a Math class, Math contains numbers and
with those numbers we can perform multiple functions like addition,
subtraction, multiplication, division etc. In Java, we can write these
functions in a method.
The signature of a Java method is:
Return type is the data type of the value returned by the method.
Example: Let us consider the method signature written below:
Launch Eclipse IDE -> create a new Class within the hello_world
package (created in chapter 3)
Line 17 contains the main line of code which will start the execution
process and will act as an entry point to Math.java.
At line 20 and 21, the methods of class Math are accessed and
arguments are passed into those methods (In addition method, value
10 is assigned to variable x and other value 10 is assigned to
variable y. In subtraction method, the value 10 is assigned to
variable x and other value 5 is assigned to variable y). Then the
results of those methods are printed by System.out.println.
What is Argument?
Arguments are data values which are passed to the method parameters.
1. if
2. else
3. else if
if ( condition ) {
…………….code………
}
else {
………….. code ………………
}
if ( condition ) {
……..code……………….
}
else if ( condition ) {
………..code………………..
}
else {
………………..code……………
}
These conditional statements check where a certain condition returns Boolean
value TRUE or FALSE. If the condition returns TRUE, a block of code
executes, else another block of code executes.
Let’s code..
Example
Launch Eclipse IDE and create a new Java Class (I named my class
ConditionalStatements) and write the following lines of code shown
in the screen shot below.
Code explanation:
At line 16, else condition is declared and this block of code runs if
both if and else if condition is not satisfied.
In this example, the value of x is 5 and the value of y is 10, so the value of x is
not greater than the value of y and hence the condition will return FALSE
and line 11 will not execute. The value of x is obviously not equal to the value
of y, so the condition will return FALSE and the line 14 will not execute.
Value of x was not greater than y (stated in if condition) and value of x was
not equal to the value of y (stated in else if condition), so this means that x is
less than y and else block of code executes.
5.2: Loops
There are two types of loop:
1. for loop
2. while loop
These loops are used to loop through a block of code to test whether a
certain condition is satisfied or not.
for loop works best with Arrays (we will learn about Array basics in
Chapter 6).
Present value of i is 1.
i = 1 -> the condition is checked (i is indeed less than 5, so the
condition returns TRUE) -> lines 8 runs -> i is incremented 1.
Present value of i is 2.
i = 2 -> the condition is checked (i is indeed less than 5, so the
condition returns TRUE) -> lines 8 runs -> i is incremented 1.
Present value of i is 3.
i = 3 -> the condition is checked (i is indeed less than 5, so the
condition returns TRUE) -> lines 8 runs -> i is incremented 1.
Present value of i is 4.
i = 4 -> the condition is checked (i is indeed less than 5, so the
condition returns TRUE) -> lines 8 runs -> i is incremented 1.
Present value of i is 5.
i = 5 -> the condition is checked (i is indeed equal to 5, so the
condition returns TRUE) -> lines 8 runs -> i is incremented 1.
Present value of i is 6.
i = 6 -> the condition is checked (i is NOT less than or equal to 5,
so the condition is FALSE) -> EXIT out of the loop.
Now let’s run the above piece of code:
5.2.2: while loop
while loop keeps on executing a block of code as long as the condition is
TRUE.
The syntax is:
while ( condition ) {
……….code…………..
}
Example
Code explanation:
Example
➢ In Eclipse IDE, create a new Class (I named my class BreakExample)
data_type[ ] array_name
or
or
Let us consider the Fruits category. Fruits can be divided into apple, orange,
banana, strawberry etc.
In Java, we can store all the fruits items or elements (shown in the screen
shot above) into a single variable using array.
For example:
String[ ] fruits = { “apple”, “orange”, “banana” }
fruits is an array which holds or stores elements of data type String and
fruits stores elements apple, orange and banana.
array_name [ index_value ]
Example 1
Code explanation:
What is length?
This condition states to continue the for loop till i is less than the length
of the array. Since the length of the array is 3, the for loop will loop 3
times.
Execution flow of the above piece of code:
Example 2
➢ Let’s create another class ( I named my class Car)
Code explanation:
At line 5, we declared an array show whose data type is string. We
also set its size meaning that this array will only hold 2 elements.
car_info returns an array of data type string and that result gets stored
in another array x.
From line 20 to 21, we print out the elements from array x.
Please note: In order to return multiple values from a method, we can use
array as we did in above example.
Chapter 7: Object Oriented
Programming Concepts
The most important Object Oriented Programming or OOP concepts are:-
1. Encapsulation
2. Inheritance
3. Polymorphism
7.1: Encapsulation
Encapsulation is the mechanism in which all the Java methods and
variable are wrapped up into a single unit (Class).
Encapsulation helps to protect the data present inside the unit and
prevents any malicious activity.
In order to access the private variable from outside the class, get and set
methods are used.
set method is used to set a value and get method is used to get the value.
Example
➢ Launch Eclipse IDE -> create a new Class (I named my class Encap).
In line 6, an object of class Encap is created and its set and get methods
are accessed.
First we pass a value to variable username using its set method at line 8 and
then we get and print out the value using its get method at line 9.
Then we pass a value to variable password using its set method at line11 and
then we get and print out the value using its get method at line 12.
The subclass or child class inherits properties from its parent class
using extends keyword.
class Parent {
………………..code…………
}
class Child extends Parent {
……………….code………………
}
A subclass can contain its own properties as well as its parent class
properties.
Example
➢ Launch Eclipse IDE, create a new class (I named my class
SchoolSuperClass) and this class will act as a superclass.
➢ Create three more classes, one for elementary school (I named my class
ElementaryChild), one for middle school ( I named my class
MiddleChild) and one for high school ( I named my class HighChild).
These three classes will act as a child class of superclass
SchoolSuperClass.java.
➢ In superclass SchoolSuperClass.java, write the following lines of code:
In this class, we declared all the variables and methods which are common
to all three schools.
Code explanation:
At line 9 and line 10, the variables school_Id and building_num are
accessed from superclass and values are passed into it.
Open the second class file MiddleChild.java and write the following
lines of code:
The above piece of code is very similar to ElementaryChild.java, only data is
different.
7.3: Polymorphism
Polymorphism is a mechanism in which a method can be executed in many
forms based on the object that is acting upon it.
Polymorphism is of 2 types:
When superclass and subclass have method with same name and
signature, the method of the subclass tends to overrides the method
of the superclass. This mechanism is called Method overriding.
Example
Lion.java
Superclass Animal have an eat method and both subclasses Cow and Lion
also have the same method with same name and signature (highlighted in
the screen shot above) .
In Class Cow.java, at line 10, we created object c of type Animal and a call
was made to the Cow constructor.
At line 11, we called the eat method.
In Class Lion.java, at line 10, we created object l of type Animal and a call
was made to the Lion constructor.
At line 11, we called the eat method.
After running Lion.java we get an output of
In both cases we see that the eat method of each subclass (Cow and Lion)
overrides the eat method of superclass (Animal).
Example
➢ Launch Eclipse IDE -> create a new class (I named my class
SPExample)
Code explanation:
At line 5, a method named show is declared.
2. Write a program that will loop through an array and will break out
of the loop once a condition is satisfied.
Given: Array car containing 5 elements Toyota, Kia, Ford, Tesla, Truck.
Exit out of the loop once car equal to Tesla.
3. Write a program which will contain two methods with parameters
and these methods will return values once called and arguments are
passed into it.
The method much have a parameter whose data type is string and this
method must return the string value once called and an argument is passed
into it.
Answers
1.
2.
3.
4.
Wish you all the best and thank you very much for
buying this book.