Wednesday, February 15, 2017

Friend of Mine


Labels:

Tuesday, February 14, 2017

File Frequency

//program that displays number of characters,lines,words in a text file.
import java.lang.*;
import java.util.*;
import java.io.*;
class FileCount
{
 public static int words=0;
public static int lines=0;
public static int chars=0;
public static void wc(InputStreamReader isr)throws IOException
{
int c=0;
while((c=isr.read())!=-1)
{
chars++;
if(c=='\n')
lines++;
if(c=='\t' || c==' ' || c=='\n')
++words;

}
       }
                public static void main(String args[])
                {
                                String fname=" ";
                                System.out.println("enter a file name:");
                                try
                                {
                                       DataInputStream dis=new DataInputStream(System.in);
                                       fname=dis.readLine();
                                       FileReader fr=new FileReader(fname);
                                       wc(fr);
                                       System.out.println("the no.of lines in file:"+lines);
                                       System.out.println("the no.of words in file:"+words);
                                       System.out.println("the no.of characters in file:"+chars);
                                }
                                catch(Exception e)
                                {
                                                System.out.println("given file"+fname+"does not exist");
                                }
                }
}

Labels:

Simple Calculator

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="Cal" width=300 height=300>
</applet>
*/

public class Cal extends Applet
implements ActionListener
{
String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
}

Labels:

Menu and Dialog

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MD" width=250 height=250>
</applet>
*/

// Create a subclass of Frame

class MenuFrame extends Frame implements ActionListener
{

String msg = "";

CheckboxMenuItem debug, test;

MenuFrame(String title)
{

super(title);
// create menu bar and add it to frame

MenuBar mbar = new MenuBar();

setMenuBar(mbar);
// create the menu items

Menu file = new Menu("File");

MenuItem item1, item2, item3, item4, item5;

file.add(item1 = new MenuItem("New..."));

file.add(item2 = new MenuItem("Open..."));

file.add(item3 = new MenuItem("Close"));

file.add(item4 = new MenuItem("-"));

file.add(item5 = new MenuItem("Quit..."));

mbar.add(file);

Menu edit = new Menu("Edit");

MenuItem item6, item7, item8, item9;

edit.add(item6 = new MenuItem("Cut"));

edit.add(item7 = new MenuItem("Copy"));

edit.add(item8 = new MenuItem("Paste"));

edit.add(item9 = new MenuItem("-"));

Menu sub = new Menu("Special");

MenuItem item10, item11, item12;

sub.add(item10 = new MenuItem("First"));

sub.add(item11 = new MenuItem("Second"));

sub.add(item12 = new MenuItem("Third"));

edit.add(sub);

// these are checkable menu items

debug = new CheckboxMenuItem("Debug");

edit.add(debug);

test = new CheckboxMenuItem("Testing");

edit.add(test);

mbar.add(edit);
item1.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
SampleDialog d=new SampleDialog(this,"NEW DIALOG BOX");
d.setVisible(true);
}
}
class SampleDialog extends Dialog implements ActionListener
{
SampleDialog(Frame parent, String title)
{
super(parent, title, false);
setLayout(new FlowLayout());
setSize(300, 200);
add(new Label("Press this button:"));
Button b;
add(b = new Button("Cancel"));
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
public void paint(Graphics g) {
g.drawString("This is in the dialog box", 10, 70);
}
}


public class MD extends Applet
{
Frame f;

public void init()
{
f = new MenuFrame("Menu Demo");

int width = Integer.parseInt(getParameter("width"));

int height = Integer.parseInt(getParameter("height"));

f.setSize(width, height);

f.setVisible(true);
}

public void start()
{
f.setVisible(true);
}

public void stop()
{
f.setVisible(false);
}

}

Labels:

Thursday, February 9, 2017

I, ME, Myself


Labels:

Tuesday, February 7, 2017

GRID LAYOUT

import java.awt.*;
import java.applet.*;
/*
<applet code="GridLayoutDemo" width=300 height=200>
</applet>
*/
public class GridLayoutDemo extends Applet
{
static final int n = 4;

public void init() {

setLayout(new GridLayout(n, n));

setFont(new Font("SansSerif", Font.BOLD, 24));

for(int i = 0; i < n; i++)
{

for(int j = 0; j < n; j++)
{

int k = i * n + j;

if(k > 0)

add(new Button("" + k));

}
}
}
}

Labels:

BORDER LAYOUT

// Demonstrate BorderLayout.
import java.awt.*;
import java.applet.*;
import java.util.*;
/*
<applet code="BorderLayoutDemo" width=400 height=200>
</applet>
*/
public class BorderLayoutDemo extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add(new Button("This is across the top."),BorderLayout.NORTH);

add(new Label("The footer message might go here."),BorderLayout.SOUTH);

add(new Button("Right"), BorderLayout.EAST);

add(new Button("Left"), BorderLayout.WEST);

String msg = "The reasonable man adapts " +
"himself to the world;\n" +
"the unreasonable one persists in " +
"trying to adapt the world to himself.\n" +
"Therefore all progress depends " +
"on the unreasonable man.\n\n" +
" - George Bernard Shaw\n\n";
add(new TextArea(msg), BorderLayout.CENTER);

}
}

Labels: