You are on page 1of 11

import java.io.

*;
import java.util.ArrayList;
import java.util.Collections;

public class FileSorter {


public static void main(String[] args) {
String filePath = "input.txt";

// Step 1: Read the content of the file into an ArrayList


ArrayList<String> lines = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
return;
}

// Step 2: Sort the ArrayList


Collections.sort(lines);

// Step 3: Write the sorted content back to the file


try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
for (String sortedLine : lines) {
writer.write(sortedLine);
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AnalogClock extends Applet implements Runnable {


private Thread thread;
private SimpleDateFormat dateFormat;
private int centerX, centerY, radius;

@Override
public void init() {
setBackground(Color.WHITE);
dateFormat = new SimpleDateFormat("hh:mm:ss");
}

@Override
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}

@Override
public void stop() {
if (thread != null) {
thread.interrupt();
thread = null;
}
}

@Override
public void run() {
while (thread != null) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public void paint(Graphics g) {
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);

drawClock(g);
drawHourHand(g, hours, minutes);
drawMinuteHand(g, minutes, seconds);
drawSecondHand(g, seconds);
}

private void drawClock(Graphics g) {


int width = getWidth();
int height = getHeight();
centerX = width / 2;
centerY = height / 2;
radius = Math.min(width, height) / 2 - 20;

g.setColor(Color.BLACK);
g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);

g.setColor(Color.BLUE);
g.fillOval(centerX - 5, centerY - 5, 10, 10);
}

private void drawHourHand(Graphics g, int hours, int minutes) {


double hourAngle = Math.toRadians(360 * ((hours % 12) + minutes / 60.0) / 12);
drawHand(g, hourAngle, radius * 0.5);
}

private void drawMinuteHand(Graphics g, int minutes, int seconds) {


double minuteAngle = Math.toRadians(360 * ((minutes % 60) + seconds / 60.0) / 60);
drawHand(g, minuteAngle, radius * 0.7);
}

private void drawSecondHand(Graphics g, int seconds) {


double secondAngle = Math.toRadians(360 * (seconds % 60) / 60);
drawHand(g, secondAngle, radius * 0.9);
}

private void drawHand(Graphics g, double angle, double length) {


int x = (int) (centerX + length * Math.sin(angle));
int y = (int) (centerY - length * Math.cos(angle));
g.drawLine(centerX, centerY, x, y);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ScientificCalculator extends JFrame {


private JTextField displayField;

public ScientificCalculator() {
setTitle("Scientific Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);

displayField = new JTextField();


displayField.setPreferredSize(new Dimension(280, 50));
displayField.setEditable(false);
getContentPane().add(displayField, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel(new GridLayout(5, 4));


getContentPane().add(buttonPanel, BorderLayout.CENTER);

String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"sin", "cos", "tan", "sqrt"
};

for (String label : buttonLabels) {


JButton button = new JButton(label);
button.addActionListener(new ButtonClickListener());
buttonPanel.add(button);
}

setVisible(true);
}

private class ButtonClickListener implements ActionListener {


public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
String currentText = displayField.getText();

// Perform appropriate action based on the command


// You can add your logic here to handle different buttons

displayField.setText(currentText + command);
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class TextEditor extends JFrame {


private JTextArea textArea;

public TextEditor() {
setTitle("Text Editor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);

textArea = new JTextArea();


JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(scrollPane, BorderLayout.CENTER);

JMenuBar menuBar = new JMenuBar();


JMenu fileMenu = new JMenu("File");
JMenuItem openMenuItem = new JMenuItem("Open");
JMenuItem saveMenuItem = new JMenuItem("Save");

openMenuItem.addActionListener(new OpenActionListener());
saveMenuItem.addActionListener(new SaveActionListener());

fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
menuBar.add(fileMenu);

setJMenuBar(menuBar);
setVisible(true);
}

private class OpenActionListener implements ActionListener {


public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showOpenDialog(TextEditor.this);
if (option == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append("\n");
}
reader.close();
textArea.setText(content.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

private class SaveActionListener implements ActionListener {


public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showSaveDialog(TextEditor.this);
if (option == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TextEditor();
}
});
}
}

You might also like