0% found this document useful (0 votes)
48 views5 pages

Object Oriented Assignment Java

This document contains an assignment submission for a software engineering class. It addresses 7 topics in object oriented programming in Java: 1) Enums, 2) Generics, 3) Boxing, 4) AutoBoxing, 5) Collections, 6) Varargs Methods, and 7) File Handling. For each topic, it provides a short explanation and code example to illustrate the concept. The assignment was submitted by Zain Ali of section BSSE-2B to their instructor Ms. Iqra Shahzad on May 11, 2023.

Uploaded by

Zain Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
48 views5 pages

Object Oriented Assignment Java

This document contains an assignment submission for a software engineering class. It addresses 7 topics in object oriented programming in Java: 1) Enums, 2) Generics, 3) Boxing, 4) AutoBoxing, 5) Collections, 6) Varargs Methods, and 7) File Handling. For each topic, it provides a short explanation and code example to illustrate the concept. The assignment was submitted by Zain Ali of section BSSE-2B to their instructor Ms. Iqra Shahzad on May 11, 2023.

Uploaded by

Zain Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Assignment 2

CLO-2

Submitted to: Ms. Iqra Shahzad

Submitted by: Zain Ali (FL-21530)

Section: BSSE-2B

Dated: 11th May 2023

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,
}

To access the enums, you can use the dot:


Difficulty level1 = Difficulty.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;

public class AutoBoxing {


public static void main(String[] args) {
ArrayList<Integer> num = new ArrayList<>(); //here any int
stored in arraylist gets converted into Integer object
num.add(7);
num.add(9);
System.out.println("Arraylist Num :" + num);
}
}

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;

public class AutoBoxing {


public static void main(String[] args) {
ArrayList obj = new ArrayList(); //here any int stored in
arraylist gets converted into Integer object
obj.add("Hello World");
obj.add("5");
obj.add("$");
System.out.println("Arraylist :" + obj);
}
}
Output:

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;

public class FileHandling{


public static void main(String[] args) {
//Creating a new file
File myfile = new File("contents.txt");
try {
myfile.createNewFile();
} catch (IOException e) {
System.out.println("cant create file");
throw new RuntimeException(e);
}
//Writing a file
try {
FileWriter fileWriter = new FileWriter("contents.txt");
fileWriter.write("Hello World!");
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
//Reading a file
try {
Scanner sc = new Scanner(myfile);
while (sc.hasNextLine()){
System.out.println(sc.nextLine());
}
sc.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
//Deleting a file
if (myfile.delete()){
System.out.println("File " + myfile.getName() + " has
been deleted successfully");
} else {
System.out.println("Cant delete the file :" +
myfile.getName());
}
}
}
Output:

You might also like