You are on page 1of 12

Manipal

BCA REVAMPED PROGRAMME


IV SEMESTER
ASSIGNMENT
Name

VELMURUGAN C
____________________________________________________

Registration No.

531210112
____________________________________________________

Learning Center

KUWAIT EDUCATIONAL CENTER


____________________________________________________

Learning Center Code

2527
____________________________________________________

Course/Program

BCA
____________________________________________________

Semester

IV Semester
____________________________________________________

Subject Code

BC0047
____________________________________________________

Subject Title

JAVA PROGRAMMING
____________________________________________________

Date of Submission

26.02.2014
____________________________________________________

Marks Awarded

:
____________________________________________________

Average marks of both assignments

_________________________________________________

_______________________________________________

Signature of Center Coordinator

Signature of Evaluator

Directorate of Distance Education


Sikkim Manipal University
II Floor, Syndicate House, Manipal 576 104

SMU

_________________________________________

Sikkim Manipal University

Directorate of Distance Education

Question 1: What are the different types of Operators available in


java?
Ans.: The different types of Operators available in JAVA are:
Arithmetic Operators:
The following arithmetic operations can be performed in Java
Opera
tor
+
*
/
%

Use

Meaning

op1+op2
op1-op2
op1*op2
op1/op2
op1 % op2

Adds op1 and op2


Subtracts op2 from op1
Multiplies op1 and op2
Divides op1 by op2
Computes the remainder of dividing op1 by op2

Increment and Decrement Operators:

The increment operator is ++ and decrement operator is .


This is used to add 1 to the value of a variable or subtract 1 from
the value of a variable.
These are placed either before or after the variable name.
When the operator ++ is placed after the variable name, first
assignment of the value of the variable takes place and then
the value of the variable is incremented. This operation is also
called post increment.
When the operator is placed before the variable, first
increment of the variable takes place and then the assignment
occurs. This operation is also called as pre increment.
If there is no assignment and only the value of variable has to
be incremented or decremented then placing the operator
after or before does not matter.

Comparison Operators:
Op

Meaning

Example

==

Equal

op1 = = op2

!=

Not Equal

op1 != op2

<

Less than

op1 < op2

>

Greater than

op1 > op2

<=

Less than or equal

op1 <= op2

>=

Greater than or equal op1 >= op2

Logical Operators:
Op

Use

Return true if

&&

op1 && op2

If both are true

||

op1 || op2

If one is true

!op

Op is false

&

Op1 & op2

If both are true , always evaluates op1


and op2

Op1 | op2

If one is true, always evaluates op1 and


op2

Operator Precedence
If more than one operator is used in an expression, JAVA establishes
Operator Precedence to determine the order.
Addition and subtraction has the same priority. When the operators are
having the same priority, they are evaluated from left to right in the order
they appear in the expression.
In general the following order is followed when evaluating an expression.

Increment and decrement operations.


Arithmetic operations
Comparisons
Logical operations
Assignment operations

For example: 20+10*10-15/5


In the above expression multiplication and division operation have higher
priority than the addition and multiplication.

Question 2: Explain the types of relationship in java.


Ans.:
Inheritance is one of the cornerstones of object-oriented programming
because it allows the creation of hierarchical classifications. Using
inheritance we can create a general class. This class can be inherited
by other, more specific classes, each adding those things that are
unique to it. In the terminology of Java, a class that is inherited is called
a super class. The class that does the inheriting is called a subclass.
Therefore, a subclass is a specialized version of a super class. It
inherits all of the instance variables and methods defined by the super
class and add its own, unique elements.
Relationships are classified as follows:

A
A
A
A

Kind-Of relationship.
Is-A relationship.
Part-Of-relationship.
Has-A relationship.

Consider the similarities and differences among the


objects/classes: Automobile, Ford, Porsche, Car and Engine.

following

We can make the following observations:

A truck is a kind of an automobile.


A car is a (different) kind of an automobile.
An engine is a part of an automobile.
An automobile has an engine.
The ford is a car.

A-Kind-Of Relationship:
We can take the example of a Human Being and an Elephant. Both are
'KIND-OF' mammals. As both are mammals they share the attributes and
behaviours of mammals. Hence Human Being and Elephants are subset
of Mammals class.
The following figure depicts the relationship between the mammals and
Human Being classes:

A Kind-Of
Human
Being

Mammals

Is-A Relationship:
Let us take an instance of the human being class ASHOK, who 'IS-A'
human being and, therefore, a mammal.
The following figure depicts the 'is-A' relationship:
Is-A
Ashok

A Kind-Of
Human Being

Mammals

Is-A

Has-A Relationship / Part of Relationship:


A human being has a heart which represents 'has-a' relationship. Heart is
a 'part-of' human being which represents 'part-of' relationship.
The following diagram depicts the relationship between a human being
and a heart:
Has-A

Human
Being

Heart
Part-Of

Question 3: What are exception classes? Explain the common


exceptions in Java.
Ans.:
Exception Classes:

The term exception denotes an exceptional event. It can be defined as an


abnormal event that occurs during program execution and disrupts the
normal flow of instruction.
Error-handling becomes a necessity when you develop applications that
need to take care of unexpected situations.
The unexpected situations that may occur during program execution are:

Running out of memory

Resource allocation errors.

Inability to find a file.

Problems in network connectivity.

The errors by the programmer might be due to situations that might


occur while running the program.

The errors might be due to a programming mistake, bad input data,


corrupts files or problems in physical devices.

It is necessary to take care about these situations.

The possible remedies could be to notify the user of an error. Save the
work environment; allowing exiting from the program without adversely
affecting the other programs in execution.
The class at the top of the exception class hierarchy is called Throwable.
Two classes are derived from the Throwable class- Error and Exception.
The Exception class is used for the exceptional conditions that have to be
trapped in a program. The Error class defines a condition that does not
occur under normal circumstances.
The Error class is used for
catastrophic failures such as VirtualMachineError. These classes are
available in the java.lang package.
Common Exceptions:
Java has several predefined exceptions. The most common exceptions
that you may encounter are described below:
Arithmetic Exception
This exception is thrown when an exceptional arithmetic condition has
occurred. For example, a division by zero generates such an exception.
NullPointer Exception

This exception is thrown when an application attempts to use null where


an object is required. An object that has not been allocated memory holds
a null value. The situations in which an exception is thrown include:
Using an object without allocating memory for it.
Calling the methods of a null object.
Accessing or modifying the attributes of a null object.
ArrayIndexOutOfBounds Exception
The exception ArrayIndexOutOfBounds Exception is thrown when an
attempt is made to access an array element beyond the index of the
array. Example: if you try to access the eleventh element of an array
thats has only ten elements, the exception will be thrown.
Question 4: Explain the methods of InputStream class and
OutputStream class.
Ans.: All programs accept input from a user, process it and produce an
output. Therefore, all programming languages support input and output
operations. Java provides support for input and output through the classes
of the java.io package.
Java handles all input and output in the form of streams. A stream is a
sequence of bytes traveling from a source to a destination over a
communication path. If a program writes o steam, it is the streams
source. Similarly, if it is reading from a stream, it is the streams
destination. Streams are powerful because they abstract the details of
input/output (I/O) operations.
The two basic streams used are the input and the output streams.
Each stream has a particular functionality. You can only read from an input
stream and conversely, you can only write to an output stream. Some
streams are classified as node stream because they read from or write to
a specific place like a disk file or memory. Some streams are classified as
filters. Filters are used to read data from one stream and write it to
another stream.
The two major classes for byte streams are InputStream and
OutputStream. These abstract classes are subclassed to provide a variety
of I/O capabilities. The InputStream class lays the foundation for the input
class hierarchy, whereas the OutputStream class lays the foundation for
the output class hierarchy.
The diagrams given below represent the hierarchy of the two streams:

InputStream

SequenceInputStream

ByteArrayInputStream

FileInputStream

ObjectInputStream

PipedInputStream

FilterInputStream

OutputStream
PushbackInputStream

BufferedInputStream

DataInputStream

ObjectOutputStream

ByteArrayOutputStrea
m

FileOutputStream

PipedOutputStream

FilterOutputStream

BufferedInputStream

DataInputStream

JDK 1.1 introduces the Reader and the Writer classes for Unicode I/O.
These classes support internationalization.
The hierarchy of these classes is given below

Reader

BufferedReader

CharArrayReader

InputStreamReader

StringReader

PipedReader

FilterReader

Reader Class Hierarchy

Writer

BufferedWriter

StringWriter

CharArrayWriter

PipedWriter

PrintWriter

FilterWriter

OutputStreamWriter

Writer Class Hierarchy


Question 5: Write a Java program to find the sum of 1+3+5+.
,for 10 terms in the series.
Ans.:
The usage of for loop is as follows:
for (initial
statement;

statement;

termination

condition;

increment

instruction)

When multiple statements are to be included in the for loop, the


statements are included inside flower braces.
for (initial statement; termination condition; increment instruction)
{
statement1;
statement2;
}

public class sum


{
public static void main (String args[])
{
int total = 0;
for (int i = 1; i<=20; i+=2)
{
total += i;

}
System.out.println("Total is " + total);
}
}

OUTPUT : 400

Question 6: Write a program in Java to check whether a given


year is leap year or not.
Ans.:
A leap year is a year which is divisible by 4, with the exception that if the year is
divisible by 100, then it should also be divisible by 400.
Given below is a Java program which finds whether a given year is a leap year or not.
1 public class LeapYear {
2
3

public void find(int year) {

if (year % 100 == 0) {

5
6
7
8
9
10
11
12
13
14
15
16
17
18 }

if (year % 400 == 0) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
} else {
if (year % 4 == 0) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
}
}

We first check if the year is divisible by 100 or not.


If the year is divisible by 400, we check if it is divisible by 400. If yes, we
display a message saying that it is a leap year. Otherwise, we print a
message saying that it is not a leap year.
If the year is not divisible by 400, we check if it is divisible by 4. If yes, we
print a message saying that it is a leap year. Otherwise, it is not a leap
year.

Sample Executions :
Input :
year = 800
Output :
800 is a leap year
Input :
year = 700
Output :
700 is not a leap year
Input :
year = 34
Output :
34 is not a leap year
Input :
year = 2013
Output :
2013 is not a leap year

You might also like