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

Java Lab File

The document contains multiple Java programming experiments covering various concepts such as command line arguments, inheritance, polymorphism, Java I/O, OOP principles, packages, exception handling, and multithreading. Each experiment includes code examples and their expected outputs. It serves as a comprehensive guide for learning Java programming fundamentals.

Uploaded by

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

Java Lab File

The document contains multiple Java programming experiments covering various concepts such as command line arguments, inheritance, polymorphism, Java I/O, OOP principles, packages, exception handling, and multithreading. Each experiment includes code examples and their expected outputs. It serves as a comprehensive guide for learning Java programming fundamentals.

Uploaded by

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

EXPERIMENT – 02

Creating simple java program using command line arguments.

public class CommandLineExample {


public static void main(String[] args) {
if (args.length == 0) {
System.out.println("No command line arguments provided.");
System.out.println("Usage: java CommandLineGreeting <your name>");
}
else {
System.out.println("Hello, " + args[0] + "!");
}
}
}

OUTPUT
EXPERIMENT – 04

Create java programs using inheritance and polymorphism.


Example 1: Inheritance
class Person {
String name;
int age;
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
class Student extends Person {
String college;
void displayStudentInfo() {
displayInfo(); // Call base class method
System.out.println("College: " + college);
}
}
public class InheritanceExample {
public static void main(String[] args) {
Student student = new Student();
student.name = "Abhineet Singh";
student.age = 20;
student.college = "BBDNIIT";
student.displayStudentInfo();
}
}

OUTPUT
Example 1: Polymorphism
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound();
a2.sound();
}
}

OUTPUT
EXPERIMENT – 07

Construct java program using java I/O package.

import java.io.*;
public class FileWriteReadExample {
public static void main(String[] args) {
String fileName = "example.txt";
try {
FileWriter writer = new FileWriter(fileName);
writer.write("Hello, this is a Java I/O example.\n");
writer.write("We are writing to a file using FileWriter.");
writer.close();
System.out.println("File written successfully.");
}
catch (IOException e) {
System.out.println("An error occurred while writing to the file.");
e.printStackTrace();
}
try {
FileReader reader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(reader);

System.out.println("\nReading from file:");


String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}

bufferedReader.close();
reader.close();
}
catch (IOException e) {
System.out.println("An error occurred while reading the file.");
e.printStackTrace();
}
}
}
OUTPUT
EXPERIMENT – 03

Understanding OOP concepts and basics of Java programming.

Object-Oriented Programming (OOP) Concepts


OOP is a programming paradigm based on the concept of objects. The four main
principles of OOP are:

1. Encapsulation:
Binding data and methods into a single unit (class).
Use of private access modifier and getters/setters to protect data.

Example:

class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

2. Inheritance:
One class (child) inherits properties and behavior from another (parent).
Promotes code reusability.
Use of extends keyword.

Example:

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}

3. Polymorphism:
Ability of an object to take many forms.
Compile-time polymorphism (method overloading).
Run-time polymorphism (method overriding).

Example:

class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}

4. Abstraction:
Hiding implementation details and showing only the functionality.
Achieved using abstract classes or interfaces.

Example:

abstract class Vehicle {


abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts");
}
}
Basics of Java Programming

1. Java Syntax:
Java is case-sensitive.
Every application starts with a main method.

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

2. Data Types:
Primitive: int, char, float, boolean, etc.
Non-primitive: String, arrays, classes, interfaces.

3. Control Structures:
if, else, switch, for, while, do-while.

4. Classes and Objects:


A class is a blueprint; an object is an instance.

5. Access Modifiers:
public, private, protected, and default (no modifier).

6. Keywords:
Examples: class, static, void, new, return, this, super, etc.
EXPERIMENT – 06

Create java program with the use of java packages.


Create the Package Class
(Save inside a folder named shapes)

package shapes;
public class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
}

Main Class to Use the Package

import shapes.Rectangle;
public class AreaCalculator {
public static void main(String[] args) {
Rectangle rect = new Rectangle(5.0, 3.0);
double area = rect.getArea();
System.out.println("Area of the rectangle: " + area);
}
}

OUTPUT
EXPERIMENT – 05

Implement error-handling techniques using exception handling and


multithreading.

class Task extends Thread {


private int[] numbers;
public Task(int[] numbers) {
this.numbers = numbers;
}
public void run() {
try {
for (int i = 0; i < numbers.length; i++) {
System.out.println("Thread " + Thread.currentThread().getName() +
" - Dividing 100 by " + numbers[i]);
int result = 100 / numbers[i]; // may throw ArithmeticException
System.out.println("Result: " + result);
}
}
catch (ArithmeticException e) {
System.out.println("Exception in thread " +
Thread.currentThread().getName() + ": Division by zero not allowed.");
}
finally {
System.out.println("Thread " + Thread.currentThread().getName() + "
finished execution.\n");
}
}
}
public class ExceptionHandlingWithThreads {
public static void main(String[] args) {
int[] array1 = {10, 5, 0, 2};
int[] array2 = {4, 2, 1};
Task t1 = new Task(array1);
Task t2 = new Task(array2);
t1.start();
t2.start();
}
}
OUTPUT
EXPERIMENT – 01

Use Java compiler and eclipse platform to write and execute java
program.

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, Welcome to Java Programming!");
System.out.println("This program isrunning in Eclipse.");
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
}
}

OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy