Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Understanding the Features, Environment, and Development Process, Assignments of Java Programming

An overview of Java programming, including its features such as simplicity, security, and portability. It also covers the Java environment and the process of building and running Java applications. Java is a popular programming language known for its ability to create cross-platform applications, and this document serves as a useful resource for both beginners and experienced programmers.

Typology: Assignments

2020/2021

Uploaded on 04/12/2021

jvnaveenbabu
jvnaveenbabu 🇮🇳

1 document

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 | P a g e
UNIT - I
1. Creation of Java (History of Java)
Java is an Object-Oriented programming Language developed by James
Gosling and his colleagues at Sun Microsystems in 1991. It took 18 months to
develop the first working version. This language was initially called “Oak”, but
was renamed “Java” 1995. Java is intended to be compiled to a bytecode,
which is then run by a Java Virtual Machine (JVM). The original motto for
java was not the Internet. The primary motivation was the need for a platform-
independent language that could be used to create software to be embedded in
various consumer electronic devices.
Sun Microsystems released the first version 1.0 of java in 1995. It was
followed by java 1.02 two months later. In this version database connectivity
and distributed object features are added. Version 1.1 released in 1997, added a
robust event model, awt enhancements, jar files, RMI (Remote Method
Invocation) and Beans. Version 1.2 released at the end of 1998, has the feature
of Swing, Java 2D, Accessibility Drag and Drop, JAVA IDL (Java Interface
Definition Language), JNI (Java Native Interface) enhancements and JVMDI
(Java Virtual Machine Debug Interface). JDK 1.2 is called Java 2 platform.
Present version of java is JAVA SE 10.
2. Why Java is Important to the Internet?
The reason for this is quite simple. Java expands the universe of objects that
can move freely in cyberspace. In a network, two very broad categories of
objects are transmitted between the server and your personal computer: First
type of object can be transmitted to your computer: Passive information and
dynamic, active programs. For example, when you read your e-mail, you are
viewing passive data. Even when you download a program, the program’s code
is still only passive data until you execute it. Second type of object can be
transmitted to your computer: a dynamic, self executing program. Such a
program is an active agent on the client computer, yet is initiated by the server.
For example, a program might be provided by the server to display to display
properly the data that the server is sending.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Java Programming: Understanding the Features, Environment, and Development Process and more Assignments Java Programming in PDF only on Docsity!

UNIT - I

  1. Creation of Java (History of Java) Java is an Object-Oriented programming Language developed by James Gosling and his colleagues at Sun Microsystems in 1991. It took 18 months to develop the first working version. This language was initially called “Oak”, but was renamed “Java” 1995. Java is intended to be compiled to a bytecode, which is then run by a Java Virtual Machine (JVM). The original motto for java was not the Internet. The primary motivation was the need for a platform- independent language that could be used to create software to be embedded in various consumer electronic devices.

Sun Microsystems released the first version 1.0 of java in 1995. It was followed by java 1.02 two months later. In this version database connectivity and distributed object features are added. Version 1.1 released in 1997, added a robust event model, awt enhancements, jar files, RMI (Remote Method Invocation) and Beans. Version 1.2 released at the end of 1998, has the feature of Swing, Java 2D, Accessibility Drag and Drop, JAVA IDL (Java Interface Definition Language), JNI (Java Native Interface) enhancements and JVMDI (Java Virtual Machine Debug Interface). JDK 1.2 is called Java 2 platform. Present version of java is JAVA SE 10.

  1. Why Java is Important to the Internet? The reason for this is quite simple. Java expands the universe of objects that can move freely in cyberspace. In a network, two very broad categories of objects are transmitted between the server and your personal computer: First type of object can be transmitted to your computer: Passive information and dynamic, active programs. For example, when you read your e-mail, you are viewing passive data. Even when you download a program, the program’s code is still only passive data until you execute it. Second type of object can be transmitted to your computer: a dynamic, self – executing program. Such a program is an active agent on the client computer, yet is initiated by the server. For example, a program might be provided by the server to display to display properly the data that the server is sending.

Java Applets: An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser.

Security: Users are worried about their systems with viral infection and malicious programs. Java answers these issues by providing a “firewall” between a networked application and your computer.

Portability: Many types of computers and operating systems are in use throughout the world - and many are connected to the Internet. For programs to be dynamically downloaded to all platforms connected to the Internet, some means of generating portable executable code is needed. Java provides portability.

  1. Features of Java (Java Buzzwords) i. Simple: Java is a simple language that can be learned easily, even if you have just stared programming. ii. Secure: Java program can be executed in an environment that disallows the viruses and malicious programs. iii. Portable: Java programs can run on multiple platforms by a special program called the Java Virtual Machine. Hence only one version of the application need to be developed and maintained for all platforms. iv. Object-oriented: Java is purely Object-Oriented and provides Abstraction, Encapsulation, Inheritance and Polymorphism. An object is an instance of class consists of variables and related methods. v. Robust: Java is a reliable language. It does not have permission to access all of your computer memory and also provide exceptions and error handling, so java program can’t cause a crash. Java programs are less prone to error. vi. Multithreaded: Multithreading is the ability of an application to perform multiple tasks at the same time. A simple program does many tasks simultaneously. vii. Architecture neutral: Programmer can write programs in java without knowing the systems architecture. Java compiler produces the byte code

hierarchical classification. This is possible by deriving a new class called derived class from an existing class called base class. The new class will have the combined features of both the classes. Polymorphism : Polymorphism is another important OOP concept. Polymorphism means the ability of giving the same name to different methods. The individual methods may implement different tasks but can be differentiated by the type of arguments passed to them.

  1. How java differs from C++ Java is a true Object Oriented Language while c++ is basically c with Object Oriented extension. The following C++ features are intentionally omitted from Java or significantly modified. i. Java does not support operator overloading. ii. Java does not include structure and union. iii. Java does not have template classes as in c++. iv. Java does not support multiple inheritances of classes. This is accomplished using a new feature called “interface”. v. Java does not use pointers vi. There are no header files and delete operator in Java. vii. Java is platform independent. viii. Java does not allow default arguments.
  2. Java Environment Java environment includes a large number of development tools and hundreds of classes and methods. The development tools are part of the system known as Java Development Kit (JDK) and classes and methods are part of the Java Standard Library ( JSL ), also known as the Application Programming Interface (API).

JDK: The JDK comes with a collection of tools that are used for developing and running java programs. JDK includes:  appletviewer – Enable us to run java applets with actually using java compatible browser.  javac – The java compiler, which translates java source code to bytecode file that the interpreter can understand.

 java – java interpreter interpret the bytecode and run the instruction.  javap – Java disassemble, which enables us to convert bytecode file into a program description.  Rmic – create skeletons and stubs for remote method invocation.  javah – Produces header files for use with native methods.  javadoc – creates HTML format documentation from java source code files.  jdb – Java debugger, which helps us to find errors in our programs.

JSL or API: Java Standard Library (JSL) or Application Programming Interface (API) includes hundreds of classes and methods grouped into several functional packages. JDK 1.2 has 58 packages. The most commonly used packages are:

 java.lang – It contains the main language support class deals with Object wrappers, Strings, Multithreading and basic features of java.  java.util – It provides classes that support collections, date, time and calendar operations, parsing and basic event processing.  java.io – This package provides reading and writing data in the form of streams and device independent file services.  java.awt – This package provides classes for creating GUI programs. It contains, java’s Abstract Windowing Toolkit (AWT) etc.  java.applet – This package includes a set of classes with support for HTML embedded java applets.  java.net – It combines the classes supporting low-level internet programming and www.  javax.swing – It offers classes for components, higher level controls and pluggable look and feel.

programmer would like to refer to at a later stage. Comments must explain why and what of classes and how of algorithms. The comment lines must be in between /** and */.

Package statement: The first statement allowed in a java is a package statement. This statement declares a package name and informs the compiler that the classes defined here belong to this package. For example: package colleges;

Import Statements: This section contains all import packages. These statements instruct the interpreter to load the respected classes from the packages. Example: import java.util.*;

Interface statements: An interface is like a class but includes a group of method declarations. This is an optional section and is used only when we wish to implement inheritance feature in the program.

Class Definitions: A java program may contain multiple class definitions. Classes are the primary and essential elements of a java program. These classes are used to map the objects of real-world problems. The number of classes used depends on the complexity of the problem.

Main method class: Since every java standard-alone program requires a main method as its starting point, this class is the essential part of a java program. The main method creates objects of various classes and establishes communications between them. On reaching the end of main, the program terminates and the control passes back to the operating system.

  1. Java tokens The smallest individual units in a program are known as tokens. A token is consists of java character set. The compiler recognizes them for building up expressions and statements. The java language includes 5 types of tokens. They are: i) Keywords ii) Identifiers iii) Literals iv) Operators v) Separators.

Keywords: keywords are essential part of a language definition. They implement specific feature of the language. Java has reserved 50 words as keywords. Understanding of all these words is important for java programs. Examples: class, import, double, for etc.

Identifiers: Identifiers are programmer designed tokens. They are used for naming classes, methods, variables, objects etc. Java identifiers follow the following rules. a) They can have alphabets, digits and the underscore and dollar sign characters. b) They must not begin with a digit. c) Uppercase and lowercase letters are distinct. d) They can be of any length.

Literals: Literals in java are a sequence of characters that represent constant values to be stored in variables. Java specifies 5 types of literals. They are: Inters literals, float point literals, character literals, string literals and Boolean literals.

Operators and separators: An operator is a symbol that takes one or more arguments and operates on them to produce a result. Operators are of many types: Arithmetic, Relational, Logical, Assignment, Increment etc. Separators are symbols used to indicate groups. For example, ( ), {},[ ] etc.

  1. Data types Java defines eight primitive types of data: byte, short, int, long, char, float, double and boolean. These can be put in four groups. i. Integers type ii. Floating-points type. iii. Characters type. iv. Boolean type.

Integers : Java defines four integer types: byte, short, int and long. All these are signed values. Java does not support the concept of unsigned integers. The width and ranges of these integers vary widely, as shown in this table.

Characters : In java, the data type used to store characters is char. Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. For this purpose, it requires 16 bits. Thus, in java char is a 16 bit type. The range of a char is 0 to 65,536. There are no – ve chars.

Boolean : For logical values, java has a primitive type called boolean. It can have only one of two possible values, true or false. This is the type returned by all conditional expressions that govern the control statements. For example the declaration of variable b of type Boolean is: boolean b;

  1. Variables, declaring variables, Dynamic initialization, scope and life time. Variables are identifiers used to assign names to memory locations. A memory location holds a value with which we are working. A variable can be used to store a value of any data type. The declaration statement defines the type of variable. The general form of declaration of a variable is:

type identifier [= value] [, identifier [= value]... ];

Example: int a=7, b=56; float ave; int i;

Java allows variables to be initialized dynamically, using any valid expression at time the variable is declared. For example, double c = Math.sqrt(aa + bb);

Java allows variables to be declared within any block. A block defines a scope. A scope determines what objects are visible to other parts of your program. It also determines the life time of those objects. Variables are created when their scope is entered and destroyed when their scope is left. This means that a variable will not hold its value once it has gone out of scope. Thus the life time of a variable is confined to its scope.

  1. Type conversion and casting. The conversion of data from one data type to another is called type conversion or type casting. This is classified in two types: Implicit and explicit. i. Implicit type casting/automatic type casting: Implicitly java provides type casting. Java automatically promotes values to a higher data type to prevent any loss of information. For example: byte b=100; int a = b; Here the byte value of b is automatically promoted to higher data type int and then stored in variable a. This is done by java automatically. So, it is called automatic type conversion or implicit type conversion. Here there is no data loss. This is also called widening conversion.

ii. Explicit type casting: To create a conversion between two incompatible types, you must use a cast. A cast is simply an explicit type conversion. For example, what if you want to assign an int value to byte variable? This conversion will not be performed automatically, because a byte is smaller than an int. This kind of conversion is called a narrowing conversion. Example: int a=5; byte b = (byte) a; Here the value of a is explicitly converted into byte type before storing into the variable b.

Type casting Rules a. A Boolean variable cannot be type cast. b. If the range is narrowed, an explicit type cast is required. c. If the conversion increases the range of data that can be store, it is some time known as promotion or widening. d. If any of the operand type is double, the other one will be converted to double. This works similarly long, float, int, short, and byte. e. Java automatically promotes byte or short operand to int.

Two Dimensional Array (or) Multidimensional Array: In java, multidimensional arrays are actually arrays of arrays. Java allows us to define tables of values by using two dimensional arrays. For example, the following declares a two dimensional array variable called twod with 4 rows and 5 columns. int twod[ ][ ] = new int[4][5]; (or) int twod[ ][ ] = new int [4] [ ]; twod[0] = new int [5]; twod[1] = new int [5]; twod[2] = new int [5]; twod[3] = new int [5];

Two dimensional arrays can be initialized during declarations. By default all memory locations are initialized with zeros. We can do it in one of two ways. For example, twod is a 2 by 3 two dimensional array initialized in two ways as follow:

int twod[2 ] [3 ] = {12, 23, 9, 45, 22, -9}; // Initializing with specific values.

int twod[2 ] [3 ] ={{12, 23, 9},{ 45, 22, -9}}; // Initializing with same values.

Two Dimensional array with variable sizes int twod[ ][ ] = new int [4] [ ]; twod[0] = new int [1]; twod[1] = new int [2]; twod[2] = new int [3]; twod[3] = new int [4];

  1. Operators in Java Java provides a rich operator environment. These can be divided into the following four groups: i. Arithmetic Operators. ii. Bitwise Operators. iii. Relational Operators. iv. Logical Operators.

Arithmetic Operators : Arithmetic operators are used in mathematical expressions in the same way as used in algebra. The operands of the arithmetic operators must be of a numeric type. The following table lists the arithmetic operators. Lets a, b and c are integer type operands.

Operator Description Example

  • Addition c = a + b
  • Subtraction c = a - b
  • Multiplication c = a* b / Division c = a / b % Modulus c = a % b ++ Increment a++ (or) a = a+ += Addition assignment a += b (or) a = a+b -= Subtraction assignment a -= b (or) a = a-b *= Multiplication assignment a = b (or) a = ab /= Division assignment a /= b (or) a = a/b %= Modulus assignment a %= b (or) a = a%b -- Decrement a-- (or) a = a-

Bitwise Operators : Java defines several bitwise operators which can be applied to the integer types: long, int, short, char and byte. These operators act upon the individual bits of their operands. They are summarized in the following table.

Operator Description Example ~ Bitwise Unary NOT c = ~ a & Bitwise AND c = a & b | Bitwise OR^ c = a| b ^ Bitwise exclusive OR c = a ^ b

Shift right c = a >> 2 << Shift left c = a << 2 &= Bitwise AND assignment a&=b (or) a = a & b | = Bitwise OR assignment a | =b (or) a = a | b ^= Bitwise exclusive ORassignment A^=b (or) a = a ^ b = Shift right assignment a>>=b (or) a =a>>b <<= Shift left assignment A<<=b (or) a =a<<b

  1. Control Statements Programming languages use control statements to cause the flow of execution of statements. In Java, the control statements can be divided into three categories: Selection, Iteration and Jump.
  2. Explain about Selection or Decision control statements with an example.

Selection: Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable. Java support two selection statements: if and switch. i. If – The if statement is java’s conditional branch statement. It can be used to route the program execution through two different paths. It is also called two-way selection statement. The general form are: a) If (condition) {statement1; statement2;

....... .. ;} else {statement1; statement2; ....... .. ;} b) Nested ifs – A nested if is an if statement within another if or else. Nested ifs are very common in programming. Example, if (i==10) { if (j < 20) a=b; if (K>100) c= d; else a=c; } else a = d;

c) The if–else–if Ladder: A sequence of nested ifs is the if-else-if ladder. It looks like this: if (condition) statement1; else if (condition) statement2; else if (condition) statement3;

......... else { statement n;}

ii. Switch – The switch statement is a multi-way selection statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. The general form of a switch statement: Switch ( expression) { case value1: sequence of statements separated by semicolon break; case value2: sequence of statements separated by semicolon break;

... .............. Case value n: sequence of statements separated by semicolon break; default: sequence of statements separated by semicolon if any

}

Exmple:

switch (i) { case 1: System.out.print(“January”); break; case 2: System.out.print(“February”); break; case 3: System.out.print(“March”); break; case 4: System.out.print(“April”); break; case 5: System.out.print(“May”); break; case 6: System.out.print(“June”); break; case 7: System.out.print(“July”); break; case 8: System.out.print(“August”); break case 9: System.out.print(“September”);

executed once again. This process is repeated until the test condition value is false. On exit, the program continues with the statement immediately after the body of the loop.

Example: Whiel (i < 10) { i++; System.out.print(“While statement”); }

do .. while – The do while is an exit-controlled loop statement. The control executes the body of the loop and tests the condition at the bottom of the loop, if it is true then the body of the loop is executed another time. This process is repeated until the test condition value is false. On exit, the program continues with the statement immediately after the body of the loop. The body of the loop is always executed at least once.

Example: do { i++; System.out.print(“While statement”); } Whiel (i < 10)

  1. Explain about Jump control statements with an example. Java supports three jump statements: break, continue and return. These statements transfer control to another part of your program.

break – An early exit from a loop can be accomplished by using the break statement. When the break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. When the loops are nested, the break will only exit from the loop containing it. That is, the break will exit only a single loop.

continue – java support another jump statement called continue statement. Unlike the break which causes the loop to be terminated, the continue causes the loop to be continued with the next iteration after skipping any statements in between. The format of the continue statement is simply continue;.

return – The return statement is used to explicitly return from a method. It causes program control to transfer back to the caller of the method. The general form of return statement is: return;.

  1. Operator precedence: Operator precedence defines the order in which various operators are evaluated. The table below shows the order of precedence for java operators, from highest to lowest.

Highest Precedence ( ) [ ]. ++ -- ~!

  • / %

<< = < <= = = != & ^ | && || ?: = op= Lowest Precedence