Object Oriented Assignment Java
Object Oriented Assignment Java
CLO-2
Section: BSSE-2B
Department of Software Engineering, National University of Modern Languages, Islamabad Spring, 2023
Question 1: Explain the following topics in Object oriented programming in Java
with help of short examples.
1. Enums:
An enum is a special "class" that represents a group of constants
(unchangeable variables). To create an enum, use the enum keyword
(instead of class or interface), and separate the constants with a comma.
Note that they should be in uppercase letters:
Example:
Enum Difficulty {
LOW,
MEDIUM,
HIGH,
}
2. Generics:
Generics means parameterized types. The idea is to allow type (Integer,
String, … etc., and user-defined types) to be a parameter to methods,
classes, and interfaces. Using Generics, it is possible to create classes
that work with different data types. An entity such as class, interface, or
method that operates on a parameterized type is a generic entity.
Example:
public class Generic<T1> {
int val;
T1 t1;
public Generic(int val,T1 t1) {
this.val = val;
this.t1 = t1;
}
public static void main(String[] args) {
Generic<String> g1 = new Generic(2,"String 1");
String s1 = g1.t1;
//int s1 = g1.t1 will produce an error here since a string
is provided. If we parse it we get a runtime error.
System.out.println(s1);
}
}
Output:
3. Boxing:
Placing a primitive variable in an object is known as boxing. This allows the
primitive to be used where objects are required. For this purpose Java
provides wrapper classes for each primitive – namely: Byte, Short, Integer,
Long, Float, Double, Character and Boolean.
Example:
public class Boxing {
public static void main(String[] args) {
int a = 5;
Integer b = a;
System.out.println("a: " + a + ", b: " + b);
}
}
Output:
4. AutoBoxing:
Autoboxing refers to the conversion of a primitive value into an object of
the corresponding wrapper class is called autoboxing. For example,
converting int to Integer class. The Java compiler applies autoboxing
when a primitive value is:Passed as a parameter to a method that expects
an object of the corresponding wrapper class.Assigned to a variable of
the corresponding wrapper class.
Example:
import java.util.ArrayList;
Output:
5. Collections:
The Collection in Java is a framework that provides an architecture to store
and manipulate the group of objects. Java Collections can achieve all the
operations that you perform on a data such as searching, sorting, insertion,
manipulation, and deletion. Java Collection means a single unit of objects.
Java Collection framework provides many interfaces (Set, List, Queue, Deque)
and classes (ArrayList, LinkedList,etc).
Example:
import java.util.ArrayList;
6. Varargs Methods:
The varrags allows the method to accept zero or muliple arguments. Before varargs
either we use overloaded method or take an array as the method parameter but it was
not considered good because it leads to the maintenance problem. If we don't know
how many argument we will have to pass in the method, varargs is the better
approach.
Code:
class Varags {
int add(int ... n){
int sum=0;
for (int i : n){
sum += i;
}
return sum;
}
}
public class Test{
public static void main(String[] args) {
Varags v = new Varags();
System.out.println(v.add(2,35,6,3,42,9)); //we can pass as
many arguments as we want.
}
}
Output:
7. File Handeling:
In Java, with the help of File Class, we can work with files. This File Class is
inside the java.io package. The File class can be used by creating an object of
the class and then specifying the name of the file.
The File class from the java.io package, allows us to work with files.To use
the File class, create an object of the class, and specify the filename or
directory name:
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;