Monday, 22 August 2011

prime in java


to calculate whether the number is prime or not.prime in java
javalearing.blogspot.com
class prime
{
public static void main(String args[])
{


int num=11;
int i;
for( i=2;i<num;i++)
{
int n=num%i;
if(n==0)
{
System.out.println("not prime");
break;
}
}
if(i==num){
System.out.println("prime number");
}
 }   }

package implementation in java

javalearing.blogspot.com
Here we will show u a simple program of packages and its implementation.
first create a package naming pack(any name)then create class and its method should be static and public  as shown  below:


package pack;
public class hemant
{
public static void show(String s)
{
    System.out.println(s);
    }
    }
save above as hemant.java
now compile your file as: javac hemant.java
now make another file in which you wanted to import your package for example demo.java

import pack.hemant; //for importing package pack having class name hemant
class demo
{
public static void main(String args[])
{
hemant.show("hi bye");
}
}
save above as demo.java
compile your package class first:        javac hemant.java
compile your another file create by you: javac demo.java
run your demo file:java demo
output:
hi bye
finally you get the clear concept of packages.

A JAVA PROGRAM SHOWING OBJECTS AND ITS CLASS IMPLEMENTATION


HERE WE HAVE BOX AS A CLASS WITH WIDTH,HEIGHT AND DEPTH AS ATTRIBUTES AND VOLUME AS METHOD.PROGRAM IS SELF EXPLAINATORY.
class box
{
double width;
double height;
double depth;

public void volume()
{
System.out.println("volume is ");
System.out.println(width*height*depth);
}
}
class boxdemo
{
public static void main(String args[]){
box mybox1=new box();
box mybox2=new box();
mybox1.width=100;
mybox1.height=20;
mybox1.depth=10;
mybox2.width=3;
mybox2.height=6;
mybox2.depth=7;
mybox1.volume();
mybox2.volume();
}
}

NOTEPAD OPENING USING JAVA


QUESTION:NOTEPAD OPENING USING JAVA:
Here we use ProcessBuilder class and create its object following is a simple program opening notepad using java.
import java.io.*;
import java.util.*;
class notepad
{
public static void main(String args[])
{

try
{
ProcessBuilder proc=new ProcessBuilder("notepad.exe","report");
proc.start();
}
catch (Exception e)
{
System.out.println("notepad not open");
}


}
}