1.
Demonstration of Programs on Command Line Arguments
public class Factorial {
public static void main(String[] args) {
if ([Link] < 1) {
[Link]("Please provide a number to calculate its
factorial.");
return;
}
try {
int num = [Link](args[0]);
if (num < 0) {
[Link]("Factorial is not defined for negative numbers.");
return;
}
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
[Link]("The factorial of " + num + " is: " + factorial);
} catch (NumberFormatException e) {
[Link]("Please provide a valid integer.");
}
}
}
----------------------
public class PalindromeCheck {
public static void main(String[] args) {
if ([Link] < 1) {
[Link]("Please provide a string to check.");
return;
}
String input = args[0];
String reversed = new StringBuilder(input).reverse().toString();
if ([Link](reversed)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
}
}
-------------
public class SumArgs {
public static void main(String[] args) {
if ([Link] < 2) {
[Link]("Please provide two numbers as arguments.");
return;
}
try {
int num1 = [Link](args[0]);
int num2 = [Link](args[1]);
int sum = num1 + num2;
[Link]("The sum is: " + sum);
} catch (NumberFormatException e) {
[Link]("Please provide valid integers.");
}
}
}
2. Demonstration of Creation of Class, Objects
// Define a class
class Car {
// Attributes
String make;
String model;
int year;
// Constructor to initialize the object
public Car(String make, String model, int year) {
[Link] = make;
[Link] = model;
[Link] = year;
}
// Method to display car details
public String displayDetails() {
return year + " " + make + " " + model;
}
// Method to start the car
public String start() {
return "The " + model + " is starting.";
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Create objects (instances of the class)
Car car1 = new Car("Toyota", "Corolla", 2020);
Car car2 = new Car("Honda", "Civic", 2021);
// Access object methods
[Link]("Car 1: " + [Link]());
[Link]("Car 2: " + [Link]());
// Call methods on objects
[Link]([Link]());
[Link]([Link]());
}
}
3. Programs on Method Overloading and Constructor Overloading
//Simple Program for Method Overloading
public class MethodOverloadingExample {
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
// Overloaded method to add three numbers
public int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
MethodOverloadingExample obj = new MethodOverloadingExample();
// Call the method with two parameters
[Link]("Sum of 10 and 20: " + [Link](10, 20));
// Call the method with three parameters
[Link]("Sum of 10, 20, and 30: " + [Link](10, 20, 30));
}
}
//Simple Program for Constructor Overloading
public class ConstructorOverloadingExample {
int number;
String name;
// Constructor with one parameter
public ConstructorOverloadingExample(int num) {
number = num;
name = "Default Name";
}
// Overloaded constructor with two parameters
public ConstructorOverloadingExample(int num, String str) {
number = num;
name = str;
}
public void display() {
[Link]("Number: " + number + ", Name: " + name);
}
public static void main(String[] args) {
// Create an object using the constructor with one parameter
ConstructorOverloadingExample obj1 = new
ConstructorOverloadingExample(10);
[Link]();
// Create an object using the constructor with two parameters
ConstructorOverloadingExample obj2 = new
ConstructorOverloadingExample(20, "John");
[Link]();
}
}
4. Programs on Inheritance and Method Overriding
// Parent Class
class Animal {
void sound() {
[Link]("Animal makes a sound.");
}
}
// Child Class
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks: Woof! Woof!");
}
}
// Main Class
public class SmallInheritanceExample {
public static void main(String[] args) {
Animal animal = new Animal();
[Link](); // Calls the parent class method
Dog dog = new Dog();
[Link](); // Calls the overridden method in Dog class
Animal polyAnimal = new Dog();
[Link](); // Demonstrates polymorphism (calls Dog's
method)
}
}
5. Program on demonstrations of Input Output Stream.
import [Link].*;
import [Link];
class FileIO{
void File()
{
try {
InputStream input = new FileInputStream("fi[Link]");
int size = [Link]();
[Link]("Available byte:"+size);
byte [] array = new byte[size];
[Link](array);
String data=new String(array);
[Link]("Data in File\n"+data);
[Link]();
}
catch(FileNotFoundException e)
{
[Link]("Error:File Not Found.");
}
catch(IOException e){
[Link]();
}
}
void Out(){
try {
Scanner sc = new Scanner([Link]);
OutputStream out = new FileOutputStream("fi[Link]");
[Link]("Enter Data to write:-");
String data =[Link]();
byte [] bdata = [Link]();
[Link](bdata);
[Link]("Data Written successfully.");
} catch (IOException e) {
[Link]();
}
}
}
class IOStream{
public static void main(String[] args) {
FileIO f = new FileIO();
[Link]();
[Link]();
}
}
6. Program on Creation of Packages, importing the user defined
Package.
//Step 1: Create the package
Create a package named mypackage with a class MyClass.
File: [Link]
package mypackage;
public class MyClass {
public void displayMessage() {
[Link]("Hello from MyClass inside mypackage!");
}
public int addNumbers(int a, int b) {
return a + b;
}
}
//Step 2: Compile the package
1. Save the [Link] file inside a folder named mypackage.
2. Open the terminal/command prompt, navigate to the directory
containing the mypackage folder, and run:
javac mypackage/[Link]
Step 3: Create a program to use the package
Create another Java file to import and use the mypackage.
File: [Link]
import [Link];
public class MainProgram {
public static void main(String[] args) {
MyClass myObject = new MyClass();
[Link]();
int sum = [Link](10, 20);
[Link]("The sum of 10 and 20 is: " + sum);
}
}
Step 4: Compile and run the program
javac [Link]
java MainProgram
7. Demonstration of Multithreading using Thread Class and Runnable
Interface.
// Using Thread Class
class MyThread extends Thread {
@Override
public void run() {
[Link]("Thread Class: Running in " +
[Link]().getName());
}
}
// Using Runnable Interface
class MyRunnable implements Runnable {
@Override
public void run() {
[Link]("Runnable Interface: Running in " +
[Link]().getName());
}
}
public class SimpleMultithreadingDemo {
public static void main(String[] args) {
// Using Thread Class
MyThread thread1 = new MyThread();
// Using Runnable Interface
Thread thread2 = new Thread(new MyRunnable());
// Start the threads
[Link]();
[Link]();
// Main thread task
[Link]("Main Thread: Running in " +
[Link]().getName());
}
}
8. Demonstration of GUI using Swings
import [Link].*;
import [Link];
import [Link];
public class SimpleSwingExample {
public static void main(String[] args) {
// Create a JFrame
JFrame frame = new JFrame("Simple Swing Example");
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
// Create a JLabel
JLabel label = new JLabel("Click the button!");
[Link](100, 50, 150, 30);
[Link](label);
// Create a JButton
JButton button = new JButton("Click Me");
[Link](100, 100, 100, 30);
[Link](button);
// Add an ActionListener to the button
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked!");
}
});
// Make the frame visible
[Link](true);
}
}