Académique Documents
Professionnel Documents
Culture Documents
Dheeraj Mehrotra City Montessori School & Degree College Lucknow (India)
attention@computerscienceexpertise.com www.computerscienceexpertise.com Towards QUALITY IT LITERACY FOR ALL
Concept of OOP
o OOP stands for Object Oriented Programming. It is the latest trend in programming languages supported by C++ and Java and has had the revolutionary success in the race of OOP. The limitations of the Procedure Oriented Programming (POP) languages, have given place to the development of the OOP.
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
Features of OOP
o o Abstraction refers to the act of representing essential features without including the background details or explanations. Encapsulation is an act of wrapping up of data and methods into a single unit (called class). It is one of the most fundamental concepts of the Object Oriented programming. The very insulation of the data from direct access by the program is termed as Data Hiding. Encapsulation, is hence, just a way to implement Abstraction. Inheritance is the process by which objects of one class acquire the properties of objects of another class. This further provides an important extension to the idea of reusability. i.e. a particular programmer can make use of an existing class and without any further modifications, add additional features and capabilities to it, by deriving a new class from the existing one, through inheritance. Polymorphism is the capacity to allow objects having different internal structures to share the same external interface. It is the ability for a message or data to be processed in more than one form.
attention@computerscienceexpertise.com
ADVANTAGES OF OOP
o Elimination of redundant coding system and usage of existing classes through inheritence. o Programs can be developed by sharing existing modules. o Security of data values from other segments of the program through data hiding. o Possibility of co-existence of multiple instances of an object without any interference.
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
History of JAVA
o JAVA is an object oriented programming language. It was developed by Sun Microsystems in 1991 as a part of the research work to develop software for consumer electronics. It was designed to be small, simple and portable across platforms and operating systems, both at the source and at the binary level. It was developed as a full-fledged programming language in which one can accomplish the same sorts of tasks and solve the similar problems that one can in other programming languages, such as C++, BASIC etc.
attention@computerscienceexpertise.com
About JAVA
o Modeled after C++, the Java language was designed to be small, simple, and portable across platforms and operating systems, both at the source and at the binary level.
attention@computerscienceexpertise.com
Why JAVA
o The Java language was developed at Sun Microsystems in 1991 as part of a research project to develop software for consumer electronics devicestelevision sets, VCRs, toasters, and the other sorts of machines you can buy at any department store. Java's goals at that time were to be small, fast, efficient, and easily portable to a wide range of hardware devices. It is those same goals that made Java an ideal language for distributing executable programs via the World Wide Web, and also a general-purpose programming language for developing programs that are easily usable and portable across different platforms. This is the major reason why Java is said to be the most successful platform independent language.
attention@computerscienceexpertise.com
Advantages of Java
o Java as a language has significant advantages over other languages and other programming environments that make it suitable for just about any programming task. Some of them are listed as follows:
attention@computerscienceexpertise.com
o Applets Applets appear in a Web page much in the same way as images do, but unlike images, applets are dynamic and interactive. Applets can be used to create animations, figures, or areas that can respond to input from the reader, games, or other interactive effects on the same Web pages among the text and graphics. Java enabled browsers can successfully run applets and thus give a truly rich experience at the user end.
attention@computerscienceexpertise.com
Platform Independence Platform independence is one of the most significant advantages that Java has over other programming languages, particularly for systems that need to work on many different platforms. Java is platform-independent at both the source and the binary level. Platformindependence is a program's capability of moving easily from one computer system to another. Java binary files called byte-codes are also platform-independent and can run on multiple platforms without the need to recompile the source. Byte-codes are a set of instructions that look a lot like machine code, but are not specific to any one processor. Because of them, compilation happens just once; interpretation occurs each time the program is executed. Java byte-codes help make "write once, run anywhere" possible.
attention@computerscienceexpertise.com
o Simplicity In addition to its portability and objectorientation, one of Java's initial design goals was to be small and simple, and therefore easier to write, easier to compile, easier to debug, and, best of all, easy to learn. Keeping the language small also makes it more robust because there are fewer chances for programmers to make difficult-to-find mistakes. Despite its size and simple design, however, Java still has a great deal of power and flexibility.
attention@computerscienceexpertise.com
Better Cousin of C, C++ Java is modeled after C and C++, and much of the syntax and object-oriented structure is borrowed from the latter. If you are familiar with C++, learning Java will be particularly easy for you, because you have most of the foundation already. Although Java looks similar to C and C++, most of the more complex parts of those languages have been excluded from Java, making the language simpler without sacrificing much of its power. There are no pointers in Java, nor is there pointer arithmetic. Strings and arrays are real objects in Java. Memory management is automatic. To an experienced programmer, these omissions may be difficult to get used to, but to beginners or programmers who have worked in other languages; they make the Java language far easier to learn.
attention@computerscienceexpertise.com
Applets and Applications The most common types of programs written in the Java programming language are applets and applications. If you've surfed the Web, you're probably already familiar with applets. An applet is a program that adheres to certain conventions that allow it to run within a Java-enabled browser. An application is a standalone program that runs directly on the Java platform. A special kind of application known as a server serves and supports clients on a network. Examples of servers are Web servers, proxy servers, mail servers, and print servers. Another specialized program is a servlet. A servlet can almost be thought of as an applet that runs on the server side. Java Servlets are a popular choice for building interactive web applications, replacing the use of CGI scripts. Servlets are similar to applets in that they are runtime extensions of applications. Instead of working in browsers, though, servlets run within Java Web servers, configuring or tailoring the server.
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
Tokens of JAVA
o JAVA comprises of tokens as smallest element of the program. They implement various features within a program. They are as follows : o o o o o Keywords Identifiers Literals Separators Operators
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
declares a class named exampleprogram, where class is the reserve word. Exampleprogram is an identifier as supplied by the programmer.
class exampleprogram {public static void main(string args[ ]) {System.out.println(Hail The World !);}}
This is followed by brace {, which indicates the beginning of the block of executable statements. The third line : public static void main(string args[ ]) defines the function main( ). The declaration of the main() function contains the following keywords: Public, Static and Void. The Public keyword acts as an access specifier which declares the main function to be publically accessible for other classes. The Static definition declares that the very function or method is independent of the entire class and does not figure as a part of any objects of the class. In the above program, the class exampleprogram is unchanging or static, as a state. The term static means unchanging and implicitly final. The Void is a data type modifier which defines the main() function of no return type. The argument of the main() function lies within braces eg. (String args[ ]), here String args[ ] declares an argument parameter as args containing an array of objects of the class type String. attention@computerscienceexpertise.com It specifies that the main method takes an argument that is an object of string.
PDF created with pdfFactory Pro trial version www.pdffactory.com
BlueJ Environment?
To make effective use of BlueJ, one has to eliminate the very un-object-oriented public static void main and replace it with other function name so as to instantiate the object and call the method for execution.
attention@computerscienceexpertise.com
usage of println( ) method"); value of a is "+a); value of b is "+b); value of c is "+c); value of d is "+d);
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
Arithmetic Operators
Operator + * / % Meaning Addition Substraction Product Division Modulus Format a+b a-b a*b a/b a%b
attention@computerscienceexpertise.com
Assignment Operators
Operator/ Operation/ Format/ Meaning = += -= *= /= %= Assignment Add to the variable Subtract from variable Multiply to variable Divide into Modulus of a=10 a+=5 a-=5 a*=5 a/=5 a%=5 10 assigned to the variable a a=a+5 a=a-5 a=a*5 a=a/5 a=a%5
attention@computerscienceexpertise.com
Shorthand Notation
Shorthand Operation sum sum sum sum sum +=10; -=10; /=10; *=10; %=10; Equivalence Operation sum sum sum sum sum = = = = = sum sum sum sum sum + 10; - 10; / 10; * 10; % 10;
(Similarly a variable within an expression on the right can also be used as :) sum +=total; sum = sum + total;
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
IF Statement
This statement acts as a conditional control statement of Java and checks the specified condition. If the given condition is true the given instruction is executed otherwise the instruction is ignored. There are different formats of if statement and is widely used for computation, logical reasoning and decision making within a program. A simple if statement has the following format : if (expression) statement1; where an expression is the condition which if found true executes statement1 otherwise the control comes to the next statement.
attention@computerscienceexpertise.com
IF ELSE Statement
This conditional control statement of Java conducts the execution of the statement if the condition is true and conducts other specified execution if the condition is false. It consists of an if statement, followed by a statement or block of statements, followed by the keyword else, followed by another statement or block of statements. It has the following syntax : if (expression) statement 1; else statement 2; If the expression is true, the statement 1 is executed otherwise the statement 2 is executed. The statement 1 and statement 2 can be a single statement or a block/compound statements.
attention@computerscienceexpertise.com
Switch Statement
It has the following format : switch(expression) { case value1: block1 break; case value2: block2 break; default: block break; }The break statement used at the end of each case acts as an exit point from the
switch statement. It works as a jumping statement and the execution is transferred to the statement following the switch statement. The Default statement is the executing condition in the condition when the value of switch does not match with the case. It does not require any usage of break as it is always the last segment of the switch structure.
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
CONSTRUCTORS
A class is a combination of member variables and methods. The member variables are initialized by a constructor. All Java classes have special methods called constructors that are used to initialize a new object of that type. A constructor is a method with the same name as a class. It is invoked automatically each time an object of that class is initiated. It does not return any value. Java supports method name overloading, so that a class can have any number of constructors, all of which have the same name. Like other overloaded methods, constructors are differentiated from one another by the number or type of their arguments.
attention@computerscienceexpertise.com
CONSTRUCTOR TYPES
a. Overloaded Constructors: Under this format the constructor methods are utilized to initialize the object using the parameter argument. The arguments are passed to the constructors and appropriate constructor is invoked by matching the number, types and order of the arguments specified in each method definition. b. Default Constructors: The default constructors are used to initialize all the variables to a 0 value. As per the procedure in Java all variables must be initialized when the object is initialized. This is compulsory and it is done by use of a constructor.
attention@computerscienceexpertise.com
STRING Manipulation
JAVA offers string manipulation using the functions. In order to declare a string variable the following format is used : Declaration of a String in JAVA: String var-name; var-name = new String(value);
attention@computerscienceexpertise.com
FORMAT: a2=a1.toLowerCase; USAGE: This function/method converts all the characters of the string in lowercase. (ii). toUpperCase; FORMAT: a2=a1.toUpperCase; USAGE: This function/method is used to convert all the characters of the string a1 into uppercase. (iii). replace(); FORMAT: a2 = a1.replace(n,m); USAGE: This function replaces all the occurances of the character n with m in the string a1. (iv). trim(); FORMAT: a2=a1.trim(); USAGE: This function is used to remove all the white spaces at the beginning and end of the string a1.
(v). equals(); FORMAT: a1.equals(a2); USAGE: This method gives true if a1 string is equal to a2. (vi). length(); FORMAT: a1.length(); USAGE: This function returns the length of characters present in the string. (vii). ChartAt(); FORMAT: a1.ChartAt(n); USAGE: This function/method returns nth character of the string a1. (viii). concat(); FORMAT: a1.concat(a2); USAGE: This function concatenates a1 and a2 strings. (ix). substring() FORMAT: a1.substring(n); USAGE: This returns substring starting from the nth character of the string a1.
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
attention@computerscienceexpertise.com
o End of Session.
n Thank you Send your queries at attention@computerscienceexpertise.com Visit: www.computerscienceexpertise.com Towards Quality IT Literacy for All.
attention@computerscienceexpertise.com
For your own Unlimited Reading and FREE eBooks today, visit:
http://www.Free-eBooks.net
To show your appreciation to the author and help others have wonderful reading experiences and find helpful information too, we'd be very grateful if you'd kindly post your comments of this book here.
COPYRIGHT INFORMATION Free-eBooks.net respects the intellectual property of others. When a book's copyright owner submits their work to Free-eBooks.net, they are granting us permission to distribute such material. Unless otherwise stated in this book, this permission is not passed onto others. As such, redistributing this book without the copyright owner's permission can constitute copyright infringement. If you believe that your work has been used in a manner that constitutes copyright infringement, please follow our Notice and Procedure for Making Claims of Copyright Infringement as seen in our Terms of Service here: http://www.free-ebooks.net/tos.html