0% found this document useful (0 votes)
2 views

Java Programs Part1

The document contains multiple Java programs demonstrating various functionalities, including text field validation, an airline reservation system, finding the largest and smallest elements in an array, a student information portal, and generating a spiral matrix based on user input. Each program showcases different aspects of Java programming such as GUI components, class structures, and control flow. The examples are practical applications that illustrate fundamental programming concepts.

Uploaded by

Prakash Chandran
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)
2 views

Java Programs Part1

The document contains multiple Java programs demonstrating various functionalities, including text field validation, an airline reservation system, finding the largest and smallest elements in an array, a student information portal, and generating a spiral matrix based on user input. Each program showcases different aspects of Java programming such as GUI components, class structures, and control flow. The examples are practical applications that illustrate fundamental programming concepts.

Uploaded by

Prakash Chandran
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
You are on page 1/ 3

1.

Key Event Listener for TextField Validation


import javax.swing.*;
import java.awt.event.*;

public class TextFieldValidation {


public static void main(String[] args) {
JFrame frame = new JFrame("Validation");
JTextField textField = new JTextField();
textField.setBounds(50, 50, 200, 30);

textField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
String text = textField.getText();
if (!text.matches("[a-zA-Z]*")) {
JOptionPane.showMessageDialog(frame, "Only alphabets allowed");
textField.setText("");
}
}
});

frame.add(textField);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}

2. Airline Reservation System with Flight and Passenger Class


class Flight {
String flightNo, destination;
int seatsAvailable;

Flight(String no, String dest, int seats) {


flightNo = no;
destination = dest;
seatsAvailable = seats;
}

void bookSeat() {
if (seatsAvailable > 0) {
seatsAvailable--;
System.out.println("Booking confirmed");
} else {
System.out.println("No seats available");
}
}

void cancelSeat() {
seatsAvailable++;
System.out.println("Booking cancelled");
}
}
class Passenger {
String name;
int age;

Passenger(String name, int age) {


this.name = name;
this.age = age;
}

void payment(String type) {


System.out.println("Payment done using: " + type);
}
}

3. Largest and Smallest Element in Array


class MinMaxArray {
public static void main(String[] args) {
int[] arr = {12, 45, 2, 67, 34};
int max = arr[0], min = arr[0];
for (int num : arr) {
if (num > max) max = num;
if (num < min) min = num;
}
System.out.println("Max: " + max + ", Min: " + min);
}
}

4. Student Information Portal


import javax.swing.*;
import java.awt.event.*;

public class StudentPortal {


public static void main(String[] args) {
JFrame frame = new JFrame("Student Info Portal");
JTextField name = new JTextField();
JTextField dept = new JTextField();
JTextField regNo = new JTextField();
JButton submit = new JButton("Submit");
JButton reset = new JButton("Reset");

name.setBounds(50, 50, 150, 20);


dept.setBounds(50, 80, 150, 20);
regNo.setBounds(50, 110, 150, 20);
submit.setBounds(50, 140, 100, 20);
reset.setBounds(160, 140, 100, 20);

submit.addActionListener(e -> {
System.out.println("Name: " + name.getText());
System.out.println("Dept: " + dept.getText());
System.out.println("RegNo: " + regNo.getText());
});

reset.addActionListener(e -> {
name.setText("");
dept.setText("");
regNo.setText("");
});

frame.add(name);
frame.add(dept);
frame.add(regNo);
frame.add(submit);
frame.add(reset);
frame.setSize(400, 250);
frame.setLayout(null);
frame.setVisible(true);
}
}

5. Spiral Matrix from User Input


import java.util.*;

class SpiralMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of matrix (n): ");
int n = sc.nextInt();
int[][] matrix = new int[n][n];
int value = 1;
int top = 0, bottom = n-1, left = 0, right = n-1;

while (value <= n*n) {


for (int i = left; i <= right; i++) matrix[top][i] = value++;
top++;
for (int i = top; i <= bottom; i++) matrix[i][right] = value++;
right--;
for (int i = right; i >= left; i--) matrix[bottom][i] = value++;
bottom--;
for (int i = bottom; i >= top; i--) matrix[i][left] = value++;
left++;
}

for (int[] row : matrix) System.out.println(Arrays.toString(row));


}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy