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

Java Lab Manual: Code Examples & Outputs

The document is a Java Lab Manual containing ten programming exercises, each demonstrating different Java concepts such as classes, inheritance, exception handling, multithreading, file handling, array operations, constructors, interfaces, method overloading and overriding, and collections. Each program includes the objective, code, and expected output. This manual serves as a practical guide for learning Java programming through hands-on examples.

Uploaded by

inbox.ayesha1989
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)
8 views5 pages

Java Lab Manual: Code Examples & Outputs

The document is a Java Lab Manual containing ten programming exercises, each demonstrating different Java concepts such as classes, inheritance, exception handling, multithreading, file handling, array operations, constructors, interfaces, method overloading and overriding, and collections. Each program includes the objective, code, and expected output. This manual serves as a practical guide for learning Java programming through hands-on examples.

Uploaded by

inbox.ayesha1989
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

Java Lab Manual (With Full Code and

Output)
Program 1: Class and Object Demo
Objective: Demonstrate use of class and object.

Code:

class Demo {
void show() {
[Link]("Hello from object!");
}

public static void main(String[] args) {


Demo d = new Demo();
[Link]();
}
}

Expected Output:
Hello from object!

Program 2: Inheritance Example


Objective: Demonstrate single inheritance.

Code:

class A {
void msg() {
[Link]("Hello from A");
}
}
class B extends A {
public static void main(String args[]) {
B obj = new B();
[Link]();
}
}
Expected Output:
Hello from A

Program 3: Exception Handling


Objective: Handle arithmetic exception.

Code:

public class Main {


public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Exception caught");
}
}
}

Expected Output:
Exception caught

Program 4: Multithreading
Objective: Create and run a thread.

Code:

class MyThread extends Thread {


public void run() {
[Link]("Thread running");
}

public static void main(String[] args) {


MyThread t = new MyThread();
[Link]();
}
}

Expected Output:
Thread running
Program 5: File Handling
Objective: Write to a file using FileWriter.

Code:

import [Link].*;

class FileWrite {
public static void main(String args[]) throws IOException {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello File");
[Link]();
[Link]("File written");
}
}

Expected Output:
File written

Program 6: Array Operations


Objective: Find maximum number in an array.

Code:

public class MaxInArray {


public static void main(String[] args) {
int[] arr = {2, 10, 5};
int max = arr[0];
for (int i = 1; i < [Link]; i++) {
if (arr[i] > max) max = arr[i];
}
[Link]("Max: " + max);
}
}

Expected Output:
Max: 10

Program 7: Constructor Usage


Objective: Demonstrate constructor invocation.
Code:

class Cons {
Cons() {
[Link]("Constructor called");
}

public static void main(String[] args) {


new Cons();
}
}

Expected Output:
Constructor called

Program 8: Interface Implementation


Objective: Implement interface methods.

Code:

interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() {
[Link]("Bark");
}

public static void main(String[] args) {


new Dog().sound();
}
}

Expected Output:
Bark

Program 9: Overloading and Overriding


Objective: Demonstrate method overriding.

Code:
class A {
void show() {
[Link]("From A");
}
}

class B extends A {
void show() {
[Link]("From B");
}

public static void main(String[] args) {


B obj = new B();
[Link]();
}
}

Expected Output:
From B

Program 10: Collections Usage


Objective: Use ArrayList from Java Collections.

Code:

import [Link].*;

class Example {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
[Link](1); [Link](2); [Link](3);
[Link]("List: " + list);
}
}

Expected Output:
List: [1, 2, 3]

Common questions

Powered by AI

Handling arithmetic exceptions ensures that programs can address arithmetic errors such as division by zero efficiently, maintaining program stability and preventing unexpected crashes. The example captures an ArithmeticException and outputs 'Exception caught', allowing the program to handle the error gracefully. This is crucial for building robust applications, as it enhances reliability and user experience by preventing abrupt terminations .

Constructors in Java initialize objects when they are created, ensuring that an object starts in a valid state with necessary attributes set up. In the example, invoking the Cons constructor outputs 'Constructor called', demonstrating immediate object setup upon instantiation. This aligns with encapsulation and initialization principles in object-oriented programming, ensuring objects begin with a defined behavior .

Method overloading and overriding enhance Java's flexibility by allowing multiple methods with the same name. Overloading offers different implementations based on parameters, while overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass, supporting polymorphism. The example demonstrates overriding where class B provides a specific implementation for the show method, thus customizing inherited behavior .

Inheritance improves code reusability by allowing a new class to use the properties and methods of an existing class without rewriting the code. In the provided example, class B inherits from class A, allowing B to use the msg method without defining it again. This reduces duplication and promotes the reuse of existing functionality .

File handling is critical in many applications for persisting data and enabling data exchange outside the application runtime. Implementing file writing, as shown in the example using FileWriter, allows programs to write contents to files, such as logs, backups, or configuration settings, thus increasing utility and user interaction capabilities. The example writes 'Hello File' to test.txt, showcasing basic file I/O operations .

Threads and multithreading allow a program to perform multiple operations concurrently, making efficient use of CPU resources and enhancing the performance of applications, particularly in tasks that are independent or can be parallelized. The Java example demonstrates creating and starting a new thread, which runs concurrently with the main thread, enabling more responsive and faster-performing applications .

Java achieves polymorphism through interfaces by allowing objects to be treated as instances of their interfaces rather than their specific classes. This enables different classes to implement the same interface and define methods that fulfill the same role in different contexts. In the Dog example, the Dog class implements the Animal interface and provides a specific implementation of the sound method. This enables the method to be called on any instance of Animal, facilitating abstraction and interchangeability .

Exception handling in software development is crucial for building robust applications that can gracefully handle runtime errors, avoiding crashes and unpredictable behavior. The given example shows handling an ArithmeticException when a division by zero is attempted, which prevents the program from terminating unexpectedly and provides a controlled response, thereby improving user experience and system stability .

ArrayLists, as part of Java Collections, provide a dynamic array-like structure that resizes automatically, offering significant flexibility over standard arrays. They improve data management by allowing easy insertion, update, and retrieval operations without manual array size management. In the example, ArrayList is used to store integers, showcasing simplicity in adding elements and flexibility compared to arrays, enhancing both performance and code maintainability .

Interfaces in Java provide a way to implement polymorphic behavior, allowing different classes to implement the same interface methods in different ways. This supports a design architecture where functionality can be expanded without exact dependencies on class hierarchies, promoting loose coupling and high cohesion. The example shows class Dog implementing the Animal interface and defining the sound method, which could be implemented differently in other classes .

You might also like