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

Cs201 - List of Experiments

Uploaded by

zizzy1029
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)
36 views

Cs201 - List of Experiments

Uploaded by

zizzy1029
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/ 34

‭ICFAI Foundation for Higher Education, Hyderabad.

‭Faculty of Science and Technology‬

‭Department of Computer Science and Engineering‬

‭First/Second Semester, 2023 - 2024‬

‭LAB MANUAL‬

‭ ourse Code: CS201‬


C ‭Course Title: Object Oriented Programming‬
‭Concepts‬

‭List of Experiments:‬

‭1.‬ ‭Programs to implement basic java Concepts.‬

‭2.‬ ‭Programs to implement input / output operations.‬

‭3.‬ ‭Programs to implement command line arguments and String‬

‭functions.‬

‭4.‬ ‭Programs to implement Inheritance concepts.‬

‭5.‬ ‭Programs to implement packages and interfaces.‬

‭6.‬ ‭Programs to implementException handling.‬

‭7.‬ ‭Programs to implement File I/O Operations.‬

‭8.‬ ‭Programs to implement multithreading.‬

‭9.‬ ‭Programs to implement Applets.‬

‭10.‬‭Program to implement Swings‬


/‭/ Experiment 1‬
‭1.‬ ‭Programs to implement basic java concepts?‬

/‭/ 1.‬‭Hello World Program (Basic Syntax)‬


‭public class HelloWorld {‬
‭public static void main(String[] args) {‬
‭System.out.println("Hello, World!"); // Prints "Hello, World!" to the‬
‭console‬
‭}‬
‭}‬

/‭/2.‬‭Variables and Data Types‬


‭public class DataTypes {‬
‭public static void main(String[] args) {‬
‭int age = 25;‬
‭double salary = 55000.50;‬
‭char grade = 'A';‬
‭boolean isJavaFun = true;‬

‭ ystem.out.println("Age: " + age);‬


S
‭System.out.println("Salary: " + salary);‬
‭System.out.println("Grade: " + grade);‬
‭System.out.println("Is Java Fun? " + isJavaFun);‬
‭}‬
}‭ ‬
‭// Write a Java program to demonstrate Arithmetic Operators.‬
‭class A_Operators {‬
‭public static void main(String []args) {‬
‭int a, b, c;‬
‭a = 50;‬
‭b = 100;‬
‭c = 150;‬
‭System.out.println ("Arithmetic Operators:\n");‬
‭System.out.println("Addition Arithmetic Operator");‬
‭System.out.println("Value of (a + b) is "+(a+b));‬
‭ ystem.out.println("\nSubtract Arithmetic Operator");‬
S
‭System.out.println("Value of (b - a) is "+(b-a));‬
‭System.out.println("\nDivision Arithmetic operator");‬
‭System.out.println("Value of (c / a) is "+(c/a));‬
‭System.out.println("\nMultiplication Arithmetic operator");‬
‭System.out.println("Value of (a * b) is "+(a*b));‬
‭System.out.println("\nModulus Arithmetic operator");‬
‭System.out.println("Value of (c % b) is "+(c%b));‬
‭}‬
‭}‬
‭Output:‬

/‭* Write a Java program to demonstrate Arithmetic Assignment‬


‭Operators. */‬
‭class Assignment {‬
‭public static void main(String []args) {‬
‭int a, b, c, d, e;‬
‭a = 5;‬
‭b = 10;‬
‭c = 15;‬
‭d = 20;‬
‭e = 30;‬
‭System.out.println("Arithmetic Assignment Operators:\n");‬
‭a += 5;‬
‭System.out.println("Add AND assignment operator");‬
‭System.out.println("Value of a: = "+a);‬
‭b -= 5;‬
‭System.out.println("\nSubtract AND assignment operator");‬
‭System.out.println("Value of b: = "+b);‬
‭c *= 5;‬
‭System.out.println("\nMultiple AND assignment operator");‬
‭System.out.println("Value of c: = "+c);‬
‭d /= 10;‬
‭System.out.println("\nDivide AND assignment operator");‬
‭System.out.println("Value of d: = "+d);‬
e‭ %= 9;‬
‭System.out.println("\nModulus AND assignment operator");‬
‭System.out.println("Value of e: = "+e);‬
‭}‬
‭}‬

‭Output:‬

/‭* Write a Java program to demonstrate the Relational/ Comparison‬


‭operators. */‬
‭class RelationalComparison {‬
‭public static void main(String []args) {‬
‭int a, b, c, d;‬
‭a = 5;‬
‭b = 10;‬
‭c = 15;‬
‭System.out.println("\nRelational Operators:");‬
‭System.out.println("Value of (a == b) is " + (a == b) );‬
‭System.out.println("Value of (a != b) is " + (a != b) );‬
‭System.out.println("\nValue of (b > c) is " + (b > c) );‬
‭System.out.println("Value of (c < a) is " + (c < a) );‬
‭System.out.println("\nValue of (c >= b) is " + (c >= b) );‬
‭System.out.println("Value of (b <= c) is " + (b <= c) );‬
‭}‬
‭}‬
‭Output:‬

/‭/ Write a Java program to demonstrate the Logical operators in Java.‬


‭class StudyDemo1 {‬
‭public static void main(String []args) {‬
‭boolean a, b, c, d;‬
‭a = true;‬
‭b = false;‬
‭c = true;‬
‭d = false;‬
‭ ystem.out.println("\nLogical Operators:");‬
S
‭System.out.println("Value of (a && b) = " + (a&&b));‬
‭System.out.println("Value of (b || c) = " + (b||c) );‬
‭System.out.println("Value of !(a && d) = " + !(a && d));‬
‭System.out.println("Value of (a ^ d) = " + (a ^ d));‬
‭}‬
‭}‬
‭Output:‬

/‭/ Write a Java program to demonstrate Unary operators in Java.‬


‭class UnaryOperators{‬
‭public static void main(String []args) {‬
‭int a, b;‬
‭a = 5;‬
‭System.out.println("Unary Operators:");‬
‭System.out.println("\nUnary Increment Operators");‬
‭System.out.println(a++);‬
‭System.out.println(++a);‬
‭System.out.println("\nUnary Decrement Operators\n");‬
‭System.out.println(a--);‬
‭System.out.println(--a);‬
‭}‬
‭}‬
‭Assignment:‬
‭1.Write a Java program to demonstrate the Ternary/ Conditional‬
‭Operator. ?‬

2‭ .Write‬‭a‬‭Java‬‭Program‬‭to‬‭demonstrate‬‭the‬‭following‬‭Bitwise‬‭Operators‬
‭in‬ ‭Java‬ ‭Bitwise‬‭NOT,‬‭Bitwise‬‭AND,‬‭Bitwise‬‭OR,‬‭Bitwise‬‭X-OR,‬‭Bitwise‬
‭Right‬ ‭Shift‬ ‭Operator,‬ ‭Bitwise‬ ‭Right‬ ‭Shift‬ ‭Phil‬ ‭Zero‬ ‭Operator,‬ ‭Bitwise‬
‭Left‬ ‭Shift‬‭Operator,‬‭Bitwise‬‭AND‬‭Assignment,‬‭Bitwise‬‭OR‬‭Assignment,‬
‭Bitwise‬ ‭X-OR‬ ‭Assignment,‬ ‭Bitwise‬ ‭Right‬ ‭Shift‬ ‭Assignment‬ ‭Operator,‬
‭Bitwise‬ ‭Right‬ ‭Shift‬ ‭Phil‬ ‭Zero‬ ‭Assignment‬ ‭Operator,‬ ‭Bitwise‬ ‭Left‬ ‭Shift‬
‭Assignment Operator‬
‭// 3. If-Else Statement (Control Structures)‬

‭public class IfElseExample {‬


‭public static void main(String[] args) {‬
‭int number = 10;‬

‭if (number > 0) {‬


‭System.out.println("The number is positive.");‬
‭} else if (number < 0) {‬
‭System.out.println("The number is negative.");‬
‭} else {‬
‭System.out.println("The number is zero.");‬
‭}‬
‭}‬
‭}‬

/‭/ 4. For Loop (Iteration)‬


‭public class ForLoopExample {‬
‭public static void main(String[] args) {‬
‭for (int i = 1; i <= 5; i++) {‬
‭System.out.println("Count: " + i);‬
‭}‬
‭}‬
‭}‬

/‭/ 5. While Loop (Iteration)‬


‭public class WhileLoopExample {‬
‭public static void main(String[] args) {‬
‭int i = 1;‬

‭while (i <= 5) {‬
‭System.out.println("Count: " + i);‬
‭i++;‬
‭}‬
‭}‬
‭}‬

/‭/ 6. Arrays‬
‭public class ArraysExample {‬
‭public static void main(String[] args) {‬
‭int[] numbers = {1, 2, 3, 4, 5};‬
‭for (int i = 0; i < numbers.length; i++) {‬
‭System.out.println("Number: " + numbers[i]);‬
‭}‬
‭}‬
‭}‬

/‭/ 7. Methods (Functions)‬


‭public class MethodsExample {‬
‭public static void main(String[] args) {‬
‭int result = addNumbers(5, 10);‬
‭System.out.println("Sum: " + result);‬
‭}‬
‭public static int addNumbers(int a, int b) {‬
‭return a + b;‬
‭}‬
‭}‬

/‭/ 8. Class and Objects‬


‭class Car{‬
‭String color;‬
‭String model;‬
‭void displayInfo() {‬
‭System.out.println("Color: " + color);‬
‭System.out.println("Model: " + model);‬
‭}‬
‭}‬
‭public class Main{‬
‭public static void main(String[] args){‬
‭Car myCar = new Car();‬
‭ yCar.color = "Red";‬
m
‭myCar.model = "Toyota";‬
‭myCar.displayInfo();‬
‭}‬
‭}‬

‭// 9. Constructor‬

‭class Student {‬
‭String name;‬
‭int age;‬

/‭/ Constructor‬
‭Student(String name, int age) {‬
‭this.name = name;‬
‭this.age = age;‬
‭}‬
‭void displayInfo() {‬
‭System.out.println("Name: " + name);‬
‭System.out.println("Age: " + age);‬
‭}‬
}‭ ‬
‭public class Main {‬
‭public static void main(String[] args) {‬
‭Student student1 = new Student("John", 20);‬
‭student1.displayInfo();‬
‭}‬
‭}‬
/‭/ Experiment 2‬
‭2.‬ ‭Programs to implement input / output operations?‬
Scanner‬
‭// 1.‬‭Basic Input and Output (Using‬‭ ‭)‬
‭import java.util.Scanner;‬

‭public class BasicIO {‬


‭public static void main(String[] args) {‬
‭Scanner scanner = new Scanner(System.in);‬

‭ ystem.out.print("Enter your name: ");‬


S
‭String name = scanner.nextLine();‬

‭ ystem.out.print("Enter your age: ");‬


S
‭int age = scanner.nextInt();‬

‭ ystem.out.println("Hello, " + name + "! You are " + age + " years‬
S
‭old.");‬

‭scanner.close();‬
‭}‬
‭}‬

Scanner‬
/‭/ 2. Handling Different Data Types (Using‬‭ ‭)‬
‭import java.util.Scanner;‬

‭public class InputDataTypes {‬


‭public static void main(String[] args) {‬
‭Scanner scanner = new Scanner(System.in);‬

‭ ystem.out.print("Enter an integer: ");‬


S
‭int intValue = scanner.nextInt();‬

‭ ystem.out.print("Enter a floating-point number: ");‬


S
‭double doubleValue = scanner.nextDouble();‬
‭ ystem.out.print("Enter a single character: ");‬
S
‭char charValue = scanner.next().charAt(0);‬

‭scanner.nextLine(); // Consume newline left-over‬

‭ ystem.out.print("Enter a string: ");‬


S
‭String stringValue = scanner.nextLine();‬

‭ ystem.out.println("\nYou entered:");‬
S
‭System.out.println("Integer: " + intValue);‬
‭System.out.println("Double: " + doubleValue);‬
‭System.out.println("Character: " + charValue);‬
‭System.out.println("String: " + stringValue);‬

‭scanner.close();‬
‭}‬
‭}‬

/‭/3. Reading Multiple Lines of Input‬


‭import java.util.Scanner;‬
‭public class MultipleLinesInput {‬
‭public static void main(String[] args) {‬
‭Scanner scanner = new Scanner(System.in);‬
‭System.out.println("Enter multiple lines of text (type 'exit' to‬
‭quit):");‬
‭while (true) {‬
‭String line = scanner.nextLine();‬
‭if (line.equalsIgnoreCase("exit")) {‬
‭break;‬
‭}‬
‭System.out.println("You entered: " + line);‬
‭}‬
‭scanner.close();‬
‭}‬
}‭ ‬
‭// 4. Input Validation Example‬
‭import java.util.Scanner;‬
‭public class InputValidation {‬
‭public static void main(String[] args) {‬
‭Scanner scanner = new Scanner(System.in);‬
‭int number;‬

‭while (true) {‬
‭System.out.print("Enter a positive number: ");‬
‭if (scanner.hasNextInt()) {‬
‭number = scanner.nextInt();‬
‭if (number > 0) {‬
‭break;‬
‭} else {‬
‭System.out.println("Error: Number must be positive.");‬
‭}‬
‭} else {‬
‭System.out.println("Error: Invalid input. Please enter an‬
‭integer.");‬
‭scanner.next(); // Consume the invalid input‬
‭}‬
‭}‬

‭ ystem.out.println("You entered a positive number: " + number);‬


S
‭scanner.close();‬
‭}‬
‭}‬

‭// Java Type Casting:‬

/‭/Two types of Type Casting in Java:‬


‭//a) Automatic Type Casting‬
‭/*Write a Java program to demonstrate Automatic type Casting‬
‭(Widening)*/‬
‭class Widening{‬
‭public static void main(String[] args) {‬
‭int a = 25; // int type‬
‭// int to float type i.e. Automatic/ Widening Type Casting‬
‭float b = a;‬
‭System.out.println ("The int = "+a);‬
‭System.out.println ("After Automatic Casting, the float value = "+b);‬
‭}‬
‭}‬

/‭/ b) Manual Type Casting‬


‭/* Write a program to see an example of the Manual/ Narrowing Type‬
‭Casting. */‬

‭class Narrowing{‬
‭public static void main(String[] args) {‬
‭// float type‬
‭float a = 25.7f;‬
‭// float to int type i.e. Manual/ Narrow Type Casting‬
‭int b = (int)a;‬
‭System.out.println("The float = "+a);‬
‭System.out.println("After Manual Casting, the int value = "+b);‬
‭}‬
‭}‬
‭Assignment: 1‬
‭1. Write a Java Program to get the default value of primitive data types.‬
‭2. Wrote a Java Program to convert integer to string & to convert string‬
‭to integer.‬

‭// Experiment 3‬
‭3.‬ ‭Programs to implement command line arguments and String functions?‬
‭( Java programs that demonstrate how to work with command-line‬
‭arguments‬‭and various string functions:)‬

/‭/ 1.‬‭Command-Line Arguments‬


‭public class CommandLineArgs {‬
‭public static void main(String[] args) {‬
‭// Check if arguments are passed‬
‭if (args.length > 0){‬
‭System.out.println("Command-line arguments:");‬

‭for (int i = 0; i < args.length; i++){‬


‭System.out.println("Argument " + i + ": " + args[i]);‬
‭}‬
‭} else {‬
‭System.out.println("No command-line arguments passed.");‬
‭}‬
‭}‬
‭}‬

‭ un this program with command-line arguments. For example:‬


R
‭java CommandLineArgs arg1 arg2 arg3‬

/‭* 2. String Functions‬


‭Write program demonstrates various string manipulation functions like‬
length()‬
‭ charAt()‬
‭,‬‭ substring()‬
‭,‬‭ toUpperCase()‬
‭,‬‭ ‭,‬
toLowerCase()‬
‭ trim()‬
‭,‬‭ replace()‬
‭,‬‭ split()‬
‭, and‬‭ ‭. */‬

‭public class StringFunctions {‬


‭public static void main(String[] args) {‬
‭String str = " Hello, Java World! ";‬

/‭/ Length of the string‬


‭System.out.println("Length: " + str.length());‬
/‭/ Character at a specific index‬
‭System.out.println("Character at index 7: " + str.charAt(7));‬
‭// Substring from a string‬
‭System.out.println("Substring (7, 11): " + str.substring(7, 11));‬
‭// Convert to uppercase‬
‭System.out.println("Uppercase: " + str.toUpperCase());‬
‭// Convert to lowercase‬
‭System.out.println("Lowercase: " + str.toLowerCase());‬
‭// Trim whitespace from both ends‬
‭System.out.println("Trimmed: '" + str.trim() + "'");‬
‭// Replace a character or a sequence‬
‭System.out.println("Replace 'Java' with 'World': " +‬
‭str.replace("Java", "World"));‬
‭// Split the string‬
‭String[] words = str.trim().split(" ");‬
‭System.out.println("Words:");‬
‭for (String word : words) {‬
‭System.out.println(word);‬
‭}‬
‭}‬
‭}‬

/‭/ 3. Combine Command-Line Arguments with String Functions‬


‭public class CommandLineStringFunctions {‬
‭public static void main(String[] args) {‬
‭if (args.length > 0) {‬
‭String input = args[0];‬
‭System.out.println("Original String: " + input);‬
‭System.out.println("Length: " + input.length());‬
‭System.out.println("Uppercase: " + input.toUpperCase());‬
‭System.out.println("Lowercase: " + input.toLowerCase());‬
‭System.out.println("Reversed: " + new‬
‭StringBuilder(input).reverse().toString());‬
‭// Check if the input is a palindrome‬
‭String reversed = new StringBuilder(input).reverse().toString();‬
‭if (input.equalsIgnoreCase(reversed)) {‬
‭System.out.println("The string is a palindrome.");‬
‭} else {‬
‭System.out.println("The string is not a palindrome.");‬
‭}‬
‭} else {‬
‭System.out.println("Please provide a string as a command-line‬
‭argument.");‬
‭}‬
‭}‬
‭}‬

‭ un the program with a string argument:‬


R
‭java CommandLineStringFunctions "madam"‬

‭/* 4. Count Words in a Sentence (Using Command-Line Arguments)‬


‭(The program counts the number of words in a sentence passed as a command-line argument.)‬
‭public class WordCount {‬
‭public static void main(String[] args) {‬
‭if (args.length > 0) {‬
‭String sentence = String.join(" ", args);‬
‭String[] words = sentence.trim().split("\\s+");‬
‭System.out.println("Word count: " + words.length);‬
‭} else {‬
‭System.out.println("Please provide a sentence as a‬
‭command-line argument.");‬
‭}‬
‭}‬
‭}‬
‭java WordCount "Java is a versatile programming language"‬

‭// Experiment 4‬
‭4.‬ ‭Programs to implement Inheritance concepts?‬

/‭*a. Write a JAVA program that illustrates simple inheritance.‬


‭Program: */‬
‭class Car‬
‭{‬
‭void display()‬
‭{‬
‭System.out.println(&quot;Light Weight Motor Vehicle&quot;);‬
‭}‬
‭}‬
‭class Maruthi extends Car‬
‭{‬
‭void brand()‬
‭{‬
‭System.out.println(&quot;Brand: Maruthi Suziki&quot;);‬
‭}‬
‭}‬
‭class Vehicle‬
‭{‬
‭public static void main(String args[])‬
‭{‬
‭Maruthi m = new Maruthi();‬
‭m.brand();‬
‭m.display();‬
‭}‬
‭}‬
‭Output:‬
‭Brand: Maruthi Suziki Light‬
‭Weight Motor Vehicle‬

/‭*b. Write a JAVA program that illustrates multi-level inheritance.‬


‭Program: */‬
‭class Car‬
‭{‬
‭ oid display()‬
v
‭{‬
‭System.out.println(&quot;Light Weight Motor Vehicle&quot;);‬
‭}‬
‭}‬

c‭ lass Maruthi extends Car‬


‭{‬
‭void brand()‬
‭{‬

‭ ystem.out.println(&quot;Brand: Maruthi Suziki&quot;);‬


S
‭}‬
}‭ ‬
‭class Swift extends Maruthi‬
‭{‬
‭void model()‬
‭{‬
‭System.out.println(&quot;Model: Swift&quot;);‬
‭}‬
‭}‬
‭class Vehicle‬
‭{‬
‭public static void main(String args[])‬
‭{‬
‭Swift s = new Swift();‬
‭s.display();‬
‭s.brand();‬
‭s.model();‬
‭}‬
‭}‬
‭Output:‬
‭Light Weight Motor Vehicle‬
‭Brand: Maruthi Suziki‬
‭Model: Swift‬
/‭* c. Write a JAVA program to given the example for ‘super‘ keyword.‬
‭Program:‬ ‭*/‬

c‭ lass Animal‬
‭{‬
‭protected String type=&quot;animal&quot;;‬
‭}‬
‭class Dog extends Animal‬
‭{‬
‭public String type=&quot;mammal&quot;;‬
‭public void printType()‬
‭{‬
‭System.out.println(&quot;I am a &quot; + type);‬
‭System.out.println(&quot;I am an &quot; + super.type);‬
‭}‬
‭}‬
‭class Main‬
‭{‬
‭public static void main(String[] args)‬
‭{‬
‭Dog dog1 = new Dog();‬
‭dog1.printType();‬

‭}‬
}‭ ‬
‭Output:‬
‭I am a‬
‭mammal I am‬
‭an animal‬

/‭/ d. Write a JAVA program to demonstrate Abstract Class.‬


‭//Program:‬
a‭ bstract class Shape‬
‭{‬
‭abstract void draw();‬
‭}‬
‭class Rectangle extends Shape‬
‭{‬
‭void draw()‬
‭{‬
‭System.out.println(&quot;Drawing Rectangle&quot;);‬
‭}‬
‭}‬
‭class Circle extends Shape‬
‭{‬
‭void draw()‬
‭{‬
‭System.out.println(&quot;Drawing Circle&quot;);‬
‭}‬
‭}‬
‭class Test‬
‭{‬
‭public static void main(String args[])‬
‭{‬
‭Shape s1 = new Rectangle(); s1.draw();‬
‭Shape s2 = new Circle(); s2.draw();‬
‭}‬
‭}‬

‭ utput:‬
O
‭Drawing Rectangle‬
‭Drawing Circle‬

‭ ssignment‬
A
‭1. Write a Java Program to demonstrate method overriding‬

‭// Experiment 5‬
‭5.‬ ‭Programs to implement packages and interfaces?‬
‭a. Write a JAVA program for multiple inheritance using interfaces.‬

/‭/ Program:‬
‭interface Printable‬
‭{‬
‭void print();‬
‭}‬
‭interface Showable‬
‭{‬
‭void show();‬
‭}‬
‭class Sample implements Printable,Showable‬
‭{‬
‭public void print()‬
‭{‬
‭System.out.println(&quot;Hello&quot;);‬
‭}‬
‭public void show()‬
‭{‬
‭System.out.println(&quot;Welcome&quot;);‬
‭}‬
‭public static void main(String args[])‬
‭{‬
‭Sample obj=new Sample();‬
‭obj.print();‬
‭obj.show();‬
‭}‬
‭}‬

‭ utput:‬
O
‭Hello‬
‭Welcome‬

‭/b. Write a JAVA program to create and import a package.‬


/‭/Program:‬
‭package pack;‬
‭public class A‬
‭{‬
‭public void msg()‬
‭{‬
‭System.out.println(&quot;Hello&quot;);‬
‭}‬
‭}‬
‭//save by B.java‬

‭ ackage mypack;‬
p
‭import pack.*;‬
‭class B‬
‭{‬
‭public static void main(String args[])‬
‭{‬
‭A obj = new A();‬
‭obj.msg();‬
‭}‬
‭}‬

‭Output:‬
‭Hell‬‭o‬
/‭/ Experiment 6 (‬‭Exception Handling)‬
‭1. Write a Java program to demonstrate the benefits of exception handling.‬
‭public class BenefitsOfExceptionHandling {‬
‭public static void main(String[] args) {‬
‭try {‬
‭int[] numbers = {1, 2, 3};‬
‭System.out.println(numbers[5]);‬
‭}‬
‭catch (ArrayIndexOutOfBoundsException e)‬
‭{‬
‭System.out.println("Exception caught: Attempted to access an‬
‭index that does not exist.");‬
‭}‬
‭System.out.println("Program continues to run smoothly after‬
‭handling the exception.");‬
‭}‬
‭}‬
‭2. Write a Java program to demonstrate exception hierarchy‬
‭public class ExceptionHierarchyExample {‬
‭public static void main(String[] args) {‬
‭try {‬
‭String str = null;‬
‭str.length();‬
‭}‬
‭catch (NullPointerException e)‬
‭{‬
‭System.out.println("Caught a NullPointerException: " +‬
‭e.getMessage());‬
‭}‬
‭catch (Exception e)‬
‭{‬
‭System.out.println("Caught a generic exception: " + e.getMessage());‬
‭}‬
‭}}‬
‭3. Write a Java program to demonstrate checked and unchecked exceptions.‬
‭import java.io.File;‬
‭import java.io.FileNotFoundException;‬
‭import java.util.Scanner;‬

‭public class CheckedUncheckedExample {‬


‭public static void main(String[] args) {‬
‭// Unchecked Exception example‬
‭try {‬
‭int result = 10 / 0;‬
‭}‬
‭catch (ArithmeticException e)‬
‭{‬
‭System.out.println("Caught unchecked exception: " + e.getMessage());‬
‭}‬

‭try {‬
‭readFromFile("nonexistentfile.txt");‬
‭}‬
‭catch (FileNotFoundException e) {‬
‭System.out.println("Caught checked exception: " + e.getMessage());‬
‭}‬
‭}‬

/‭/ Method that throws a checked exception‬


‭public static void readFromFile(String filename) throws‬
‭FileNotFoundException {‬
‭File file = new File(filename);‬
‭Scanner sc = new Scanner(file); // FileNotFoundException is a checked‬
‭exception‬
‭}‬
‭}‬

‭4. Write a Java program to demonstrate the usage of try, catch, throw and finally.‬
‭public class TryCatchThrowFinallyExample {‬
‭public static void main(String[] args) {‬
‭try {‬
‭validateAge(15);‬
‭}‬
‭catch (IllegalArgumentException e)‬
‭{‬
‭System.out.println("Exception caught: " + e.getMessage());‬
‭}‬
‭finally‬
‭{‬
‭System.out.println("Finally block executed, resource cleanup can be‬
‭done here.");‬
‭}‬
‭}‬

‭public static void validateAge(int age) {‬


‭if (age < 18) {‬
‭throw new IllegalArgumentException("Age must be 18 or above.");‬
‭}‬
‭}‬
‭}‬
‭// Experiment‬‭7 (File I/O Operations)‬

‭1.Write a Java program to read and write text files using file streams.‬
‭import java.io.FileInputStream;‬
‭import java.io.FileOutputStream;‬
‭import java.io.IOException;‬

‭public class ReadWriteTextFileUsingStreams {‬


‭public static void main(String[] args) {‬
‭try {‬
‭// Writing to a text file using FileOutputStream‬
‭FileOutputStream‬ ‭fos‬ ‭=‬ ‭new‬
‭FileOutputStream("textfile.txt");‬
‭String‬‭data‬‭=‬‭"This‬‭is‬‭a‬‭real-time‬‭example‬‭of‬‭writing‬‭text‬
‭to a file.";‬
‭fos.write(data.getBytes());‬
‭fos.close();‬
‭System.out.println("Text written to file successfully.");‬

/‭/ Reading from the text file using FileInputStream‬


‭FileInputStream fis = new FileInputStream("textfile.txt");‬
‭int character;‬
‭System.out.print("Reading text from file: ");‬
‭while ((character = fis.read()) != -1) {‬
‭System.out.print((char) character);‬
‭}‬
‭fis.close();‬
‭} catch (IOException e) {‬
‭System.out.println("An‬ ‭error‬ ‭occurred:‬ ‭"‬ ‭+‬
‭e.getMessage());‬
‭}‬
‭}‬
‭}‬
2‭ .Write‬ ‭a‬ ‭Java‬ ‭program‬ ‭to‬ ‭read‬ ‭and‬ ‭write‬ ‭binary‬ ‭data‬ ‭using‬ ‭byte‬
‭streams.‬
‭import java.io.FileInputStream;‬
‭import java.io.FileOutputStream;‬
‭import java.io.IOException;‬

‭public class ReadWriteBinaryFileUsingByteStreams {‬


‭public static void main(String[] args) {‬
‭try {‬
‭// Writing binary data to a file using FileOutputStream‬
‭FileOutputStream fos = new FileOutputStream("binaryfile.dat");‬
‭byte[]‬‭binaryData‬‭=‬‭{10,‬‭20,‬‭30,‬‭40,‬‭50};‬‭//‬‭Example‬‭binary‬
‭data‬
‭fos.write(binaryData);‬
‭fos.close();‬
‭System.out.println("Binary data written to file successfully.");‬

/‭/ Reading binary data from the file using FileInputStream‬


‭FileInputStream fis = new FileInputStream("binaryfile.dat");‬
‭int byteData;‬
‭System.out.print("Reading binary data from file: ");‬
‭while ((byteData = fis.read()) != -1) {‬
‭System.out.print(byteData + " ");‬
‭}‬
‭fis.close();‬
‭} catch (IOException e) {‬
‭System.out.println("An‬ ‭error‬ ‭occurred:‬ ‭"‬ ‭+‬
‭e.getMessage());‬
‭}‬
‭}‬
‭}‬

3‭ .Write‬ ‭a‬ ‭Java‬ ‭program‬ ‭to‬ ‭handle‬ ‭text‬ ‭input‬ ‭and‬ ‭output‬ ‭using‬
‭BufferedReader and BufferedWriter.‬
‭import java.io.BufferedReader;‬
i‭mport java.io.BufferedWriter;‬
‭import java.io.FileReader;‬
‭import java.io.FileWriter;‬
‭import java.io.IOException;‬

‭public class TextInputOutputUsingBufferedReaderWriter {‬


‭public static void main(String[] args) {‬
‭try {‬
‭// Writing text to a file using BufferedWriter‬
‭BufferedWriter‬ ‭writer‬ ‭=‬ ‭new‬ ‭BufferedWriter(new‬
‭FileWriter("bufferedtextfile.txt"));‬
‭writer.write("This‬ ‭is‬ ‭a‬ ‭real-time‬ ‭example‬ ‭using‬
‭BufferedWriter.");‬
‭writer.newLine();‬
‭writer.write("BufferedWriter makes writing efficient.");‬
‭writer.close();‬
‭System.out.println("Text‬ ‭written‬ ‭to‬ ‭file‬ ‭using‬
‭BufferedWriter.");‬

‭// Reading text from the file using BufferedReader‬


‭BufferedReader‬ ‭reader‬ ‭=‬ ‭new‬ ‭BufferedReader(new‬
‭FileReader("bufferedtextfile.txt"));‬
‭String line;‬
‭System.out.println("Reading‬ ‭text‬ ‭from‬ ‭file‬ ‭using‬
‭BufferedReader:");‬
‭while ((line = reader.readLine()) != null) {‬
‭System.out.println(line);‬
‭}‬
‭reader.close();‬
‭} catch (IOException e) {‬
‭System.out.println("An‬ ‭error‬ ‭occurred:‬ ‭"‬ ‭+‬
‭e.getMessage());‬
‭}‬
‭}‬
‭}‬
4‭ .Write‬ ‭a‬ ‭Java‬ ‭program‬ ‭to‬ ‭perform‬ ‭binary‬ ‭input‬ ‭and‬ ‭output‬ ‭using‬
‭DataInputStream and DataOutputStream.‬
‭import java.io.DataInputStream;‬
‭import java.io.DataOutputStream;‬
‭import java.io.FileInputStream;‬
‭import java.io.FileOutputStream;‬
‭import java.io.IOException;‬
‭public class BinaryInputOutputUsingDataStreams {‬
‭public static void main(String[] args) {‬
‭try {‬
‭// Writing binary data using DataOutputStream‬
‭DataOutputStream‬ ‭dos‬ ‭=‬ ‭new‬ ‭DataOutputStream(new‬
‭FileOutputStream("datastreamfile.dat"));‬
‭dos.writeInt(100);‬
‭dos.writeDouble(9.99);‬
‭dos.writeBoolean(true);‬
‭dos.close();‬
‭System.out.println("Binary‬ ‭data‬ ‭written‬ ‭using‬
‭DataOutputStream.");‬
‭// Reading binary data using DataInputStream‬
‭DataInputStream‬ ‭dis‬ ‭=‬ ‭new‬ ‭DataInputStream(new‬
‭FileInputStream("datastreamfile.dat"));‬
‭System.out.println("Reading‬‭binary‬‭data‬‭from‬‭file‬‭using‬
‭DataInputStream:");‬
‭System.out.println("Int: " + dis.readInt());‬
‭System.out.println("Double: " + dis.readDouble());‬
‭System.out.println("Boolean: " + dis.readBoolean());‬
‭dis.close();‬
‭} catch (IOException e) {‬
‭System.out.println("An‬ ‭error‬ ‭occurred:‬ ‭"‬ ‭+‬
‭e.getMessage());‬
‭}‬
‭}‬
‭}‬

5‭ .Write‬ ‭a‬ ‭Java‬ ‭program‬ ‭to‬ ‭demonstrate‬ ‭random‬ ‭access‬ ‭file‬ ‭operations‬
‭(read/write at specific positions).‬
‭import java.io.IOException;‬
‭import java.io.RandomAccessFile;‬

‭public class RandomAccessFileOperations {‬


‭public static void main(String[] args) {‬
‭try {‬
‭// Writing and reading using RandomAccessFile‬
‭RandomAccessFile‬ ‭raf‬ ‭=‬ ‭new‬
‭RandomAccessFile("randomaccessfile.txt", "rw");‬
‭raf.writeUTF("Hello, this is a random access file.");‬
‭raf.seek(0); // Move pointer to the start‬
‭System.out.println("Data at start: " + raf.readUTF());‬

/‭/ Modifying the content at specific position‬


‭raf.seek(0); // Move pointer to the start again‬
‭raf.writeUTF("Modified random access file content.");‬
‭raf.seek(0); // Read the modified content‬
‭System.out.println("Modified data: " + raf.readUTF());‬

r‭ af.close();‬
‭} catch (IOException e) {‬
‭System.out.println("An‬ ‭error‬ ‭occurred:‬ ‭"‬ ‭+‬
‭e.getMessage());‬
‭}‬
‭}‬
‭}‬
‭6.Write‬‭a‬‭Java‬‭program‬‭to‬‭manage‬‭files‬‭using‬‭the‬‭File‬‭class‬‭(create,‬‭delete,‬‭get‬
‭file information).‬
‭import java.io.File;‬
‭import java.io.IOException;‬
‭public class FileManagementExample {‬
‭public static void main(String[] args) {‬
‭try {‬
‭// Creating a new file‬
‭File file = new File("fileManagementExample.txt");‬
‭if (file.createNewFile()) {‬
‭System.out.println("File created: " + file.getName());‬
‭} else {‬
‭System.out.println("File already exists.");‬
‭}‬

/‭/ Displaying file information‬


‭System.out.println("File path: " + file.getAbsolutePath());‬
‭System.out.println("Readable: " + file.canRead());‬
‭System.out.println("Writable: " + file.canWrite());‬
‭System.out.println("Executable: " + file.canExecute());‬

/‭/ Deleting the file‬


‭if (file.delete()) {‬
‭System.out.println("File deleted: " + file.getName());‬
‭} else {‬
‭System.out.println("Failed to delete the file.");‬
‭}‬
‭} catch (IOException e) {‬
‭System.out.println("An‬ ‭error‬ ‭occurred:‬ ‭"‬ ‭+‬
‭e.getMessage());‬
‭}‬
‭}‬
‭}‬

‭ ssignment Questions:‬
A
‭1. Write a Java program to rethrow an exception.‬
‭2.Write‬ ‭a‬ ‭Java‬ ‭program‬ ‭to‬ ‭read‬ ‭and‬ ‭write‬ ‭character‬ ‭data‬ ‭using‬ ‭character‬
‭streams.‬
/‭/ Experiment 8‬
‭8.Programs to implement Multithreading?‬
‭class T1 extends Thread{‬
‭public void run(){‬
‭System.out.println("Thread1 is running...");‬
‭}‬

c‭ lass T2 extends Thread{‬


‭public void run(){‬
‭System.out.println("Thread2 is running...");‬
‭}‬
‭class T3 extends Thread{‬
‭public void run(){‬
‭System.out.println("Thread3 is running...");‬
‭}‬
‭class T4 extends Thread{‬
‭public void run(){‬
‭System.out.println("Thread4 is running...");‬
‭}‬

p‭ ublic static void main(String args[]){‬


‭T1 ob1=new T1();‬
‭ob1.start();‬

‭ 2 ob1=new T2();‬
T
‭ob2.start();‬
‭T3 ob3=new T3();‬
‭ob3.start();‬
‭T4 ob4=new T4();‬
‭ob4.start();‬

}‭ ‬
‭}‬
‭Assignment‬
‭1.‬ ‭Write a Program to print even and odd numbers using two threads in Java‬
‭// Experiment 9‬
‭9. Programs to implement Applets?‬
‭import java.applet.*;‬
‭import java.awt.*;‬
‭public class MyApplet extends Applet‬
{‭ ‬
‭int height, width;‬
‭public void init()‬
‭{‬
‭height = getSize().height;‬
‭width = getSize().width;‬
‭setName("MyApplet");‬
‭}‬
‭public void paint(Graphics g)‬
‭{‬
‭g.drawRoundRect(10, 30, 120, 120, 2, 3);‬
‭}‬
‭}‬
‭Assignment‬
‭1.Write a program to create two Labels and Buttons using Applets‬

/‭/ Experiment 10‬


‭10. Programs to implement Swings?‬
‭Creating JFrame window by Instantiating JFrame class‬
‭import javax.swing.*; //importing swing package‬
‭import java.awt.*; //importing awt package‬
p‭ ublic class First‬
‭{‬
‭JFrame jf;‬
‭public First() {‬
‭jf = new JFrame("MyWindow");//Creating a JFrame with name MyWindow‬
‭JButton btn = new JButton("Say Hello");//Creating a Button named Say‬
‭Hello‬
‭jf.add(btn); //adding button to frame‬
‭jf.setLayout(new FlowLayout());//setting layout using FlowLayout object‬
‭jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//setting close‬
‭operation.‬
‭jf.setSize(400, 400); //setting size‬
‭jf.setVisible(true); //setting frame visibility‬
‭}‬
‭public static void main(String[] args)‬
‭{‬
‭new First();‬
‭}‬
‭}‬
‭Assignment‬
‭1.‬ ‭Write a program to create a TextFiled and Label for entering‬
‭Username‬

You might also like