0% found this document useful (0 votes)
37 views4 pages

Java Lab Assignment Solutions

The document contains several Java programming examples covering various concepts such as Fibonacci series, factorial calculation, command line arguments, and user-defined packages. It also includes examples of constructors, string operations, inheritance, exception handling, GUI with Swing, event handling, and file I/O. Each example demonstrates fundamental programming techniques and practices in Java.

Uploaded by

laita nikam
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)
37 views4 pages

Java Lab Assignment Solutions

The document contains several Java programming examples covering various concepts such as Fibonacci series, factorial calculation, command line arguments, and user-defined packages. It also includes examples of constructors, string operations, inheritance, exception handling, GUI with Swing, event handling, and file I/O. Each example demonstrates fundamental programming techniques and practices in Java.

Uploaded by

laita nikam
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

1.

Fibonacci Series
class FibonacciSeries {
public static void main(String[] args) {
int n = 10, a = 0, b = 1, c;
[Link]("Fibonacci Series: " + a + " " + b);
for (int i = 2; i < n; i++) {
c = a + b;
[Link](" " + c);
a = b;
b = c;
}
}
}

2. Factorial of a Number
import [Link];

class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
int fact = 1;
for (int i = 1; i <= n; i++)
fact *= i;
[Link]("Factorial = " + fact);
}
}

3. Command Line Arguments


class CommandArgs {
public static void main(String[] args) {
[Link]("You entered:");
for (String arg : args)
[Link](arg);
}
}

4. Student Information using Array


import [Link];

class StudentArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of students: ");
int n = [Link]();
String[] names = new String[n];
int[] marks = new int[n];

for (int i = 0; i < n; i++) {


[Link]("Enter name of student " + (i + 1) + ": ");
names[i] = [Link]();
[Link]("Enter marks: ");
marks[i] = [Link]();
}

[Link]("\nStudent Information:");
for (int i = 0; i < n; i++)
[Link](names[i] + " - " + marks[i]);
}
}

5. User Defined Package


// File 1: mypack/[Link]
package mypack;

public class Message {


public void show() {
[Link]("Hello from user-defined package!");
}
}

// File 2: [Link]
import [Link];

class TestPackage {
public static void main(String[] args) {
Message obj = new Message();
[Link]();
}
}

6. Default & Parameterized Constructor


class Student {
String name;
int age;

Student() {
name = "Unknown";
age = 18;
}

Student(String n, int a) {
name = n;
age = a;
}

void display() {
[Link]("Name: " + name + ", Age: " + age);
}

public static void main(String[] args) {


Student s1 = new Student();
Student s2 = new Student("Ravi", 21);
[Link]();
[Link]();
}
}

7. String Operations
class StringOps {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "World";

[Link]("Concatenation: " + [Link](" " + s2));


[Link]("Length: " + [Link]());
[Link]("Uppercase: " + [Link]());
[Link]("Substring: " + [Link](1, 4));
[Link]("Equals: " + [Link]("Hello"));
}
}

8. Wrapper Classes
class WrapperDemo {
public static void main(String[] args) {
int a = 10;
Integer i = [Link](a); // Boxing
int b = i; // Unboxing
[Link]("Primitive int: " + a);
[Link]("Boxed Integer: " + i);
[Link]("Unboxed int: " + b);
}
}

9. Inheritance
class Animal {
void eat() {
[Link]("Eating...");
}
}

class Dog extends Animal {


void bark() {
[Link]("Barking...");
}

public static void main(String[] args) {


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

10. Exception Handling


class ExceptionDemo {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int c = a / b;
[Link]("Result: " + c);
} catch (ArithmeticException e) {
[Link]("Error: Division by zero not allowed!");
} finally {
[Link]("Program executed.");
}
}
}

11. Student Registration Form (Swing)


import [Link].*;

class StudentForm {
public static void main(String[] args) {
JFrame f = new JFrame("Student Registration Form");
[Link](400, 300);
[Link](null);

JLabel l1 = new JLabel("Name:");


[Link](50, 50, 100, 30);
JTextField t1 = new JTextField();
[Link](150, 50, 150, 30);

JLabel l2 = new JLabel("Age:");


[Link](50, 100, 100, 30);
JTextField t2 = new JTextField();
[Link](150, 100, 150, 30);

JButton b = new JButton("Submit");


[Link](150, 150, 100, 30);

[Link](l1); [Link](t1);
[Link](l2); [Link](t2);
[Link](b);
[Link](true);
}
}
12. Event Handling Example
import [Link].*;
import [Link].*;

class EventDemo extends Frame implements ActionListener {


TextField tf;

EventDemo() {
tf = new TextField();
[Link](60, 50, 170, 20);
Button b = new Button("Click Me");
[Link](100, 120, 80, 30);
[Link](this);
add(b);
add(tf);
setSize(300, 200);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


[Link]("Button Clicked!");
}

public static void main(String[] args) {


new EventDemo();
}
}

13. Text Stream (File Handling)


import [Link].*;
import [Link];

class FileIOExample {
public static void main(String[] args) {
try {
Scanner sc = new Scanner([Link]);
[Link]("Enter text to write: ");
String data = [Link]();

FileWriter fw = new FileWriter("[Link]");


[Link](data);
[Link]();
[Link]("Data written to file successfully.");

BufferedReader br = new BufferedReader(new FileReader("[Link]"));


[Link]("File Content: " + [Link]());
[Link]();
} catch (IOException e) {
[Link]();
}
}
}

You might also like