Java programexp1-13
Java programexp1-13
*Example Program:*
java
int a = 10;
double b = 20.5;
char c = 'A';
boolean d = true;
String e = "Hello";
*Example Program:*
java
int num = 5;
// Conditional statement
if (num % 2 == 0) {
System.out.println(num + " is even");
} else {
// Loop statement
*Example Program:*
java
import java.util.Scanner;
}
#### ii. Reading and Writing Primitive Data Types Using DataInputStream and DataOutputStream
*Example Program:*
java
import java.io.*;
// Writing data
dos.writeInt(123);
dos.writeDouble(45.67);
dos.close();
// Reading data
dis.close();
#### iii. Perform Reading and Writing Operations on Files Using FileStreams
*Example Program:*
java
import java.io.*;
public class FileStreamExample {
// Writing data
fos.write("Hello, World!".getBytes());
fos.close();
// Reading data
int i;
System.out.print((char) i);
fis.close();
*Example Program:*
java
}
### 5. Exercise Program to Create Class and Objects and Adding Methods
*Example Program:*
java
class Person {
String name;
int age;
void displayInfo() {
person.name = "Alice";
person.age = 25;
person.displayInfo();
*Example Program:*
java
class Person {
String name;
int age;
// Default constructor
Person() {
this.name = "Unknown";
this.age = 0;
// Parameterized constructor
this.name = name;
this.age = age;
void displayInfo() {
person1.displayInfo();
person2.displayInfo();
*Example Program:*
java
if (args.length > 0) {
int sum = 0;
sum += Integer.parseInt(arg);
} else {
#### ii. Input as Command Line Arguments and Update Manipulated Data in Files
*Example Program:*
java
import java.io.*;
if (args.length > 0) {
writer.close();
} else {
*Example Program:*
java
class MathOperations {
return a + b;
return a + b;
}
### 9. Exercise Programs on Inheritance
*Example Program:*
java
class Animal {
void eat() {
void bark() {
dog.eat();
dog.bark();
*Example Program:*
java
class Animal {
void sound() {
@Override
void sound() {
dog.sound();
*Example Program:*
java
package mypack;
System.out.println("This is an animal.");
import mypack.Animal;
animal.display();
*Example Program:*
java
package mypack;
import mypack.Dog;
public class Main {
dog.bark();
*Example Program:*
java
interface Animal {
void eat();
dog.eat();
}
### 13. Exercise Programs on Collections
#### i. Write a Java Program to Search a Student Mark Percentage Based on Pin Number Using
ArrayList
*Example Program:*
```java
import java.util.ArrayList;
class Student {
int pin;
double percentage;
this.pin = pin;
this.percentage = percentage;
if (student.pin == searchPin) {
import java.util.LinkedListy
//insection
officers.add("Begum");
officers.add("yasmin");
officers.add("bb");
officers.add("Naazil");
officers.add("Rabbani");
System.out.println(officers);
//updation
System.out.println(officers);
//deletion
officers.remove(3):
officers.removeFirst();
Output:
[Begum,Yasmin,bb,Naazil,Rabbani]
clase SearchValunkeys
ht.put("1", "First");
ht.put("2", "Second");
ht.put("3", "Third"):
ht.put("4", "Forth"):
ht.put("5", "Fifth");
System.out.println(ht):
System.out.println("=================");
if (ht.containsKey("1") &&ht.containsValue ("First"))
else
System.out.println("==============");
Output:
========================
=====================
iv) Write a Java program to sorting employee detalls using hash map.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
map.put("2", "BUNNY");
map.put("8", "AJAY");
map.put("4", "DIMPU");
map.put("7", "FAROOQ");
map.put("6", "Rams");
return str1.compareTo(str2);
});
if (entry.getValue().equals(value)) {
sortedMap.put(entry.getKey(), entry.getValue());
System.out.println(sortedMap);
OUTPUT
{2=BUNNY,8=AJAY,4=DIMPU, 7= FAROOQ,
6=Rams}
14.Exercise on exception handling
try Block:
The try block contains code that might throw an exception. It is used to wrap the code where you
anticipate potential errors.
catch Block:
The catch block is used to handle exceptions thrown by the try block. It defines how to respond to
specific types of exceptions.
finally Block:
The finally block contains code that is always executed, whether an exception is thrown or not. It is
typically used for cleanup activities, like closing files or releasing resources.
Program:
import java.util.Scanner;
int result = 0;
try {
} catch (NumberFormatException e) {
} catch (ArithmeticException e) {
} finally {
scanner.close();
Output
100 divided by 5 is 20
Finally block: Closing the scanner.
try {
int index = 3;
int result = 100 / number; // This line will not be reached due to the previous exception
} catch (ArrayIndexOutOfBoundsException e) {
} catch (NumberFormatException e) {
} catch (ArithmeticException e) {
} finally {
Output
try {
try {
} catch (ArrayIndexOutOfBoundsException e) {
// This block will execute if no exceptions were thrown in the inner try block
int a = 10;
int b = 0;
} catch (ArithmeticException e) {
} finally {
Output
15.Exercise on multithreading
@Override
try {
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
};
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
}
System.out.println("Single thread execution complete.");
Output
Task running: 1
Task running: 2
Task running: 3
Task running: 4
Task running: 5
thread1.start();
thread2.start();
thread3.start();
try {
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
this.threadName = threadName;
@Override
try {
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
Output
Thread 1 is running: 1
Thread 2 is running: 1
Thread 3 is running: 1
Thread 1 is running: 2
Thread 2 is running: 2
Thread 3 is running: 2
Thread 1 is running: 3
Thread 2 is running: 3
Thread 3 is running: 3
highPriorityThread.start();
mediumPriorityThread.start();
lowPriorityThread.start();
try {
highPriorityThread.join();
mediumPriorityThread.join();
lowPriorityThread.join();
} catch (InterruptedException e) {
@Override
try {
} catch (InterruptedException e) {
Output
class Chat
if (flag)
try{
Wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.print in (msg):
flag= true;
notify();
if (!flag)
{
try{
Wait();
System.out.println(msg)
flag falaer
notify)
Chat B
Stringi) si "Hi", "How are you ?", "I am also doing final
this.smi
miQuestiontal [1]):
Chat mi
this.n21
public void run() ( for (int 10; 12.length; i++) .Answer (s2[1])