0% found this document useful (0 votes)
4 views17 pages

Grade8_Java

Basic java concepts to be introduced for grade 8 ICSE ICT

Uploaded by

Nilofar Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Grade8_Java

Basic java concepts to be introduced for grade 8 ICSE ICT

Uploaded by

Nilofar Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

EuroSchool ICSE Grade-8

Programming with Java


Java is a high-level programming language and is very popular
amongst software developers. One reason for Java’s popularity is
that it is platform independent. This means that the same Java
program can be run on many different types of computers.

Java was first released by Sun Microsystems in May 1995 by a


team of software developers headed by James Gosling.

BlueJ is Java Integrated Development Environment specifically


designed for teaching. It is very useful for familiarizing oneself with
the Java syntax and for basic software development.

Java is known as an object-oriented programming(OOP) language.

Just like in English language, to learn the language we start with


‘words’ similarly in Java Language we have ‘tokens’

Tokens
A token is functional and fundamental unit of computer program.
Each program statement is composed of various components
known as a ‘Token’.
Various tokens in Java are:
1. Literals(Constants)
2. Keywords
3. Identifiers(Variables)
4. Operators

1. Literals(Constants)
Literals are the constants i.e. the fixed values in Java program.
Java literals can be classified as:
 Integer Literals e.g. 14, 345, -18, -391
 Real Literals e.g. 24.6, 0.0072, -3.652
 Character Literals e.g. ‘A’, ‘d’, ‘3’, ‘*’
 String Literals e.g. “COMPUTER”, “Year 2020”, “10% per annum”
 Boolean Literals only two possible values– true or false(never
enclosed in quotes)
E.g. boolean ans = true;

Programming with Java 1


EuroSchool ICSE Grade-8

2. Operators
Operators are basically symbols or tokens to perform arithmetic or
logical operations.
Types of operators:
i. Arithmetical operators:
Arithmetic operators are used to perform common mathematical
operations. For example +, -, *, /, % for addition, subtraction,
multiplication, division and modulus respectively.
ii. Logical operators:
Logical operators are used to determine the logic between
variables or values. For example
Operator Name Description Example
&& Logical returns true if both x < 5 && x < 10
And statements are true
|| Logical Or Returns true if one of x < 5 || x < 4
the statements is true
! Logical Not Reverse the result, !(x < 5 && x < 10)
returns false if the
result is true

iii.Relational operators:
Relational operators are used to compare two values (or
variables). The return value of a relational operator is
either true or false. For example <(less than), <=(less than or
equal to), >(greater than), >=(greater than or equal to),
==(equal to), !=(not equal to)

Programming with Java 2


EuroSchool ICSE Grade-8

3. Identifiers(Variables)

Variable is named memory location that contains a value.


Value can change

Syntax: <data type> <space><variable name>


o e.g int m;
o float p,q,r;

Rules for naming a variable


 Java keywords cannot be used as a variable name.
 A variable name must start with a letter or an underscore
character (_)
 A variable name cannot start with a digit.
 A variable name can only contain alpha-numeric characters and
underscores ( a-z, A-Z , 0-9 , and _ )

Initializing a variable
Uninitialized variable contains garbage value(absurd value). A
variable can be initialized as follows:
Direct assignment of a constant to a defined variable
e.g. int a = 5;
float b = 10.5;
double d = a + b;

4. Keywords
Keywords are reserved words which are preserved by the system
and carry special meaning for the system compiler.
E.g. class, public, etc

Programming with Java 3


EuroSchool ICSE Grade-8

Data types in Java


Data types in Java are of different sizes and values that can be
stored in the variable.
There are 8 primitive data types categorized into 4 different types
as follows:

Integer type
• A variable of integer type can store a whole number.
• Either positive or negative, but without a decimal point.
• It has four types as shown below:

Floating point
• To store a fractional number i.e. a number with decimal points.
• It has two types as shown below:

Programming with Java 4


EuroSchool ICSE Grade-8

Characters
• A character type variable contains a single character.
• Size of character variable is 1 byte i.e. 8 bits
• The declaration of a character can be given as:
• Syntax: <data-type> <variable> = <‘character literal’>;
• For e.g.: char ch = ‘a’;
• A character literal is always enclosed within single quotes.
• Similarly, a set of characters(String) can be declared as,
• Syntax: <data-type> <variable> = <“String constant”>;
• For e.g.: String p = “Computer Applications”;
• A string is always enclosed within double quotes.

Boolean
In programming, when we need a data type that can only have one
of two values, like:
 YES / NO
 ON / OFF
 TRUE / FALSE
For this, Java has a boolean data type having size 1 byte, which
can store true or false values.
For example: boolean ans = true;

Programming with Java 5


EuroSchool ICSE Grade-8

Java Hello World Program


A "Hello, World!" is a simple program that outputs Hello, World! on
the screen.

Programming with Java 6


EuroSchool ICSE Grade-8

Programming Examples:

Example 1:
Write a program to find sum of two values stored in two variables.
public class Example1
{
public static void main()
{
int a=5;
int b=10;
int sum = a + b;
System.out.println("Sum is "+sum);
}
}
Output:

Example 2:
Write a program to print a name stored in a string variable.
public class Example2
{
public static void main()
{
String name = "Rahul";
System.out.println("Hello "+name);
}
}
Output:

Programming with Java 7


EuroSchool ICSE Grade-8

Example 3:
Write a program to accept a name from main method and print it.
public class Example3
{
//Accepting input through main method
public static void main(String name)
{
System.out.println("Hello "+name);
}
}
When executing this program, after selecting void main() we will
get another window where we can enter a value for name.

Output:

Programming with Java 8


EuroSchool ICSE Grade-8

Example 4:
Here’s a program to show the use of all six arithmetic operators
where two integer values are being accepted from the main
method.

public class Example4


{
public static void main(int a, int b)
{
int sum = a+b;
int difference = a-b;
int product = a*b;
int quotient = a/b;
int remainder = a%b;

System.out.println("Sum of the two numbers is "


+sum);
System.out.println("Difference of the two
numbers is " +difference);
System.out.println("Product of the two numbers
is " +product);
System.out.println("Quotient after dividing 1st
number by the second number is
"+quotient);
System.out.println("Remainder after dividing 1st
number by the second number is
"+remainder);
}
}

Programming with Java 9


EuroSchool ICSE Grade-8

Execution:

Output:

Programming with Java 10


EuroSchool ICSE Grade-8

Example 5:
Let’s see a program to understand the use of post-increment and
post-decrement operators.
public class Example5
{
public static void main(int a, int b)
{
System.out.println("Original value of a is "+a);
System.out.println("Original value of b is "+b);

a++; //increments the value of the variable by 1


b--; //decrements the value of the variable by 1

System.out.println("Changed value of a is "+a);


System.out.println("Changed value of b is "+b);
}
}
Execution:

Output:

Programming with Java 11


EuroSchool ICSE Grade-8

Example 6:
Let’s understand relational operators with the help of an example.
As we had seen earlier the return value of a relational operator is
either true or false.
public class Example6
{
public static void main(int a, int b)
{
System.out.println("Is (a < b)? "+(a<b));
System.out.println("Is (a <= b)? "+(a<=b));

System.out.println("Is (a > b)? "+(a>b));


System.out.println("Is (a >= b)? "+(a>=b));

System.out.println("Is (a == b)? "+(a==b));


System.out.println("Is (a != b)? "+(a!=b));
}
}
Output:

Programming with Java 12


EuroSchool ICSE Grade-8

Conditional Construct

Now, we’ll learn how to use the if-else statement in Java.


The if-else statement is the most basic of all control structures,
and it’s likely also the most common decision-making statement in
programming.
It allows us to execute a certain code section only if a specific
condition is met.
Syntax of if…else
The if statement always needs a ‘boolean’ expression as its
parameter.
if (condition)
{
// Executes when condition is true.
}
else
{
// Executes when condition is false.
}
It can be followed by an optional else statement, whose contents
will be executed if the boolean expression is false.

Programming with Java 13


EuroSchool ICSE Grade-8

Example 7:
Let’s understand it with the help of a simple example.
public class Example7
{
public static void main(int a, int b)
{
if(a>b)
{
System.out.println("a is greater than b");
}
else
{
System.out.println("b is greater than a");
}
}
}
Execution: Output:

Programming with Java 14


EuroSchool ICSE Grade-8

Example 8:
Write a program to check if the given number is odd or even.
public class Example8
{
public static void main(int a)
{
if(a%2==0)
{
System.out.println("a is even");
}
else
{
System.out.println("a is odd");
}
}
}

Execution: Output:

Programming with Java 15


EuroSchool ICSE Grade-8

Example 9:
Write a program to accept age through main method check if the
person is eligible for voting.
public class Example9
{
public static void main(int age)
{
if(age>=18)
{
System.out.println("You're eligible for
voting");
}
else
{
System.out.println("You're not eligible for
voting");
}
}
}
Execution:

Output:

Programming with Java 16


EuroSchool ICSE Grade-8

Example 10:
Write a program to accept gender as a character input through
main method and print an appropriate message.
public class Example10
{
public static void main(char gender)
{
if(gender=='M')
{
System.out.println("Male");
}
else if(gender=='F')
{
System.out.println("Female");
}
}
}
Execution: Output:

Programming with Java 17

You might also like