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()); } } }
Posts
Showing posts from September, 2013
- Get link
- X
- Other Apps
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(); } }
- Get link
- X
- Other Apps
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...
- Get link
- X
- Other Apps
/* * 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...
- Get link
- X
- Other Apps
/* * 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...
- Get link
- X
- Other Apps
/* * 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...
- Get link
- X
- Other Apps
/* * 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...
- Get link
- X
- Other Apps
/* * 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...
- Get link
- X
- Other Apps
/* * 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...
- Get link
- X
- Other Apps
/* * 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...
- Get link
- X
- Other Apps
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. * * */ ...
- Get link
- X
- Other Apps
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...
- Get link
- X
- Other Apps
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 ...
- Get link
- X
- Other Apps
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 {...
- Get link
- X
- Other Apps
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...
- Get link
- X
- Other Apps
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() { ...
- Get link
- X
- Other Apps
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(); } }
- Get link
- X
- Other Apps
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"; } ...