0% found this document useful (0 votes)
17 views

OOT Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

OOT Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Index

S.No. List of Programs Signature


Create Factorial of a number using command-line
1
arguments

Write a java program to display the employee


2 details using Scanner class

Write a java program that checks whether a given


3 string is palindrome or not

Create Java programs using inheritance and


4 polymorphism

Implement error-handling techniques using


5 exception handling and multithreading

Write a JAVA program to create a package named


6 “mypack” and import & implement it in a suitable
class.

7 Construct Java Program using Java I/O Package

Write a java program to iterate through a


8 HashMap Using an iterator

9 Write Java Program to Rotate Elements of the List

Write Java Program to Insert a node at given


10 position in linked list
Program 1
Create Factorial of a number using command-line arguments
class Example1{
public static void main(String[] args){
int a , b = 1;
int n = Integer.parseInt(args[0]);
for(a = 1; a<= n ; a++)
{
b = b*a;
}
System.out.println("factorial is" +b);
}
}

Output:
Program 2
Write a java program to display the employee details using Scanner class
import java.util.*;
class EmployeeDetails
{
public static void main(String args[])
{
System.out.println("enter name,id,age,salary");Scanner sc=new
Scanner(System.in);
String n=sc.next();int i=sc.nextInt(); int a=sc.nextInt();
float s=sc.nextFloat();
System.out.println("name is"+n+"idis"+i+"ageis"+a+"salaryis"+s);
}
}
Program 3
Write a java program that checks whether a given string is palindrome or not
class palindrome
{
public static void main(String args[])
{
// Accepting the string at run time.
String s=args[0];
String s1="";
int le,j;
// Finding the length of the string.
le = s.length();
// Loop to find the reverse of the string.
for(j=le-1;j>=0;j--)
{
s1=s1+s.charAt(j);
}
// Condition to find whether two strings are equal and display the message.
if(s.equals(s1))
System.out.println("String "+s+" is palindrome"); else
System.out.println("String "+s+" is not palindrome");
}}

E:\kalpana>javac palindrome.java

E:\kalpana>java palindrome kalpana String kalpana is not palindrome

E:\kalpana>java palindrome madan


String madam is palindrome

E:\kalpana>
Program 4
Create Java programs using inheritance and polymorphism.
// Java program using inheritance
class Animal {
// method in the superclass
public void eat() {
System.out.println("I can eat");
}}
// Dog inherits Animal
class Dog extends Animal {
// overriding the eat() method
@Override
public void eat() {
System.out.println("I eat dog food");
}
// new method in subclass
public void bark() {
System.out.println("I can bark");
}}
class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog labrador = new Dog();
// call the eat() method
labrador.eat();
labrador.bark();
}
}
Output:
I eat dog food
I can bark

// Java program using polymorphism


class Language {
public void displayInfo() {
System.out.println("Common English Language");
}}
class Java extends Language {
@Override
public void displayInfo() {
System.out.println("Java Programming Language");
}}
class Main {
public static void main(String[] args) {
// create an object of Java class
Java j1 = new Java();
j1.displayInfo();
// create an object of Language class
Language l1 = new Language();
l1.displayInfo();
}}

Output: Java Programming Language


Common English Language
Program 5
Implement error-handling techniques using exception handling and
multithreading.
class H extends Thread{
public void run ()

{ String name=Thread.currentThread(). getName();


try {
for (int i=1;i<=4;i++)
{
System.out.println(name);

Thread.sleep(10000);
}
}
catch(InterruptedException e){}
}

public static void main(String[] args) {


H h1=new H();
H h2=new H();
h1.setName("First Thread");
h2.setName("Second Thread");

h1.start();
try
{
h1.join();
}

catch(InterruptedException e){}
h2.start();
System.out.println("Main Thread");
} }
Program 6
Write a JAVA program to create a package named “mypack” and
import & implement it in a suitable class.
package mypack;
public class MyPackageClass {
public void displayMessage() {
System.out.println("Hello from MyPackageClass in mypack package!");
}
// New utility method
public static int addNumbers(int a, int b) {
return a + b;
}
}

/********** PackageDemo class using mypack Package*********/


import mypack.*;
public class PackageDemo {
public static void main(String[] args) {
// Creating an instance of MyPackageClass from the mypack package
MyPackageClass myPackageObject = new MyPackageClass();
// Calling the displayMessage method from MyPackageClass
myPackageObject.displayMessage();
// Using the utility method addNumbers from MyPackageClass
int result = MyPackageClass.addNumbers(5, 3);
System.out.println("Result of adding numbers: " + result);
}
}
Program 7
Construct Java Program using Java I/O Package.

import java.io.*;
public class OotIO {
public static void main(String[] args) throws IOException
{
FileInputStream InStream = null;
FileOutputStream OpStream = null;
try {
InStream=new FileInputStream("Inputfile.txt");
OpStream=new FileOutputStream("Outputfile.txt");
// Reading source file and writing
// content to target file byte by byte
int temp;
while ((temp = InStream.read())!= -1)
OpStream.write((byte)temp);
}
finally {
if (InStream != null)
InStream.close();
if (OpStream != null)
OpStream.close();
}
}
}
Program 8
Write a java program to iterate through a HashMap Using an iterator.
import java.util.*;
public class GFG {

// Main driver method


public static void main(String[] arguments)
{
// Creating Hash map
Map<Integer, String> intType
= new HashMap<Integer, String>();

// Inserting data(Key-value pairs) in hashmap


intType.put(1, "First");
intType.put(2, "Second");
intType.put(3, "Third");
intType.put(4, "Fourth");

// Iterator
Iterator new_Iterator
= intType.entrySet().iterator();

// Iterating every set of entry in the HashMap


while (new_Iterator.hasNext()) {
Map.Entry<Integer, String> new_Map
= (Map.Entry<Integer, String>)
new_Iterator.next();

// Displaying HashMap
System.out.println(new_Map.getKey() + " = "
+ new_Map.getValue());
}
}
}

1 = First
2 = Second
3 = Third
4 = Fourth
Program 9
Write Java Program to Rotate Elements of the List.
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// creating array list
List<Integer> my_list = new ArrayList<>();
my_list.add(10);
my_list.add(20);
my_list.add(30);
my_list.add(40);
my_list.add(50);
my_list.add(60);
my_list.add(70);

// Printing list before rotation


System.out.println(
"List Before Rotation : "
+ Arrays.toString(my_list.toArray()));

// Loop according to the number of rotations


for (int i = 0; i < 4; i++) {
// storing the first element in the list
int temp = my_list.get(0);
// traverse the list and move elements to left
for (int j = 0; j < 6; j++) {
my_list.set(j, my_list.get(j + 1));
}
my_list.set(6, temp);
}

// Printing list after rotation


System.out.println(
"List After Rotation : "
+ Arrays.toString(my_list.toArray()));
}
}
Output
List Before Rotation : [10, 20, 30, 40, 50, 60, 70]
List After Rotation : [50, 60, 70, 10, 20, 30, 40]
Program 10
Write Java Program to Insert a node at given position in linked list.
import java.lang.*;
class LinkedList {
Node head;
int size = 0;
// Node Class
class Node{
int data;
Node next;

Node(int x) // parameterized constructor


{
data = x;
next = null;
}
}
public void insert(int data)
{
Node new_node = new Node(data);

new_node.data = data;
new_node.next = head;
head = new_node;
size++;
}
public void insertPosition(int pos, int data) {
Node new_node = new Node(data);
new_node.data = data;
new_node.next = null;

// Invalid positions
if(pos < 1 || pos > size + 1)
System.out.println("Invalid\n");
// inserting first node
else if(pos == 1){
new_node.next = head;
head = new_node;
size++;
}
else
{
Node temp = head;
// traverse till the current (pos-1)th node
while(--pos > 1){
temp = temp.next;
}
new_node.next= temp.next;
temp.next = new_node;
size++;
}
}
public void display()
{
System.out.print("Linked List : ");

Node node = head;


// as linked list will end when Node is Null
while(node!=null){
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
}
public class Main{
public static void main(String args[])
{
LinkedList linked_list = new LinkedList();
linked_list.insert(7);
linked_list.insert(6);
linked_list.insert(4);
linked_list.insert(3);
linked_list.insert(1);
linked_list.display();
// Inserts value: 2 at 2nd position
linked_list.insertPosition(2, 2);

// Inserts value: 5 at 5th position


linked_list.insertPosition(5, 5);

// Inserts value: 8 at 8th position


linked_list.insertPosition(8, 8);

linked_list.display();
}
}
Output:
Linked List : 1 3 4 6 7
Linked List : 1 2 3 4 5 6 7 8

You might also like