0% found this document useful (0 votes)
9 views18 pages

Java Exp.8,9,10,11

Java program for content sorting,creating analog clock using applet,create calculator using swings,crateing ms word using swings

Uploaded by

princetaak1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
9 views18 pages

Java Exp.8,9,10,11

Java program for content sorting,creating analog clock using applet,create calculator using swings,crateing ms word using swings

Uploaded by

princetaak1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
[Link]: suresh 56 Mahesh 89 Shyam 61 Vikas 92 Shloka 84 Nalini 62 Abhi 71 Bhavani 68 SOURCE CODE: // sorting the content of a given text file import [Link]. BufferedReader, import [Link]; import [Link] FileReader, import [Link]; import [Link].]OExceptior import [Link] ArrayList; import [Link]; import [Link]. Comparator, 1 Student Class class Student { String name; int marks; Public Student(String name, int marks) { [Link] = name; this. marks = marks; } } 1 nameCompare Class to compare the names class NameCompare implements Comparator { @Override public int compare(Student s1, Student s2) { retum [Link]([Link]); } } 1 marksCompare Class to compare the marks class MarksCompare implements Comparator { @Override public int compare(Student s1, Student s2) { return [Link] - [Link]; 7 } public class SortTextFile { public static void main(Stringl] args) { try{ 1! Creating BufferedReader object to read the input text file BufferedReader reader = new BufferedReader(new FileReader("input txt"); 11 Creating ArrayList to hold Student objects ArrayList studentRecords = new ArrayList<>(); // Reading Student records one by one String currentLine = [Link](); while (currentLine != null) { Stringl} studentDetail = [Link](""); String name = studentDetail(0}; int marks = [Link](studentDetaill1)); 1/ Creating Student object for every student record and adding it to ArrayList [Link](new Student(name, marks)); currentLine = [Link](); 2 // Closing the reader [Link](); // Sorting ArrayList studentRecords based on marks [Link](studentRecords, new MarksCompare()); 11 Creating BufferedWriter object to write into output text file BufferedWriter writer = new ButferedWriter(new FileWriter(“[Link]")); J Mriting every studentRecords into output text file for (Student student : studentRecords) { [Link]([Link]); [Link](” " + student. marks); [Link](); } 11 Closing the writer [Link](); } catch (IOException e) { [Link](); } } } OUTPUT: [Link] : Vikas 92 Mahesh 89 Shloka 84 Shyam 81 Abhi 71 Bhavani 68 Nalini 62 Suresh 56 SOURCE CODE: // to develop an analog clock using an applet . // Java program to illustrate // analog clock using Applets import [Link]. Applet import [Link].*; import [Link].*; public class analogClock extends Applet { @override public void init() { // Applet window size & color [Link](new Dimension(800, 400)); setBackground(new Color (50, 50, 50); new Taread() { @override public void run() { while (true) { repaint(); delayAnimation() ; } }.start(); } // Animating the applet private void delayAnimation() { try { // Animation delay is 1000 milliseconds Thread. sleep (1000) ; } catch (InterruptedException e) { [Link]() ; } } // Paint the applet @override public void paint (Graphics g) { // Get the system time Calendar time = [Link]() ; int hour = [Link] (Calendar .HOUR_OF_DAY) ; int minute = [Link] (Calendar .MINUTE) ; int second = time. get (Calendar SECOND) ; // 12 hour format if (hour > 12) { hour -= 12; } // Draw clock body center at (400, 200) [Link] ([Link]) ; g.£illoval (300, 100, 200, 200); // Labeling [Link] (Color .black) ; [Link]("12", 390, 120); g-drawString("9", 310, 200); [Link]("6", 400, 290); [Link]("3", 480, 200); // Declaring variables to be used double angle; int x, yi // Second hand's angle in Radian angle = [Link]((15 - second) * 6); // Position of the second hand // with length 100 unit x = (int) ([Link] (angle) * 100); y = (int) ([Link] (angle) * 100); // Red color second hand [Link] (Color. red) ; [Link](400, 200, 400 + x, 200 - y); // Minute hand's angle in Radian angle = Math. toRadians((15 - minute) * 6); // Position of the minute hand // with length 80 unit x = (int) ([Link] (angle) * 80); y = (int) ([Link](angle) * 80); // blue color Minute hand [Link] (Color .blue) ; [Link] (400, 200, 400 + x, 200 - y); // Hour hand's angle in Radian angle = [Link]((15 - (hour * 5)) * 6); // Position of the hour hand // with length 50 unit x = (int) ([Link] (angle) * 50); y = (int) ([Link] (angle) * 50); // Black color hour hand [Link] ([Link]) ; g-drawLine (400, 200, 400 + x, 200 - y); Here's the HTML code to embed the applet: OUTPUT: This will display a clock with moving hour, minute, and second hands to represent the current time. Tle SOURCE CODE: // to develop a scientific calculator using swings. import [Link].*; import [Link].*; import [Link]."; class Calculator extends JFrame implements ActionListener { private static final long serialVersionUID = 1; // Adding serial version UID 1/ Declare components private JTextField displayField; private String operand1, operand2, operator, /{ Constructor Calculator() { operandi = operand2 = operator = ""; J Set up the frame sefTitle(*Calculator’); setSize(300, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new BorderLayout()); 11 Create and add display field displayField = new JTextField(16); [Link](false); add(displayField, BorderLayout. NORTH); // Create and add button panel JPanel buttonPanel = new JPanel(new GridLayout(s, 4)); addButtons(buttonPanel); add(buttonPanel, BorderLayout CENTER); } /{ Method to add buttons to the panel private void addButtons(JPanel panel) { String[] buttonLabels T "9", "4", hk for (String label : buttonLabels) { Button button = new JButton(label); [Link](this); [Link](button); } ) 1/ ActionListener implementation Public void actionPerformed(ActionEvent e) { String command = [Link](); switch (command) { case 'C’ clearDisplay(); break; case" calculateResult(); break; case "+" case" case case": setOperator(command); break; default: appendToDisplay(command); break; } } // Method to append a digit or decimal point to the display private void appendToDisplay(String input) { if ([Link]()) { operand! += input; [Link](operand1); }else { operand2 += input; [Link](operand2); } } 1 Method to set the operator private void setOperator(String op) { operator = op; [Link](operandt + operator); } // Method to calculate and display the result private void calculateResult() { if (loperand1 .isEmpty() && [Link]() && ![Link]()) { double result = 0; double num = [Link](operand1); double num2 = Double. parseDouble(operand2); switch (operator) { case "+": result = numt + num2; breal case"! result = numt - num2; break; case "*": result = num1 * num2; breal case": if (num2 != 0) ( result = numt /num2; jelse { [Link](this, "Error: Division by zero", "Error", JOptionPane.ERROR_MESSAGE); clearDisplay(); retum; } break; } [Link]([Link](result); operand! = [Link](result); operand2 = operatoi } } // Method to clear the display and reset operands and operator private void clearDisplay() { operand! = operand? = operator [Link]("); } /{ Main method Public static void main(String[] args) { ‘[Link](() -> { new Calculator().setVisible(true); ym } } OUTPUT: laa. oO x P+ oe | Ty T-]-]— —) SOURCE CODE: //Create an editor like MS-word using swings import [Link].*; import [Link]. DocumentEvent; import [Link]; import [Link]; // Add this import for UndoableEdit import [Link]." import [Link].*; import [Link]"; import [Link] ArrayList; import [Link]; // Add this import for PrinterException class Editor extends JFrame implements ActionListener, DocumentListener { private static final long serialVersionUID = private JTextArea textArea; private File currentFile; private boolean isTextModified; private ArrayList undoBuffer; private ArrayList redoButfer; Editor() ( 11 Create the fram sefTitle("Text Editor setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setSize(800, 600); setLocationRelativeTo(null); // Create a menu bar JMenuBar menuBar = new JMenuBar(); // Create File menu JMenu fileMenu = new JMenu("File"); JMenultem newMenultem = new JMenultem("New’); JMenultem openMenultem = new JMenultem("Open'); JMenultem saveMenultem = new JMenultem("Save"); JMenultem saveAsMenultem = new JMenultem("Save As"); JMenultem printMenultem = new JMenultem('Print’); JMenultem exitMenultem = new JMenultem("Exit"); /) Add action listeners to File menu items [Link] (this); ‘[Link](thi [Link](this); [Link](this); [Link] (this); [Link](this); 1/ Add File menu items [Link](newMenultem); [Link](openMenultem); [Link](saveMenultem); [Link](saveAsMenultem); [Link](); [Link](printMenultem); [Link](); [Link](exitMenultem); Create Edit menu JMenu editMenu = new JMenu("Edit"); JMenultem undoMenultem = new JMenultem("Undo’); JMenultem redoMenultem = new JMenultem("Redo") JMenultem cutMenultem = new JMenultem("Cut"); JMenultem copyMenultem = new JMenultem("Copy"); JMenultem pasteMenultem = new JMenultem("Paste"); 1] Add action listeners to Edit menu items [Link](this); [Link](this); [Link](this); [Link](this); [Link] Listener(this); 11 Add Edit menu items [Link](undoMenultem); [Link](redoMenultem); [Link](); [Link](cutMenultem); [Link](copyMenultem); [Link](pasteMenultem); 1] Add menus to menu bar [Link]{ileMenu); [Link](editMenu); 11 Set menu bar to frame setJMenuBar(menuBar); 1 Create text area textArea = new JTextArea(): [Link]().addDocumentListener (this); JSorollPane scrollPane = new JScrollPane(textArea); 1/ Add text area to frame add(scroliPane, BorderLayout. CENTER); currentFile = null; isTextModified = false; undoButfer = new ArrayList<>(); redoBuffer = new ArrayList<>(); 1] Add window listener for closing the application addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { closeEditor(); } ys 1/ Set frame visible setVisible(true); } Public void actionPerformed(ActionEvent e) { String command = [Link](); switch (command) { case "New: newFile(); break; case "Open openFile(); break; case "Save": saveFile(); break; case "Save As": saveFileAs(); break; case "Print printFile(); break; case "Undo": undoEdit(); break; case "Redo’ redoEdit(); break; case "Cut": [Link](); break; case "Copy": [Link](): break; case "Paste" textArea paste); break; case "Exit': closeEditor(); break; default: break; } } private void newFile() { if (isTextModified) { int choice = [Link](this, "Do you want to save changes to the current file?", "Save changes", JOptionPane.YES_NO_CANCEL_OPTION); if (choice == JOptionPane.YES_OPTION) { saveFile(); } else if (choice == JOptionPane.CANCEL_OPTION) { retum; } } [Link]("); currentFile = null; isTextModified = false; setTitle("Text Editor"); } private void openFile() { JFileChooser fileChooser = new JFileChooser(); int response = [Link](this); if (response == JFileChooser.APPROVE_OPTION) { File file = [Link](); try (ButferedReader reader = new ButferedReader(new FileReader(file))) StringBuilder text = new StringBuilder(); String line; while ((line = [Link]()) != null) { [Link](ine).append("\n"); } [Link]([Link]()); currentFile = file; TextModified = false; setTitle("Text Editor - " + [Link]()); } catch (IOException ex) { [Link](this, "Error opening file: "+ [Link](), "Error", JOptionPane.ERROR_MESSAGE); } } } private void saveFile() { if (currentFile == null) { saveFileAs(); }else { try (BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile))) { [Link]([Link]()); isTextModified = false; }eatch (IOException ex) { [Link](this, "Error saving file: " + [Link](), "Error", JOptionPane.ERROR_MESSAGE); } } } private void saveFileAs() { JFileChooser fileChooser = new JFileChooser(); int response = [Link](this); if (response == JFileChooser. APPROVE_OPTION) { File file = [Link](); try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { [Link]([Link]()); currentFile = file; ‘extModified = false; setTitle("Text Editor - " + [Link]()); } catch (IOException ex) { [Link](this, "Error saving file: " + [Link](), "Error", JOptionPane.ERROR_MESSAGE); } } } private void printFile() { try{ textArea print(); } catch (PrinterException ex) { [Link](this, "Error printing file: " + [Link](), "Error", JOptionPane.ERROR_MESSAGE); } } private void undoEdit() { if ([Link]()) { UndoableEdit edit = [Link]([Link]() - 1); [Link]\edit); [Link](); } } private void redoEdit() { if ([Link]()) { UndoableEdit edit = [Link]([Link]() - 1); [Link](edit); edit redo); } } private void closeEditor() { if (isTextModified) { int choice = [Link](this, "Do you want to save changes to the current file?", "Save changes", JOptionPane.YES_NO_CANCEL_OPTION); if (choice == JOptionPane. YES_OPTION) { saveFile(); } else if (choice == JOptionPane.CANCEL_OPTION) { return; } } dispose(); } i DocumentListener methods @Override public void insertUpdate(DocumentEvent e) { isTextModified = true; 2 @Override public void removeUpdate(DocumentEvent e) { isTextModified = true; Z @Override public void changedUpdate(DocumentEvent e) { isTextModified = true; } public static void main(String[] args) { new Editor() } } —_ x] at ce |

You might also like