Thursday, February 29, 2024

Java program to implement file input stream class to read binary data from image file.

import java.io.FileInputStream;
import java.io.IOException;

public class ReadImageFile {
    public static void main(String[] args) {
        String imagePath = "yourImageFile.jpg"; // Replace with the actual image file path and name

        try (FileInputStream fileInputStream = new FileInputStream(imagePath)) {
            int byteRead;
            while ((byteRead = fileInputStream.read()) != -1) {
                System.out.print((char) byteRead);
            }
        } catch (IOException e) {
            System.err.println("Error reading the image file: " + e.getMessage());
        }
    }
}

Java program to merge two text files in to third file.

import java.io.*;

public class MergeFiles {
    public static void main(String[] args) {
        String inputFile1 = "inputFile1.txt"; // Replace with the actual file path and name
        String inputFile2 = "inputFile2.txt"; // Replace with the actual file path and name
        String outputFile = "outputFile.txt"; // Replace with the desired output file path and name

        mergeFiles(inputFile1, inputFile2, outputFile);

        System.out.println("Files merged successfully!");
    }

    private static void mergeFiles(String inputFile1, String inputFile2, String outputFile) {
        try (BufferedReader reader1 = new BufferedReader(new FileReader(inputFile1));
             BufferedReader reader2 = new BufferedReader(new FileReader(inputFile2));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {

            String line;

            // Merge content from inputFile1
            while ((line = reader1.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }

            // Merge content from inputFile2
            while ((line = reader2.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }

        } catch (IOException e) {
            System.err.println("Error merging files: " + e.getMessage());
        }
    }
}

Java program to read a text file and displays it's contents on screen.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadTextFile {
    public static void main(String[] args) {
        String filePath = "yourFileName.txt"; // Replace with the actual file path and name

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.err.println("Error reading the file: " + e.getMessage());
        }
    }
}

Java program to check greatest among three numbers where input numbers are given by user.

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GreatestNumberApplet extends Applet implements ActionListener {
    TextField num1Field, num2Field, num3Field;
    Label resultLabel;

    public void init() {
        Label num1Label = new Label("Enter Number 1:");
        num1Field = new TextField(10);

        Label num2Label = new Label("Enter Number 2:");
        num2Field = new TextField(10);

        Label num3Label = new Label("Enter Number 3:");
        num3Field = new TextField(10);

        Button checkButton = new Button("Check Greatest");
        checkButton.addActionListener(this);

        resultLabel = new Label("");

        // Set background color
        setBackground(Color.lightGray);

        // Adding components to the applet
        add(num1Label);
        add(num1Field);
        add(num2Label);
        add(num2Field);
        add(num3Label);
        add(num3Field);
        add(checkButton);
        add(resultLabel);
    }

    public void actionPerformed(ActionEvent e) {
        int num1 = Integer.parseInt(num1Field.getText());
        int num2 = Integer.parseInt(num2Field.getText());
        int num3 = Integer.parseInt(num3Field.getText());

        int greatestNumber = findGreatestNumber(num1, num2, num3);

        resultLabel.setText("Greatest Number: " + greatestNumber);
    }

    private int findGreatestNumber(int a, int b, int c) {
        int max = a;

        if (b > max) {
            max = b;
        }

        if (c > max) {
            max = c;
        }

        return max;
    }
}

Java Applet program to draw rectangle and right-aligned oval of orange color.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class RectangleAndOvalApplet extends Applet { 
    public void paint(Graphics g) { 
   // Draw a rectangle with orange color
   g.setColor(Color.ORANGE); 
   g.drawRect(50, 50, 100, 80); 

   // Draw a right-aligned oval with orange color     g.setColor(Color.ORANGE); 
   int ovalX = getWidth() - 150; // Right-aligned
   int ovalY = 50; 
   int ovalWidth = 100; 
   int ovalHeight = 80; 
   g.fillOval(ovalX, ovalY, ovalWidth, ovalHeight); }
}

Java program to calculate area and perimeter of circle using interfaces.

interface Shape {
    double calculateArea();
}

interface CircleProperties {
    double calculatePerimeter();
}

class Circle implements Shape, CircleProperties {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double calculateArea() {
        return Math.PI * radius * radius;
    }

    public double calculatePerimeter() {
        return 2 * Math.PI * radius;
    }
}

public class CircleCalculator {
    public static void main(String[] args) {
        Circle myCircle = new Circle(5.0);

        System.out.println("Area of the circle: " + myCircle.calculateArea());
        System.out.println("Perimeter of the circle: " + myCircle.calculatePerimeter());
    }
}

Java program to check marks out of Bound exception.

import java.util.Scanner;

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

        System.out.print("Enter the marks: ");
        int marks = scanner.nextInt();

        try {
            validateMarks(marks);
            System.out.println("Marks are within the valid range.");
        } catch (MarksOutOfBoundsException e) {
            System.out.println("Exception: " + e.getMessage());
        }
    }

    private static void validateMarks(int marks) throws MarksOutOfBoundsException {
        if (marks > 100) {
            throw new MarksOutOfBoundsException("Marks out of Bound: Marks cannot be greater than 100.");
       }
    }
}

Java program to find length of given string and convert it in to upper or lower case.

import java.util.Scanner;

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

        System.out.print("Enter a string: ");
        String inputString = scanner.nextLine();

        int length = inputString.length();

        System.out.println("Length of the string: " + length);

        System.out.println("Choose an operation:");
        System.out.println("1. Convert to Uppercase");
        System.out.println("2. Convert to Lowercase");

        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                System.out.println("Uppercase: " + inputString.toUpperCase());
                break;
            case 2:
                System.out.println("Lowercase: " + inputString.toLowerCase());
                break;
            default:
                System.out.println("Invalid choice");
        }
    }
}

Write a Java program to calculate the gross salary of an employee, if basic salary of that employee is greater or equal to 20000 and year's of service is equal or greater than 5 years bonus will be 2000 else1000, TA =10%, DA= 20% and HRA=5% of salary.

import java.util.Scanner;

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

        System.out.print("Enter the basic salary of the employee: ");
        double basicSalary = scanner.nextDouble();

        System.out.print("Enter the years of service of the employee: ");
        int yearsOfService = scanner.nextInt();

        double bonus = (basicSalary >= 20000 && yearsOfService >= 5) ? 2000 : 1000;
        double ta = 0.10 * basicSalary;
        double da = 0.20 * basicSalary;
        double hra = 0.05 * basicSalary;

        double grossSalary = basicSalary + ta + da + hra + bonus;

        System.out.println("Gross Salary of the employee: " + grossSalary);
    }
}

Java program to print table of n number from given range m to n.

import java.util.Scanner;
public class TableFromMtoN{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the range of numbers from m to n for the multiplication table: ");
        int m = scanner.nextInt();
        int n = scanner.nextInt();
if(m<=n){
int x=m;
while(x<=n){
        System.out.println("Multiplication table for " + x + ":");

        for (int i = 1; i <= 10; i++) {
            int result = x * i;
            System.out.println(x + " * " + i + " = " + result);
        }
        x++;
       }
}
else{
System.out.println(" Numbers Range is Wrong,try Again");
}
}
}

Java program to print fibonacci series of n numbers.

import java.util.*;

public class Fib{

public static void main(String args[]){

int a,b,c,n;

Scanner s=new Scanner(System.in);

System.out.println("How many terms do you want in fibonacci series\n");

n=s.nextInt();

if(n>0){

System.out.println("Enter initial two numbers\n");

a=s.nextInt();

b=s.nextInt();

System.out.println("Fibonacci series of"+n+ "term is:\n");

if(n==1){

System.out.println(a);

System.exit(0);}

if(n==2){

System.out.println(a+" "+b);

System.exit(0);}

System.out.print(a+" "+b+" ");

int i;

for(i=1;i<=n-2;i++){

c=a+b;

System.out.print(c+" ");

a=b;

b=c;

}

}

else{

System.out.println("Wrong number of term");

}

}

}

Java Program to calculate Factorial of a given number

import java.util.*;

class Factorial{

public static void main(String args[]){

long fact=1,num;

int i;

Scanner s=new Scanner(System.in);

System.out.println("Enter number");

num=s.nextLong();

if(num>0){

for(i=1;i<=num;i++)

fact=fact*i;

System.out.println("Factorial of"+num+"="+fact);

}

else{

System.out.println("wrong number");

}}}

Java program to check greater number between two numbers using conditional operator.

import java.util.Scanner;

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

        System.out.print("Enter the first number: ");
        int number1 = scanner.nextInt();

        System.out.print("Enter the second number: ");
        int number2 = scanner.nextInt();

        int greaterNumber = (number1 > number2) ? number1 : number2;

        System.out.println("The greater number is: " + greaterNumber);
    }
}

Java program to implement file input stream class to read binary data from image file.

import java.io.FileInputStream; import java.io.IOException; public class ReadImageFile {     public static void main(String[] args) {       ...