Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Grade VIII

Computer Applications

Introduction to Java

Program: A program is a set of instructions written to perform a specific task.

Programming Languages: Programming Languages can be broadly classified as:


i) Low Level Language:
Machine Language (1GL)
Assembly Language (2GL)
ii) High Level Language:
Procedural (3GL)
Non-Procedural (4GL)

i) Machine Language: (1st Generation Language) Machine language is literally


binary sequences (lots of 0s and 1s) interpreted by the computer as instructions. It
is the only language that the computer directly understands.

Advantage: Its efficiency and speed of processing.

Disadvantages
a. Coding in the machine language is an extremely cumbersome and complicated
task that is prone to errors.
b. The language itself is not very human readable. (i.e. user friendly).
c. The language is also machine dependent and requires knowledge of complete
computer circuitry.

ii) Assembly Language: (2nd Generation Language) The first language similar to
English was developed in 1950 which was known as Assembly Language or
Symbolic Programming Language. Assembly language is a low level language
that uses small symbolic words called mnemonics instead of zeroes and ones.
These mnemonics like ADD to add, MUL to multiply, LOC to locate an address
and such others express the operations to be performed. A translator called the
assembler converts these mnemonics to the machine language and performs the
necessary action.
Advantages: Assembly language is very powerful and efficient. It is fast, small in
program size and flexible. This language is popularly used at microprocessor level
and system coding.
Disadvantages: It is not very user friendly and easily understandable.
The assembler must be loaded to the computer memory for translation and it
occupies a sizeable memory of the computer, hence Assembly Language cannot
be used with small capacity computers.
iii) High Level Language: (3rd Generation Language) Procedural languages are used
to solve problems and are often described as problem oriented languages. They
focus on the method or HOW to solve the problem. In high level languages, the
programs are written in a language which is closer to the user i.e. English, just
following some rules specific for the language (syntax). Every instruction that
the programmer writes in this language is translated into the machine language
using a language processor /translator called the compiler or the interpreter.
Advantages: HLL is independent of the internal machine code of any particular
computer. High Level language is the most understandable and easy to code
language besides having the following advantages, easier documentation, easy to
learn and use and modifications can be done more easily.
Disadvantages: The syntax and the semantics of the language need to be learnt
and only then can it be used.
BASIC, COBOL, Pascal, FORTRAN, C, C++, Java are some examples of HLL.
Programming in HLL can be done in one of the following two approaches:
a) Procedural Programming Approach
b) Object Oriented Programming Approach.

iv) Non-Procedural Language: 4GLs are non procedural, that is only “WHAT is
required” is to be specified and the rest gets done on its own. The main aim of the
4GL is to cut down on development and maintenance time and make it easier for
users. The user interacts with the utility software which is an integral part of a
4GL and that in turn interacts with the system to get the work done. Word
processors, spreadsheets, DBMS, graphic software are some of the examples of
4GLs.

TRANSLATORS OR LANGUAGE PROCESSORS:


(COMPILERS AND INTERPRETERS)
Translators are system software that perform the process of converting a high level
language instruction to the machine language.
A) Compiler: A compiler converts the HLL program into the machine language at one go. It
converts the entire HLL program at a time and reports all the errors of the program along
with the line numbers. After the program is debugged, it is recompiled and after that the
compiler is not needed in the memory as the object program is available.

B) Interpreter: This language processor converts s a HLL program into machine language by
converting and executing it line by line. It there is any error in any line, it reports it at the
same time and program execution cannot be resumed till the error is rectified. The interpreter
must always be present in the memory every time the program is executed. For error
debugging, interpreter is very much useful as it reports the errors immediately on occurrence
but after the program is debugged, unnecessary usage of memory takes place as it has to be
present in the memory always.
Therefore, combination of interpreter and compiler is an ideal option of HLL
translation to the machine code. For the initial error removal, the interpreter can be used and
then the program can be compiled enabling the removal of the language translator from the
memory.
Programming Approach
Programming in HLL can be done in one of the following two approaches:
a) Procedural Programming Approach
b) Object Oriented Programming Approach.

a) Procedural or Structured Programming: Developing a language with a process


centric approach is called procedural programming. It concentrates on HOW to solve
the problem. It focuses on the methods/processes for which the software is being
built. This approach refers to breaking up the complex problem into smaller problems
and then solving each by using modules (functions or procedures) which act on the
data. These functions are tightly interdependent on each other. Example: BASIC,
COBOL, C, Pascal.

Drawbacks:
i) The modules or parts of the whole program strongly depend on each other.
So a module cannot be reused by other programs.
ii) The procedural program code is not flexible to change. The program
written in a procedural language completely needs to be changed as all the
parts of a program are linked and dependent on each other.
iii) Solutions to real world problems cannot be directly depicted using this
language.

b) Object Oriented Programming: OOP simulates the real world in software. Real
world consists of identifiable entities or objects. These objects have their peculiar
characteristics and behavior. Examples of real world objects are books, pens, students,
chair, and car and so on. This approach concentrates on “WHAT”- “What is the object
that will get affected by this problem?” and then the approach works on the
attributes/state and behavior/methods of that object. Examples: Smalltalk, C++, Java.
Advantages:
i) This approach models the real world quite accurately as the programs
written in the language are focused on the objects rather than the
procedure.
ii) It is extremely reusable as these objects can be invoked in different
programs.
iii) Programs written in OOP are simple to change when the requirements for
the application changes.
iv) The principles of OOP prevent the objects from being mishandled and are
kept safe from unauthorized access.
JAVA
(An Object Oriented Programming Language)
History of Java
Sun Micro systems began for software to run on TV and VCR sets i.e. Interactive TV
and VCR sets. A team of software engineers led by James Gosling, Patric Naughton, Chris
Warth, Ed Frank and Mike Sheridan laid specifications for these projects in 1991. By
September 1992, the basic system was drafted. These interactive sets were called set-top
boxes. The hardware was called 7(Seven), OS was called Green and the programming
language was called OAK.
Between the initial implementation of OAK in the fall of 1992 and the public
announcement of Java in 1995, many more people contributed to the design and the evolution
of the language. Bill Joy, Arther van Hoff, Jonathan Payne, Frank Yellin and Tim Lindholm
were key contributors to the maturing of the older prototype.
Hot Java was the first commercial product to be developed completely in Java. On
May 23, 1995, Java was commercially released. The advent of Internet gave a tremendous
boost to this language. Java Programs can be embedded in HTML pages and downloaded by
Web browser to bring live animations and interaction to web clients. The power of Java is not
limited to web applications. Java is a general purpose programming language. It has full
programming features and can be used to develop standalone applications.

Characteristics of Java

1) It is simple: No programming language is simple, but java is bit simple as compared to


other OOP language such as C++.
2) It is object oriented: Java is called OOP language because Java is centered on creating
objects, manipulating objects and making objects working together.
3) It is distributed: Distributed computing involves several computers on a network
working together. Java is designed to make distributed computing easy. Networking
capability is inherently integrated into Java.
4) Java is compiled as well as interpreted: One needs an interpreter and a compiler to run
Java programs. The programs are compiled into Java Virtual Machine Code called
bytecode. The bytecode is machine independent and can run on any machine that has java
interpreter.
5) Java is highly case sensitive: Java is highly case aware. Instructions written in java are
different when written in the lower case and the upper case.
6) It is robust: Robust means reliable. No programming language can assure reliability.
Java puts a lot of emphasis on early checking of possible errors as java compilers can
detect many problems that would throw show up at execution time in other languages. It
has a runtime exception handling feature to provide programming support for robustness.
Java can catch and respond to an exceptional situation so that the program can continue
its normal execution and terminate gracefully when a runtime error occurs.
7) It is secure: Java being an Internet programming language is used in a networked and
distributed environment. You can download a java applet, run it on your computer and it
will cause no damage to your system.
8) It is architecturally neutral- The most remarkable feature of java is that it is
architectural neutral (Platform Independent). You can write one program that runs on
any platform with JVM (Java Virtual Machine). You can run Java Applets from a Web
Browser, but java is more than just a web applet. You can also run stand-alone java
applications directly from operating system by using a java interpreter.
9) It is portable: Java programs can run on any platform without having to be recompiled.
This is one positive aspect of portability.
10) It is multithreaded: Multithreading is capability for a program to perform several tasks
simultaneously within a program. E.g. downloading a video file while playing a video file
would be considered multithreading. It is smoothly integrated in Java.
11) It is a dynamic and one of the most high performance languages today.

JAVA APPLICATIONS
Java programs can be one of the two types: stand alone applications and applets.
Applications are stand-alone programs like any program written using high-level language.
Applications can be executed from any computer with a Java interpreter and are ideal for
developing software. Applets are special kind of Java programs that can run directly from
Java compatible Web browser. Applets are suitable for deploying (installing) Web projects.
Java Program Structure (For stand alone programs only)
Probably, the best way to start learning a programming language is to write a simple
program. [* Please consider the program structure as a mandatory syntax requirement, the
details of what is it will be learnt by you in due course of time]

/* program to -- Welcome to Java*/


class Welcome
{
public static void main(String args[])
{
System.out.println(“Welcome to Java”);
}
}
Let us now examine the parts of this sample program:
i) /* program… welcome to java*/
The text enclosed with /* …*/ is called comment. Comments or remarks are non
executable statements purely for enhancing the program readability.

ii) class Welcome


Every java program should begin with the “class” keyword. Java being OOP, classes
contain attributes/data members and behavior/methods of the objects. Methods may
contain other executable statements. Here a class/program with the name Welcome is
defined. It is also called the initial class since it defines the program name. The file
name of this program should be Welcome.java- i.e. same name as that of the class.
Although there can be many classes in a Java program, there can by only one initial
class in a Java program. An initial class contains the main function inside it.

iii) public static void main(String args[])


This is the syntax of the “main” method, it cannot be changed. It is the most important
method because it is from this method that the application begins executing the program.
(Cannot have any other name) If there are several methods in a program, the program
will only begin executing from the main method. If there are many classes within a Java
program, the execution will only begin from the initial class (the class that contains the
main method) If there is no main method in the class, no execution will take place. Thus
the main method is also called the driver method. On reaching the end of main, the
program terminates and the control passes back to the operating system.

Let us now understand each keyword of this method declaration:


public- “main” is the first method called by the Java environment when a program is
executed so it has to be accessible from the Java environment (JDK software). This
method has to be publicly available otherwise how can it be accessed outside the
program. Hence the access specifier has to be public.

static- static methods are class methods that do not need the creation of any object to
work with the method.

void – Functions or methods can return a value. void means nothing. The main
method does not return any value, hence it is declared void.

String args[ ]- Functions or methods can also take in arguments or parameters to


work on them. The main method can take arguments in this form.

iii) { } The pair of curly braces called the block of code marks the scope of the main
method and the scope of the class. They indicate the boundaries.

iv) System.out.println(“Welcome to Java”);


This statement prints “Welcome to Java” on the standard output device which is
generally the monitor. This is the statement to print the desired output that is present in the
brackets (“ … ”). Here, System – computer, out – output device(monitor) , println – print the
output and the control goes to the next line.

Exercise I : Write programs in Java to:


i) Print your name
ii) Print your name and address
iii) Print your name , school name and class name
iv) Print a pattern like “****”
“***”
“*”
v) Print the message “hello world!”.

The Java Language

Java Keywords: Keywords are the words that convey a special meaning to the language
compiler. These are reserved for special purpose and cannot be used as normal identifier
names.
abstract boolean break Byte case catch
char class const Continue default do
double else extends Final finally float
for goto if implements import instanceof
int interface long native new package
private protected public Return short static
strictfp super switch synchronized this throw
throws transient try Void volatile while

*true and false and null are reserved words but not keywords, they are literals.

Identifiers:
Identifiers are fundamental building blocks of a program. They are programmer designed
tokens. They are used for naming all the components of a program like the classes, methods,
variables, objects, labels, packages, and interfaces in a program. The identifier follows the
following rules:
1. They can have alphabets, digits, underscore, and dollar sign characters.
2. They must not begin with a digit.
3. Uppercase and lowercase letters are distinct. Java is case sensitive.
4. They can be of any length.
5. They must not be a keyword or a boolean or null literal.
Java developers follow some naming conventions for the identifiers. Conventions are good
programming practice to improve the readability of the program.
• Names of all public methods and instance variables start with a leading lowercase letter.
If the name has more than one word, the second and subsequent words are marked with a
leading uppercase letters.
E.g. average
totalMarks
• All private and local variables use only lowercase letters combined with underscores.
E.g. length
batch_strength
• All classes & interfaces start with a leading uppercase letter (and subsequent words are
marked with a leading uppercase letter).
E.g. Student
MotorCycle
• Variables that represent literal values use all uppercase letters & underscores.
Eg TOTAL
MAX_MARKS

Literals
They refer to fixed values that do not change during the execution of a program.

Integer literals
It refers to sequence of digits. There are 3 types of integer literals, namely, decimal integer,
octal integer and hexadecimal integer.
Decimal integer consists of a set of digits, 0 through 9, preceded by an optional minus sign.
An octal integer literal consists of digits 0 through 7 with a leading zero. A sequence of digits
preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets
A through F.
Real literals
Integers containing fractional parts are called real (or floating point) literals. Eg. –0.75. It can
also be expressed in exponential notation. Eg. 2.345e2

Character literals
It contains a single character enclosed in single quotes. Eg. ‘5’, ‘s’ as well as certain escape
sequence characters.

String literals
It is a sequence of characters enclosed between double quotes. Eg. “ Good Day! “

Comments:
Comments or remarks are non executable statements in the program that improve the
readability of the program. They help in internal program documentation to understand the
code better. It is a convention to use comments regularly in the source code.
There are three types of comments in Java:
i. Single line comment //this is a comment
ii. Multi line comment /* all the enclosed
lines are comments */
iii. Documentation Comment
/** Program Name : Test
* Author : ABC
* Code Date: Feb 11, 2011
*/
Data Type:
Java like any other language provides ways to facilitate the handling of different types
of data. Data types are means to identify the type of data and the associated operations of
handling it.

Java data types are of two types:


i) Primitive or Intrinsic data type.
ii) Reference or derived data type.
Primitive data types come as a part of the language. Java has 8 primitive data types- byte,
short, int, long, float, double, char, boolean.

Integer types
Holds whole numbers. Java supports four types of integers - byte, short, int and long. Java
values are signed meaning either positive or negative.
Data Types Size in Bytes
byte 1
short 2
int 4
long 8
Floating Point Types
They are used to hold numbers containing fractional parts. There are 2 types of floating point
storage- float and double. Float type values are single-precision while double represent
double-precision numbers.
Data Types Size in Bytes
float 4
double 8

Character Type
These are used to store character literals in memory. They are always enclosed in single
quotes e.g. ‘ a‘. The data type is called char. It occupies 2 bytes in memory.

Boolean Type
It is used to test a condition. A boolean datatype can only have one of these two values: true
or false. It occupies 1 byte in memory.
Variables
It is a named memory location storing varying values. It is an identifier that denotes a storage
location used to store a data value. It may take different values during program execution.

Operators and Operands


An operator is a symbol that tells computer to perform certain mathematical or logical
manipulations. They are used in programs to manipulate data and variables. Java operators
are classified into following categories:
Operands are identifiers on whom operators perform the operations.
int a = b + c; // Here = and + are operators
a, b and c are operands

Unary Operators (Operators that perform operations on one operand)


Increment & decrement operator
These operators are ++ and --. ++ add 1 to the operand while -- subtracts 1.

Binary Operators (Operators that perform operations on two operands)


Arithmetic operators
The operator +, -, *, /, % can operate on any built-in numeric data type. The unary minus
multiplies its single operand by –1.

Relational operators
The comparison of 2 data items is done with the help of relational operator. The result of a
relational expression is either true or false. The relational operators are <, <=, >, >=, ==, !=.

Logical operators
It also yields true or false. The operators are &&(and), | |(or), !(not).

Assignment operators and Shorthand Assignment Operators.


They are used to assign the value to a variable. (= is the assignment operator)
The shorthand assignment operators are +=, *=, /=, %=, -=.

Ternary Operators (Operators that perform operations on three operands)


Conditional operators
The pair ?: is a ternary operator in Java. It is used to construct conditional expression in Java.
exp1 ? exp2 : exp3
if exp1 is evaluated first, & if it is true then the value of exp2 is return else exp3 is return.

You might also like