0% found this document useful (0 votes)
1K views

Grade 8 Java Icse 8thstd

hjftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftgjjjjjjjjjjjjjjjj

Uploaded by

Blaze Anime
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Grade 8 Java Icse 8thstd

hjftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftftgjjjjjjjjjjjjjjjj

Uploaded by

Blaze Anime
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

GRADE 8

CHAPTER 2

OBJECT ORIENTED PROGRAMMING WITH JAVA


AN INTRODUCTION
OBJECTIVES:
➢ Introduction to JAVA
➢ Feature and building blocks of JAVA
➢ Keywords, variables and constants in JAVA
➢ JAVA Development Kit
➢ Introduction to BlueJ
➢ Writing simple program using BlueJ

FAMOUS PERSONALITY
Introduction to JAVA: James Gosling, founder and lead designer of JAVA
programming language.
JAVA is a high level object oriented
programming language. It was created Worked at Sun Microsystems. He was a passionate
programmer from early graduate – student days. He
by James Arthur Gosling a Canadian
had developed P-Code Virtual Machine for his professor
computer scientist. during those days.

Features of JAVA:
1. Object oriented
2. Modular
3. Platform independent
4. Both compiled and interpreted
5. case sensitive

1. Object Oriented language: In


this method of programming data
is emphasized more than the procedure of implementation.
2. Modular: Complete program can be divided into smaller chunks called as
functions which gives a modular approach to programming
3. Platform independent: It is not dependent on platform or operating
system. JAVA can run on DOS, UNIX or WINDOWS.
4. Compiled and Interpreted: Java code called as source code is first
compiled to generate Byte code. Later Byte code is converted into machine
code by the interpreter called as JAVA Virtual Machine (JVM).
5. Case sensitive: It distinguishes between upper case and lowercase
alphabet.

The building blocks of JAVA program : Object and class are the building
blocks of a java program. They are like two sides of a coin, which are
integrated with one another.
Object <h3>is a unique entity which contains data (characteristics) and
functions (Behavior) bound together.
Let us consider the chalk used to write on the black board. It is white in
colour (Characteristic). It is used to write (Behavior) on the board.

All the objects seen above are real time objects, which can be seen and their
features can be described.
All of them are having unique state (characteristics) and functions(behaviour).
Let us consider the example of book
Characteristics:
• It has brown wrapper
• It is rectangular in shape

Behaviour:
• It is used to write
• Its contents can be read
Class : <h3> Class is a group of similar objects , having similar
characteristic and behavior. Class is known as object factory as it is used
to create objects.
Let us consider the example of a Vehicle as a class. If vehicle is a class, it
can have objects like TwoWheelers, ThreeWheelers, FourWheelers.

It is a blue print or set of protocols. It can be imagined as a natural class room


system where in you study. Class room has a set of rules to be followed by
students and teachers. It has a defined curriculum to be followed. It has
predefined time table. Using this we can conclude students are objects of the
class and they follow the rules of the class.
Like this JAVA classes also has predefined set of rules to be followed by all the
objects of the class.
Consider the example of a fruit basket.

If we define “fruits ” as a class , then all


different types of fruits are its objects. They are having their own set of features
and functionality, even though all of them are having some common features.

Banana , Grapes, Orange and


Apple are individual objects of Fruits class. All of them are fruits but have
their own taste, which makes them distinct objects of the class fruits.
FACT FILE
The key persons involved in the development of JAVA language were James
Gosling, Arthur Van Hoff and Andy B, headed by James Gosling. The name was
coined by using first letters of the names of the developers.!!!!!!!!!!!!!!

Keywords (Reserved words): These are the JAVA tokens, that are reserved by
the language. They cannot be used to name variables.
Some of the frequently used Keywords are:
int double private next import
float String main static java
if short class case Scanner
else while void switch new
for public char break default
Table 2.1 Keywords in JAVA
Constants (Literals): It is a fixed value that does not change during the execution
of the program. Different types of literals are:

STRING LITERAL

REAL LITERAL

LITERALS CHARACTER LITERAL

BOOLEAN LITERAL

INTEGER LITERAL
String literal: Alphanumeric characters and special characters enclosed with in
double quotes.
ex: “Coding is fun !!!!!!”
Character literal: An alphabet , a number or a special character stored with
single quotes .
Ex: ‘a’, ‘7’
Boolean literals: Special constants true or false.
Integer literals : Positive or negative whole numbers
ex: 23, -89
Real Literals: Positive or negative decimal numbers
ex: 34.9, -0.78
Variables (Identifiers) : It is a named memory location used to store values.
Rules for naming variables:
➢ JAVA is case sensitive, upper case and lower case letters are treated
different
➢ reserved words cannot be used as identifiers
➢ _ and $ are the only two special characters, that can be used in identifier.
But $ can be used only after the first character.
➢ Though numbers can be used, they can be used only after the first
alphabet
Types of variables:
STRING VARIABLES

REAL VARIABLES

VARIABLES CHARACTER VARIABLES

BOOLEAN VARIABLES

INTEGER VARIABLES

STRING VARIABLES: Named reserved memory location used to store


String constants. Represented by the datatype String .
Intialization:
String a= “123”;
String k= “ ” ;
String ans= “I LOVE JAVA !!!!!!!!!!!” ;
Here a, k and ans are String variables.

REAL VARIABLES:
Named reserved memory location used to store positive or negative
decimal values/ constants. Represented by the datatype float.
Initialization:
float ans=5.9;
float cost= -0.8;
here ans and cost are float variables used to store real (decimal) values

CHARACTER VARIABLES: Named reserved memory location used to store


single alphanumeric values. Represented by the data type char
Initialization:
char a=’h’;
char a=’2’;
char ans=’*’;
here a and ans are character variables.
BOOLEAN VARIABLES: Named reserved memory location used to store
the boolean values true and false. Represented by the data type boolean
Initialization:
boolean result=false;
boolean value=true;
here result and value are boolean variables .

integer variables: Named reserved memory location used to store


positive or negative whole numbers. Represented by the data type int
Intialization:
int sum=5;
int i=-8;
here sum and i are int variables

Valid and invalid variables:<h3>


VALID INVALID
A12 12A
Money$ $Money
Student_Name Student Name
Employee Employee**
Table 2.2 valid and invalid variables in JAVA

Output Statement: <h3>


The most frequently used output statements in JAVA are
1. System.out.print() : used to display output on the screen. After display the
cursor remains in the same line.
2. System.out.println() : used to display output on the screen, but after display
the cursor moves to the beginning of next line.
Note: The test to be displayed is enclosed within double quotes, where as
variables doesnot need any quotes. Multiple output values can be combined
using concatenation operator +.
Example:
System.out.printl(“The sum is “+ sum);
JDK (JAVA Development Kit): <h2> It is a software development environment
used to develop JAVA programs. The most popular JDK are BlueJ, NetBeans,
Eclipse, JSource . They have the tools used to write, test and debug JAVA
Programs.

Introduction to BlueJ :

BlueJ is an IDE (Integrated Development Environment). It provides an


interactive environment having menu driven approach suitable for small scale
software development.
It is a free and opensource software licensed under GNU GPL. JDK 1.5 or higher
version must be downloaded . (cnet.com can be used to download it. ) Then
BlueJ which can be downloaded from bluej.org

JAVA programs are created as classes in BlueJ project. Every Class created will
have a sample program to guide the user. Bluej window looks like
Figure 2.1 BlueJ window
Steps to create a new Project
Step 1: Select Project
Step 2: Choose New... from the menu.
Step 3: A file selection dialogue opens that lets you specify a name and location
for the new project.
Step 4: Choose name for project. After you click OK, a directory will be created
with the name you specified, and the main window shows the new, empty
project as shown in Figure 2.2

Figure 2.2 New project window in BlueJ


Steps to create a new class:
Step 1: Click on New Class option, Create New Class window appears as shown
in Figure 2.3

Figure 2.3 Create New Class window in BlueJ


Step 2: Enter the class name and click OK
Step 3: A class gets created as shown below, with the name specified by user
Figure 2.4 Window showing new Class in BlueJ
Steps to Write JAVA program:
Step 1: Double click on the class in Figure 2.4 as shown
Step 2: The editor window appears ( As shown in Figure 2.5) with the sample
program. Clear the sample program .
Step 3: Write the required program as shown below.
Figure 2.5 Editor Window showing JAVA program
Step 4: Click on compile option to compile the program
Step 5: Right click on the compiled program to create the object, Click Ok

Figure 2.6 Create Object Window

Step 6: The object gets created as shown in Figure 2.7


Figure 2.7 Object creation

Step 7 : Right Click on the object to see a drop down menu, choose the second
option void main() to execute the program
Figure 2.8 Object creation

Step 8: The output appears on the output window as shown in Figure 2.9

Figure 2.9 Output Window

Writing the JAVA code:


Method 1: by assigning values to variables in the program
class addition
{
void main()
{
int num1=6 , num2 = 9, sum=0;
sum= num1+num2;
System.out.println( “The sum is “+sum);
}
}
Method 2: By accepting the input from the user when the program is running
class addition1
{
void main (int a, int b)
{
int sum=0;
sum= a + b;
System.out.println( “The sum is “+sum);
}
}
After compilation the following window appears, enter the values to obtain the
output:

Figure 2.9 Method Call Window


The output will appear as
The sum is 11
TRY THIS
"write once, run anywhere" is the key concept of JAVA. Write the JAVA
program and execute it on Linux machine.

Sample JAVA programs:


1. Program to display the Biodata
class Bio_Data
{
void main()
{
System.out.println( “Name : Aditi“);
System.out.println( “Standard: 9 “ );
System.out.println( “School: Indian High School “);
}
}
2. JAVA program to accept marks obtained in 3 subjects and calculate total
marks obtained.
class total
{
void main (int math, int eng, int sci)
{
int tot=0;
tot= math + eng + sci ;
System.out.println( “The total marks obtained is “+ tot);
}
}
3. JAVA program to accept Selling price and Cost price of a product and
calculate the profit obtained
class profit
{
void main (int sp, int cp)
{
System.out.println( “The profit is “+(sp-cp));
}
}
4. JAVA program to accept marks obtained by 5 students in computer science
and calculate the average marks of the class
class average
{
void main (float s1, float s2, float s3, float s4, float s5)
{
float sum=0.0;
Avg= (s1 + s2 + s3 +s4 + s5)/5;
System.out.println( “The Class Average is “+Avg);
}
}
Tech terms:
➢ Tokens: Every individual unique unit in JAVA program is termed as a
token
➢ Source Code: program written using a programming language
➢ Byte code: Code obtained after compiling the JAVA source code
➢ Platform : Operating System.
Let us Summaries:
➢ JAVA is an Object Oriented programming language
➢ Object is a unique entity having characteristic and functions
➢ Class is collection of objects, its an object factory
➢ A variable is a memory that holds data values
➢ Reserved words are the predefined words reserved by JAVA
programming language
➢ Constants are the values that does not change during the execution of
the program
➢ Bluej is a free and opensource Integrated Development Environment
used to write JAVA programs
➢ BlueJ is user friendly and has menu driven approach towards
programming

KNOW MORE
Applet is a small Java program that can be embedded
into a web page. It runs inside the web browser , can
be accessed on an Internet server, transported over
Internet, and can be automatically installed and run as
apart of a web document.
Applets run on web browser or an applet viewer. JDK
provides a standard applet viewer tool called applet
viewer.

Short cut keys used in BlueJ :


Ctrl + n Open New Project
Ctrl + o Open an existing project
Ctrl + w Close
Ctrl + s Save the project
Ctrl + k Compile the program
Ctrl +Shift +r Reset JAVA virtual machine
Table 2.3 shortcut key of BlueJ
TEACHER’S NOTE :

Discuss the concept of Procedure Oriented Programming. Compare it with OOP.

Demonstrate the concept of Object and Class in comparison with real life
examples.

Lab exercises:
Write the JAVA program and the output generated for the following
1. To calculate the amount payable by accepting cost of the product and
number of products purchased.
2. Display the Biodata by accepting the name, age and school from the
user.
3. Accept a number and display its Square and Cube.
4. Accept the marks obtained by a student in 5 different subjects.
Calculate the percentage of marks obtained.
Online Links:
to download JDK
https://download.cnet.com/Java-Development-Kit-64-Bit
to download BlueJ
https://www.bluej.org/

You might also like