From 14898356a8d8acc87e82b7007e36e1edfcebd98d Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Mon, 5 Jun 2023 03:15:38 +0530 Subject: [PATCH] P6-P15: init Signed-off-by: Amneesh Singh --- P10.java | 85 ++++++++++++++++++++++++++++++ P11.java | 31 +++++++++++ P12.java | 43 ++++++++++++++++ P13.html | 9 ++++ P13.java | 99 +++++++++++++++++++++++++++++++++++ P14.html | 9 ++++ P14.java | 49 ++++++++++++++++++ P15.in | 3 ++ P15.java | 46 +++++++++++++++++ P6.in | 3 ++ P6.java | 20 ++++++++ P7.in | 6 +++ P7.java | 25 +++++++++ P8.html | 9 ++++ P8.java | 84 ++++++++++++++++++++++++++++++ P9.java | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 16 files changed, 675 insertions(+) create mode 100644 P10.java create mode 100644 P11.java create mode 100644 P12.java create mode 100644 P13.html create mode 100644 P13.java create mode 100644 P14.html create mode 100644 P14.java create mode 100644 P15.in create mode 100644 P15.java create mode 100644 P6.in create mode 100644 P6.java create mode 100644 P7.in create mode 100644 P7.java create mode 100644 P8.html create mode 100644 P8.java create mode 100644 P9.java diff --git a/P10.java b/P10.java new file mode 100644 index 0000000..eb56b62 --- /dev/null +++ b/P10.java @@ -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); + } + }); + } +} diff --git a/P11.java b/P11.java new file mode 100644 index 0000000..18096ba --- /dev/null +++ b/P11.java @@ -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); + } +} diff --git a/P12.java b/P12.java new file mode 100644 index 0000000..cf913be --- /dev/null +++ b/P12.java @@ -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); + } +} diff --git a/P13.html b/P13.html new file mode 100644 index 0000000..90067a7 --- /dev/null +++ b/P13.html @@ -0,0 +1,9 @@ + + + Tic Tac Toe + + + + + + diff --git a/P13.java b/P13.java new file mode 100644 index 0000000..51112a3 --- /dev/null +++ b/P13.java @@ -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; + } +} diff --git a/P14.html b/P14.html new file mode 100644 index 0000000..1e0e41b --- /dev/null +++ b/P14.html @@ -0,0 +1,9 @@ + + + Moving Banner + + + + + + diff --git a/P14.java b/P14.java new file mode 100644 index 0000000..c25e81c --- /dev/null +++ b/P14.java @@ -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); + } +} diff --git a/P15.in b/P15.in new file mode 100644 index 0000000..55c6c57 --- /dev/null +++ b/P15.in @@ -0,0 +1,3 @@ +alo, i am +a decapitated corpse +speaking, good morning diff --git a/P15.java b/P15.java new file mode 100644 index 0000000..2b9a196 --- /dev/null +++ b/P15.java @@ -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'; + } +} diff --git a/P6.in b/P6.in new file mode 100644 index 0000000..55c6c57 --- /dev/null +++ b/P6.in @@ -0,0 +1,3 @@ +alo, i am +a decapitated corpse +speaking, good morning diff --git a/P6.java b/P6.java new file mode 100644 index 0000000..a3e6a4e --- /dev/null +++ b/P6.java @@ -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()); + } + } +} diff --git a/P7.in b/P7.in new file mode 100644 index 0000000..1157734 --- /dev/null +++ b/P7.in @@ -0,0 +1,6 @@ +carbon +coffee +kojima +lester +mole +studios diff --git a/P7.java b/P7.java new file mode 100644 index 0000000..3d54ef1 --- /dev/null +++ b/P7.java @@ -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 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); + } + } +} diff --git a/P8.html b/P8.html new file mode 100644 index 0000000..6a4bd37 --- /dev/null +++ b/P8.html @@ -0,0 +1,9 @@ + + + Analog Clock + + + + + + diff --git a/P8.java b/P8.java new file mode 100644 index 0000000..57be798 --- /dev/null +++ b/P8.java @@ -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); + } +} diff --git a/P9.java b/P9.java new file mode 100644 index 0000000..15f07e5 --- /dev/null +++ b/P9.java @@ -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(); + } + }); + } +}