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 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) {       ...