Thursday, February 29, 2024

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;
    }
}

No comments:

Post a Comment

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