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

OOPJ ASSIGNMENT 1

LO1 To apply fundamental programming constructs


1 What is object oriented programming? Elaborate different OOPs concepts with suitable examples
 1 Object-oriented programming is a method used for designing a program using classes and objects. Object-oriented programming is also called
the core of java. Object-oriented programming organizes a program around objects and well-defined interfaces. This can also be characterized as
data controlling for accessing the code. In this type of approach, programmers define the data type of a data structure and the operations that are
applied to the data structure. This implies software development and maintenance by using some of the concepts:
2 Different Oop Concepts In Java
a Abstraction
Abstraction aims to hide complexity from the users and show them only the relevant information. For
example, if you want to drive a car, you don’t need to know about its internal workings. The same is true of Java
classes. You can hide internal implementation details by using abstract classes or interfaces. On the abstract level, you
only need to define the method signatures (name and parameter list) and let each class implement them in their own
way.
Example
i. Hides the underlying complexity of data
ii. Helps avoid repetitive code
iii. Presents only the signature of internal functionality
iv. Gives flexibility to programmers to change the implementation of the abstract behavior
B Encapsulation
Encapsulation allows us to protect the data stored in a class from system-wide access. As its name
suggests, it safeguards the internal contents of a class like a real-life capsule. You can implement encapsulation in Java
by keeping the fields (class variables) private and providing public getter and setter methods to each of them. Java
Beans are examples of fully encapsulated classes.
Example
i. Restricts direct access to data members (fields) of a class.
ii. Fields are set to private
iii. Each field has a getter and setter method

C Polymorphism
Polymorphismrefers to the ability to perform a certain action in different ways. In Java, polymorphism
can take two forms: method overloading and method overriding. Method overloading happens when various methods
with the same name are present in a class. When they are called they are differentiated by the number, order, and
types of their parameters. Method overriding occurs when the child class overrides a method of its parent.
Example
i. The same method name is used several times.
ii. Different methods of the same name can be called from the object.
iii. All Java objects can be considered polymorphic (at the minimum, they are of their own type and instances of
the Object class).

2 What is the Java Virtual Machine? Explain its significance.

 1 The Java Virtual Machine (JVM) is the runtime engine of the Java Platform, which allows any program written in Java or
other language compiled into Java bytecode to run on any computer that has a native JVM.
2 The Java virtual machine is an abstract (virtual) computer defined by a specification. It is a part of java runtime environment.
The garbage-collection algorithm used and any internal optimization of the Java virtual machine instructions (their translation
into machine code) are not specified. The main reason for this omission is to not unnecessarily constrain implementers. Any Java
application can be run only inside some concrete implementation of the abstract specification of the Java virtual machine.[2]

3 Starting with Java Platform, Standard Edition (J2SE) 5.0, changes to the JVM specification have been developed under the Java
Community Process as JSR 924.[3] As of 2006, changes to specification to support changes proposed to the class file format (JSR
202)[4] are being done as a maintenance release of JSR 924. The specification for the JVM was published as the blue book,[5] The preface
states:

3 Explain various Features of Java


 Following are the notable features of Java:

1. Object Oriented

In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

2. Platform Independent

Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform
specific machine, rather into platform-independent byte code. This byte code is distributed over the web and interpreted by the Virtual
Machine (JVM) on whichever platform it is being run on.

3. Simple

Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.

4. Secure

With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on
public-key encryption.

5. Architecture-neutral

Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many
processors, with the presence of Java runtime system.

LO2 To illustrate the concept of packages, classes and objects.


1 Explain the concept of package in Java with suitable example
a) Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are
used for:

i. Preventing naming conflicts. For example there can be two classes with name Employee in two
packages, college.staff.cse.Employee and college.staff.ee.Employee

ii. Making searching/locating and usage of classes, interfaces, enumerations and annotations easier

iii. Providing controlled access: protected and default have package level access control. A protected
member is accessible by classes in the same package and its subclasses. A default member (without any
access specifier) is accessible by classes in the same package only.

iv. Packages can be considered as data encapsulation (or data-hiding).

b) All we need to do is put related classes into packages. After that, we can simply write an import class from
existing packages and use it in our program. A package is a container of a group of related classes where some of
the classes are accessible are exposed and others are kept for internal purpose.
c) We can reuse existing classes from the packages as many time as we need it in our program.

d) Examples of packages are:


I. These packages consist of a large number of classes which are a part of Java API.Some of the commonly
used built-in packages are:
II.
java.lang: Contains language support classes(e.g classed which defines primitive data types, math
operations). This package is automatically imported.
III.
java.io: Contains classed for supporting input / output operations.
IV.
java.util: Contains utility classes which implement data structures like Linked List, Dictionary and
support ; for Date / Time operations.
V.
java.applet: Contains classes for creating Applets.
VI.
java.awt: Contain classes for implementing the components for graphical user interfaces (like button ,
;menus etc).
VII.
java.net: Contain classes for supporting networking operations.

2 What are different types of Java Input/Ouput functions?

1) java I/O (Input and Output) is used to process the input and produce the output.
2) Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes required for
input and output operations.
3) We can perform file handling in Java by Java I/O API.
4) A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a stream because it is like a stream of
water that continues to flow.
5) In Java, 3 streams are created for us automatically. All these streams are attached with the console.

I. System.out: standard output stream


II. System.in: standard input stream
III. System.err: standard error stream

3 What is a constructor? Explain constructor overloading with a suitable example.

a) A constructor in Java is a block of code similar to a method that's called when an instance of an object is
created. Unlike methods, constructors are not considered members of a class. A constructor is called
automatically when a new instance of an object is created.

b) In Java, we can overload constructors like methods. The constructor overloading can be defined as the concept
of having more than one constructor with different parameters so that every constructor can perform a
different task.
c) Consider the following example

d) program, in which we have used different constructors in the class.

public class Student {


int id;
String name;

Student(){
System.out.println("this a default constructor");
}

Student(int i, String n){


id = i;
name = n;
}

public static void main(String[] args) {


//object creation
Student s = new Student();
System.out.println("\nDefault Constructor values: \n");
System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);

System.out.println("\nParameterized Constructor values: \n");


Student student = new Student(10, "David");
System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);
}
}

4 Compare the static and public methods in Java.

A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method
can be called without the instance or object of that class
.
What is an non-static method ?:

Every method in java defaults to a non-static method without static keyword preceding it . Non-static methods can
access any static method and static variable, without creating an instance of the object.
Below are the various important differences among these:

1. Accessing members and methods:


 In static method, the method can only access static data members and static methods of another class
or same class but cannot access non-static methods and variables. Also a static method can rewrite the
values of any static data member.

 In non-static method, the method can access static data members and static methods as well as non-
static members and method of another class or same class, also can change the values of any static data
member.

2. Calling process:
 In static method, The memory of a static method is fixed in the ram, for this reason we don’t need the
object of a class in which the static method is defined to call the static method. To call the method we
need to write the name of the method followed by the class name.

 In non-static method, the memory of non-static method is not fixed in the ram, so we need class object
to call a non-static method. To call the method we need to write the name of the method followed by
the class object name.

L03 To elaborate the concept of strings, arrays and vectors.

1 Explain various functions used to manipulate strings.

1. toLowerCase()

public String toLowerCase();

This function changes the case of the given string to the lower case. We can use this function at the time of
string to handle the ignore case with the compareTo() function itself.

2. toUpperCase()

public String toLowerCase();

This function remains the same as toLowerCase(), but it changes the case to uppercase.

3. Substring()

String substring(int StartIndex, int LastIndex)

In the example code area, we will see the use of this function with the index as an input and the output as well.
For this, we need to have a string upon which we will be performing this operation. We can use the position
from where we want the string in place of the StartIndex, and we can replace the LastIndex with the end index
to where we want the string.

4. trim()

public String trim()

Again, this is a string manipulation function we can use to remove the unwanted space from a given string.

5 Length()

Public string Length()

This function is used to find the length of the string

6 charAt()

Public string charAt()


This functioinis used to find the character at the index position passed to the function

2 Write note on the following: i. Arrays ii. Vectors

I. Array

a) Normally, an array is a collection of similar type of elements which has contiguous memory location.
b) Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are
stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only
a fixed set of elements in a Java array.
c) Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st
index and so on.
d) Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to use the sizeof
operator.Difference between JDK, JRE, and JVM
e) In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and implements
the Serializable as well as Cloneable interfaces. We can store primitive values or objects in an array in Java. Like
C/C++, we can also create single dimentional or multidimentional arrays in Java.
f) Moreover, Java provides the feature of anonymous arrays which is not available in C/C++

ii. Vectors

a) The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully
compatible with collections. It is found in the java.util package and implements the List interface, so we can use
all the methods of List interface here.

b) Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains
components that can be accessed using an integer index

c) They are very similar to ArrayList but Vector is synchronized and has some legacy method that the collection
framework does not contain.

d) It also maintains an insertion order like an ArrayList but it is rarely used in a non-thread environment as it
is synchronized and due to which it gives a poor performance in adding, searching, delete and update of its
elements.

e) The Iterators returned by the Vector class are fail-fast. In the case of concurrent modification, it fails and throws
the ConcurrentModificationException.

You might also like