Posts

Showing posts from 2013
<!--    sum.html       Copyright 2013 Ajay Bhatia <ajay@dumb-box>       This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.       This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.       You should have received a copy of the GNU General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,    MA 02110-1301, USA.    ...
/*  * SumApplet.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ import javax.swing.JApplet; import javax.swing.JLabel; import javax.swing.JButto...
Some important questions (Unit-Wise) https://anonfiles.com/file/64a1567c5237040cbf28c2aa244b7a00
// Reading serializable objects from file import java.io.File; import java.io.FileInputStream; import java.io.ObjectInputStream; public class StudentRead {     public static void main(String[] args) throws Exception {         ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("student.ser")));                Student student = (Student)in.readObject();         System.out.println(student.getName());                student = (Student)in.readObject();         System.out.println(student.getName());     } }
// Write Student objects to file import java.io.File; import java.io.FileOutputStream; import java.io.ObjectOutputStream; public class StudentWrite {     public static void main(String[] args) throws Exception {         ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("student.ser")));            Student st1 = new Student();         st1.setRollNo(1102107L);         st1.setName("Abhishek Bhatia");         st1.setCourse("BCA");                Student st2 = new Student();         st2.setRollNo(1102108L);         st2.setName("Anushi Kumra");         st2.setCourse("BCA");                Student...
// Serializable Student Class import java.io.Serializable; public class Student implements Serializable {     private long rollNo;     private String name;     private String course;        public void setRollNo(long rollNo) {         this.rollNo = rollNo;     }        public long getRollNo() {         return rollNo;     }        public void setName(String name) {         this.name = name;     }        public String getName() {         return name;     }        public void setCourse(String course) {         this.course = course;     }        public String getCourse() { ...
Program to read a file import java.util.Scanner; import java.io.File; public class FileRead {     public static void main(String[] args) throws Exception {         Scanner scan = new Scanner(new File(args[0]));                 while (scan.hasNext()) {             System.out.println(scan.nextLine());         }     } }
Program to Write to a file import java.io.File; import java.io.PrintStream; public class FileWrite {     public static void main(String[] args) throws Exception {         PrintStream ps = new PrintStream(new File(args[0]));                 ps.println("My new file created by java I/O");         ps.print("I'm on new line.");         ps.print("i'm still on same line.");         //ps.close();     } }  
Program to Toggle contents of any file import java.io.File; import java.util.Scanner; import java.io.PrintStream; public class Toggle {     public static void main(String[] args) throws Exception {         File fileOld = new File(args[0]);         File fileNew = new File("temp");         Scanner scan = new Scanner(fileOld);         PrintStream ps = new PrintStream(fileNew);                    while (scan.hasNext()) {             String content = scan.nextLine();                        for (int i = 0; i < content.length(); i++) {                 if (content.charAt(i) >= 'a' && conten...
/*  * StringDemo.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ public class StringDemo {        public static void ma...
/*  * CircleAnimator.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JB...
/*  * PictureViewer.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JBu...
/*  * MoveText.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionP...
/*  * EventsUsingInnerClass.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ import javax.swing.JFrame; import javax.swing.JButton; import java.a...
/*  * CircleAnimator.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JB...
/*  * EventsImplements.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ import javax.swing.JFrame; import javax.swing.JButton; import java.awt.ev...
Program to demonstrate Event handling on JButton using Anonymous object/listener /*  * Events.java  *  * Copyright 2013 Ajay Bhatia <ajay@dumb-box>  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *  *  */ ...
A Simple awt application with some components, layout and listener import java.awt.Frame; import java.awt.Label; import java.awt.Button; import java.awt.TextField; import java.awt.TextArea; import java.awt.FlowLayout; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class MyFirstWindow extends Frame implements WindowListener {     private Label lblMessage;     private Label lblMsg;     private Button button;     private TextField txt;     private TextArea ta;         MyFirstWindow() {         lblMessage = new Label("Welcome to GUI!!!");         lblMsg = new Label("Oh! I overlapped previous label");         button = new Button("Click Me");         txt = new TextField(30);         ta = new TextArea(10, 10...
A Simple Swing Application with some components and layout import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JTextArea; import java.awt.GridLayout; public class MyFirstSwing extends JFrame {     private JLabel lblMessage;     private JLabel lblMsg;     private JButton button;     private JTextField txt;     private JTextArea ta;        MyFirstSwing() {         lblMessage = new JLabel("Welcome to GUI!!!");         lblMsg = new JLabel("Oh! I overlapped previous label");         button = new JButton("Click Me");         txt = new JTextField(5);         ta = new JTextArea(2, 2);                setLayout(new ...
Program to demonstrate concept of abstract class in java abstract class AbstractShape {     protected double side1;     protected double side2;         AbstractShape() {     }     public void typeOfShape(String type) {         System.out.println("Shape is " + type);     }         public abstract void area(); } class Square extends AbstractShape {     public Square() {         side1 = side2 = 0.0;     }         public Square(double side) {         side1 = side2 = side;     }         public void area() {         System.out.println("Area of square is " + (side1 * side2));     } } class Rectangle extends AbstractShape {...
Program to Demonstrate use of Inheritance, Polymorphism, interface, method overriding etc class Food {     protected String name;         // Setter or mutator     public void setName(String name) {         this.name = name;     }         // Getter or accessor     public String getName() {         return name;     } } interface Eatable {     public abstract String howToEat(); } class Fruit extends Food { } class Vegetable extends Food { } class Mango extends Fruit implements Eatable {     public String howToEat() {         return "Peel it, slice it and make shake! Humm Yummy!";     } } class Apple extends Fruit implements Eatable {     public String howToEat() {         retur...
Program of Complex Number using OOP class ComplexNumber {     private double real;     private double img;     ComplexNumber() {         real = img = 0.0;        }     ComplexNumber(double real, double img) {         this.real = real;         this.img = img;        }     public ComplexNumber add(ComplexNumber c2) {         return new ComplexNumber(this.real + c2.real, this.img + c2.img);         }     public ComplexNumber multiply(ComplexNumber c2) {         return new ComplexNumber(this.real * c2.real - this.img * c2.img, this.real * c2.img + this.img * c2.real);     }     public String toString() {      ...
 A simple example of inheritance along explanation of constructor calling class A {     A() {         System.out.println("Hey! I'm father of all.");     } } class B extends A {     B() {         System.out.println("Hey! I'm child of A.");        } } class C extends B {     C() {         System.out.println("Hey! I'm child of B and younger one.");        } } public class Inheri {     public static void main(String[] args) {         C youngerone = new C();     } }
Put this code in Number.java public class Number {     private long value;         public Number() {         value = 0;     }         public Number(long value) {         this.value = value;     }         public String evenOrOdd() {         return (value % 2 == 0) ? "even" : "odd";     }         public String prime() {         for (int i = 2; i <= Math.sqrt(value); i++) {             if (value % i == 0)                     return "not prime";         }         return "prime";     }   ...