85
P10.java
Normal file
85
P10.java
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class P10 extends JFrame {
|
||||||
|
private JTextArea textArea;
|
||||||
|
private JFileChooser fileChooser;
|
||||||
|
|
||||||
|
public P10() {
|
||||||
|
setTitle("Swing Text Editor");
|
||||||
|
setSize(800, 600);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
textArea = new JTextArea();
|
||||||
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
||||||
|
add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
fileChooser = new JFileChooser();
|
||||||
|
|
||||||
|
JMenuBar menuBar = new JMenuBar();
|
||||||
|
JMenu fileMenu = new JMenu("File");
|
||||||
|
JMenuItem openMenuItem = new JMenuItem("Open");
|
||||||
|
JMenuItem saveMenuItem = new JMenuItem("Save");
|
||||||
|
JMenuItem exitMenuItem = new JMenuItem("Exit");
|
||||||
|
|
||||||
|
openMenuItem.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
int returnVal = fileChooser.showOpenDialog(P10.this);
|
||||||
|
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
|
File file = fileChooser.getSelectedFile();
|
||||||
|
try {
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
|
textArea.read(reader, null);
|
||||||
|
reader.close();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
saveMenuItem.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
int returnVal = fileChooser.showSaveDialog(P10.this);
|
||||||
|
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
|
File file = fileChooser.getSelectedFile();
|
||||||
|
try {
|
||||||
|
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
|
||||||
|
textArea.write(writer);
|
||||||
|
writer.close();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exitMenuItem.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fileMenu.add(openMenuItem);
|
||||||
|
fileMenu.add(saveMenuItem);
|
||||||
|
fileMenu.add(exitMenuItem);
|
||||||
|
menuBar.add(fileMenu);
|
||||||
|
setJMenuBar(menuBar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
P10 editor = new P10();
|
||||||
|
editor.setVisible(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
31
P11.java
Normal file
31
P11.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import java.io.IOException;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.Cookie;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public class P11 extends HttpServlet {
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
Cookie[] cookies = request.getCookies();
|
||||||
|
|
||||||
|
boolean isNewVisitor = true;
|
||||||
|
int visitCount = 1;
|
||||||
|
|
||||||
|
if (cookies != null) {
|
||||||
|
for (Cookie cookie : cookies) {
|
||||||
|
if (cookie.getName().equals("visitCount")) {
|
||||||
|
isNewVisitor = false;
|
||||||
|
visitCount = Integer.parseInt(cookie.getValue());
|
||||||
|
visitCount++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Cookie visitCountCookie = new Cookie("visitCount", String.valueOf(visitCount));
|
||||||
|
visitCountCookie.setMaxAge(24 * 60 * 60);
|
||||||
|
response.addCookie(visitCountCookie);
|
||||||
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
|
}
|
||||||
|
}
|
43
P12.java
Normal file
43
P12.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.beans.PropertyChangeSupport;
|
||||||
|
|
||||||
|
public class P12 {
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
private PropertyChangeSupport propertyChangeSupport;
|
||||||
|
|
||||||
|
public P12() {
|
||||||
|
propertyChangeSupport = new PropertyChangeSupport(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
String oldName = this.name;
|
||||||
|
this.name = name;
|
||||||
|
propertyChangeSupport.firePropertyChange("name", oldName, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
if (age < 0) {
|
||||||
|
throw new IllegalArgumentException("Age cannot be negative");
|
||||||
|
}
|
||||||
|
int oldAge = this.age;
|
||||||
|
this.age = age;
|
||||||
|
propertyChangeSupport.firePropertyChange("age", oldAge, age);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPropertyChangeListener(PropertyChangeListener listener) {
|
||||||
|
propertyChangeSupport.addPropertyChangeListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removePropertyChangeListener(PropertyChangeListener listener) {
|
||||||
|
propertyChangeSupport.removePropertyChangeListener(listener);
|
||||||
|
}
|
||||||
|
}
|
9
P13.html
Normal file
9
P13.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Tic Tac Toe</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<applet code="P13.class" width="300" height="300">
|
||||||
|
</applet>
|
||||||
|
</body>
|
||||||
|
</html>
|
99
P13.java
Normal file
99
P13.java
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
import java.applet.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
public class P13 extends Applet {
|
||||||
|
private Button[] buttons;
|
||||||
|
private Label statusLabel;
|
||||||
|
private int currentPlayer;
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
buttons = new Button[9];
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
buttons[i] = new Button("");
|
||||||
|
buttons[i].addActionListener(new ButtonClickListener());
|
||||||
|
}
|
||||||
|
|
||||||
|
statusLabel = new Label("Player 1's turn");
|
||||||
|
statusLabel.setAlignment(Label.CENTER);
|
||||||
|
|
||||||
|
Panel buttonPanel = new Panel(new GridLayout(3, 3));
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
buttonPanel.add(buttons[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
add(buttonPanel, BorderLayout.CENTER);
|
||||||
|
add(statusLabel, BorderLayout.SOUTH);
|
||||||
|
currentPlayer = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ButtonClickListener implements ActionListener {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Button clickedButton = (Button) e.getSource();
|
||||||
|
int buttonIndex = -1;
|
||||||
|
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
if (buttons[i] == clickedButton) {
|
||||||
|
buttonIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!clickedButton.getLabel().equals("") || isGameOver()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentPlayer == 1) {
|
||||||
|
clickedButton.setLabel("X");
|
||||||
|
currentPlayer = 2;
|
||||||
|
statusLabel.setText("Player 2's turn");
|
||||||
|
} else {
|
||||||
|
clickedButton.setLabel("O");
|
||||||
|
currentPlayer = 1;
|
||||||
|
statusLabel.setText("Player 1's turn");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isGameOver()) {
|
||||||
|
if (currentPlayer == 1) {
|
||||||
|
statusLabel.setText("Player 2 wins!");
|
||||||
|
} else {
|
||||||
|
statusLabel.setText("Player 1 wins!");
|
||||||
|
}
|
||||||
|
} else if (isBoardFull()) {
|
||||||
|
statusLabel.setText("It's a draw!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isGameOver() {
|
||||||
|
String[] symbols = { "X", "O" };
|
||||||
|
int[][] winCombinations = {
|
||||||
|
{ 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
|
||||||
|
{ 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 },
|
||||||
|
{ 0, 4, 8 }, { 2, 4, 6 }
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int[] combination : winCombinations) {
|
||||||
|
String symbol = buttons[combination[0]].getLabel();
|
||||||
|
if (!symbol.equals("") &&
|
||||||
|
symbol.equals(buttons[combination[1]].getLabel()) &&
|
||||||
|
symbol.equals(buttons[combination[2]].getLabel())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isBoardFull() {
|
||||||
|
for (Button button : buttons) {
|
||||||
|
if (button.getLabel().equals("")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
9
P14.html
Normal file
9
P14.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Moving Banner</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<applet code="P14.class" width="300" height="300">
|
||||||
|
</applet>
|
||||||
|
</body>
|
||||||
|
</html>
|
49
P14.java
Normal file
49
P14.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import java.applet.Applet;
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
|
||||||
|
public class P14 extends Applet implements Runnable {
|
||||||
|
private String bannerText;
|
||||||
|
private int xCoordinate;
|
||||||
|
private Thread thread;
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
bannerText = "Goofy ahh banner!";
|
||||||
|
xCoordinate = getWidth();
|
||||||
|
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
setForeground(Color.BLACK);
|
||||||
|
setFont(new Font("Arial", Font.BOLD, 20));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
if (thread == null) {
|
||||||
|
thread = new Thread(this);
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
xCoordinate -= 5;
|
||||||
|
|
||||||
|
if (xCoordinate + 200 < 0) {
|
||||||
|
xCoordinate = getWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void paint(Graphics g) {
|
||||||
|
g.clearRect(0, 0, getWidth(), getHeight());
|
||||||
|
g.drawString(bannerText, xCoordinate, 50);
|
||||||
|
}
|
||||||
|
}
|
3
P15.in
Normal file
3
P15.in
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
alo, i am
|
||||||
|
a decapitated corpse
|
||||||
|
speaking, good morning
|
46
P15.java
Normal file
46
P15.java
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class P15 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String filename = "P15.in";
|
||||||
|
|
||||||
|
int letterCount = 0;
|
||||||
|
int vowelCount = 0;
|
||||||
|
int wordCount = 0;
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
||||||
|
int cint;
|
||||||
|
boolean eating = true;
|
||||||
|
|
||||||
|
while ((cint = br.read()) != -1) {
|
||||||
|
char c = (char) cint;
|
||||||
|
|
||||||
|
if (Character.isLetter(c)) {
|
||||||
|
eating = false;
|
||||||
|
letterCount++;
|
||||||
|
|
||||||
|
if (isVowel(Character.toLowerCase(c))) {
|
||||||
|
vowelCount++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!eating) {
|
||||||
|
eating = true;
|
||||||
|
wordCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Letter count: " + letterCount);
|
||||||
|
System.out.println("Vowel count: " + vowelCount);
|
||||||
|
System.out.println("Word count: " + wordCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isVowel(char c) {
|
||||||
|
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
|
||||||
|
}
|
||||||
|
}
|
20
P6.java
Normal file
20
P6.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class P6 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
String filePath = "P6.in";
|
||||||
|
|
||||||
|
StringBuilder content = new StringBuilder();
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
content.append(line.toUpperCase()).append("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
|
||||||
|
writer.write(content.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
P7.java
Normal file
25
P7.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import java.io.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class P7 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
String filePath = "P7.in";
|
||||||
|
|
||||||
|
List<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null)
|
||||||
|
lines.add(line + '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.sort(lines); //sort
|
||||||
|
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
|
||||||
|
for (String line: lines)
|
||||||
|
writer.write(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
P8.html
Normal file
9
P8.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Analog Clock</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<applet code="P8.class" width="300" height="300">
|
||||||
|
</applet>
|
||||||
|
</body>
|
||||||
|
</html>
|
84
P8.java
Normal file
84
P8.java
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import java.applet.Applet;
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
public class P8 extends Applet implements Runnable {
|
||||||
|
Thread t = null;
|
||||||
|
boolean threadSuspended;
|
||||||
|
int hours = 0, minutes = 0, seconds = 0;
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
setBackground(Color.white);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
if (threadSuspended) {
|
||||||
|
threadSuspended = false;
|
||||||
|
synchronized (this) {
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t == null) {
|
||||||
|
t = new Thread(this);
|
||||||
|
threadSuspended = false;
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
threadSuspended = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
hours = cal.get(Calendar.HOUR_OF_DAY) % 12;
|
||||||
|
if (hours == 0)
|
||||||
|
hours += 1;
|
||||||
|
minutes = cal.get(Calendar.MINUTE);
|
||||||
|
seconds = cal.get(Calendar.SECOND);
|
||||||
|
|
||||||
|
repaint();
|
||||||
|
Thread.sleep(1000);
|
||||||
|
if (threadSuspended) {
|
||||||
|
synchronized (this) {
|
||||||
|
while (threadSuspended)
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("Process interrupted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawHand(double angle, int radius, Graphics g) {
|
||||||
|
angle -= 0.5 * Math.PI;
|
||||||
|
int x = (int) (radius * Math.cos(angle));
|
||||||
|
int y = (int) (radius * Math.sin(angle));
|
||||||
|
g.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2 + x, getHeight() / 2 + y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void paint(Graphics g) {
|
||||||
|
int radius = Math.min(getWidth(), getHeight()) / 2 - 10;
|
||||||
|
g.setColor(Color.blue);
|
||||||
|
g.drawOval(getWidth() / 2 - radius, getHeight() / 2 - radius, 2 * radius, 2 * radius);
|
||||||
|
|
||||||
|
g.setColor(Color.black);
|
||||||
|
g.fillOval(getWidth() / 2 - 5, getHeight() / 2 - 5, 10, 10);
|
||||||
|
|
||||||
|
double hourAngle = (hours + minutes / 60.0) * (2 * Math.PI / 12) - 0.5 * Math.PI;
|
||||||
|
double minuteAngle = (minutes + seconds / 60.0) * (2 * Math.PI / 60) - 0.5 * Math.PI;
|
||||||
|
double secondAngle = (seconds * 2 * Math.PI / 60) - 0.5 * Math.PI;
|
||||||
|
|
||||||
|
g.setColor(Color.black);
|
||||||
|
drawHand(hourAngle, radius / 2, g);
|
||||||
|
g.setColor(Color.gray);
|
||||||
|
drawHand(minuteAngle, (int) (0.8 * radius), g);
|
||||||
|
g.setColor(Color.red);
|
||||||
|
drawHand(secondAngle, (int) (0.9 * radius), g);
|
||||||
|
}
|
||||||
|
}
|
154
P9.java
Normal file
154
P9.java
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
public class P9 extends JFrame {
|
||||||
|
private JTextField displayField;
|
||||||
|
|
||||||
|
public P9() {
|
||||||
|
setTitle("Scientific Calculator");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setResizable(true);
|
||||||
|
|
||||||
|
displayField = new JTextField();
|
||||||
|
displayField.setPreferredSize(new Dimension(300, 30));
|
||||||
|
displayField.setEditable(false);
|
||||||
|
|
||||||
|
JPanel buttonPanel = new JPanel(new GridLayout(6, 4, 10, 10));
|
||||||
|
|
||||||
|
String[] buttonLabels = {
|
||||||
|
"7", "8", "9", "/",
|
||||||
|
"4", "5", "6", "*",
|
||||||
|
"1", "2", "3", "-",
|
||||||
|
"0", ".", "=", "+",
|
||||||
|
"sin", "cos", "tan", "sqrt",
|
||||||
|
"e", "^", "pi", "clr"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (String label : buttonLabels) {
|
||||||
|
JButton button = new JButton(label);
|
||||||
|
button.addActionListener(new ButtonClickListener());
|
||||||
|
buttonPanel.add(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
setLayout(new FlowLayout());
|
||||||
|
add(displayField);
|
||||||
|
add(buttonPanel);
|
||||||
|
|
||||||
|
pack();
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ButtonClickListener implements ActionListener {
|
||||||
|
public void actionPerformed(ActionEvent event) {
|
||||||
|
String command = event.getActionCommand();
|
||||||
|
String expression = displayField.getText();
|
||||||
|
if ("=".equals(command)) {
|
||||||
|
calculate();
|
||||||
|
} else if ("clr".equals(command)) {
|
||||||
|
displayField.setText("");
|
||||||
|
} else if ("sin".equals(command)) {
|
||||||
|
displayField.setText(String.valueOf(Math.sin(Double.parseDouble(expression))));
|
||||||
|
} else if ("cos".equals(command)) {
|
||||||
|
displayField.setText(String.valueOf(Math.cos(Double.parseDouble(expression))));
|
||||||
|
} else if ("tan".equals(command)) {
|
||||||
|
displayField.setText(String.valueOf(Math.tan(Double.parseDouble(expression))));
|
||||||
|
} else if ("sqrt".equals(command)) {
|
||||||
|
displayField.setText(String.valueOf(Math.sqrt(Double.parseDouble(expression))));
|
||||||
|
} else {
|
||||||
|
displayField.setText(displayField.getText() + command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calculate() {
|
||||||
|
String expression = displayField.getText();
|
||||||
|
try {
|
||||||
|
double result = evaluateExpression(expression);
|
||||||
|
displayField.setText(String.valueOf(result));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
displayField.setText("Error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private double evaluateExpression(String expression) {
|
||||||
|
return new Object() {
|
||||||
|
int pos = -1, ch;
|
||||||
|
|
||||||
|
void nextChar() {
|
||||||
|
ch = (++pos < expression.length()) ? expression.charAt(pos) : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean eat(int charToEat) {
|
||||||
|
while (ch == ' ') nextChar();
|
||||||
|
if (ch == charToEat) {
|
||||||
|
nextChar();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
double parse() {
|
||||||
|
nextChar();
|
||||||
|
double x = parseExpression();
|
||||||
|
if (pos < expression.length()) throw new RuntimeException("Unexpected character: " + (char) ch);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
double parseExpression() {
|
||||||
|
double x = parseTerm();
|
||||||
|
while(true) {
|
||||||
|
if (eat('+')) x += parseTerm();
|
||||||
|
else if (eat('-')) x -= parseTerm();
|
||||||
|
else return x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double parseTerm() {
|
||||||
|
double x = parseFactor();
|
||||||
|
while (true) {
|
||||||
|
if (eat('*')) x *= parseFactor();
|
||||||
|
else if (eat('/')) x /= parseFactor();
|
||||||
|
else return x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double parseFactor() {
|
||||||
|
if (eat('+')) return parseFactor();
|
||||||
|
if (eat('-')) return -parseFactor();
|
||||||
|
|
||||||
|
double x;
|
||||||
|
int startPos = this.pos;
|
||||||
|
if (eat('(')) {
|
||||||
|
x = parseExpression();
|
||||||
|
eat(')');
|
||||||
|
} else if ((ch >= '0' && ch <= '9') || ch == '.') {
|
||||||
|
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
|
||||||
|
x = Double.parseDouble(expression.substring(startPos, this.pos));
|
||||||
|
} else if (eat('e')) {
|
||||||
|
x = Math.E;
|
||||||
|
} else if (eat('p')) {
|
||||||
|
eat('i');
|
||||||
|
x = Math.PI;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Unexpected character: " + (char) ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eat('^')) x = Math.pow(x, parseFactor());
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}.parse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
new P9();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user