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");
}


}
}

Thursday, 28 April 2011

JAVA PROGRAM TO IMPLEMENT COLOR SECTIONS


www.javalearing.blogspot.com importjava.awt.*;
import java.awt.event.*;
import java.applet.*;
 
public class fillcolor extends Applet implement Item Listener
{
 checkbox red,yellow,black,blue,orange;
 CheckboxGroup cbg;
 String msg;
 String s1="red";
 String s2="yellow";
 String s3="black";
 String s4="orange";
 public void init()
 {
  cbg = new CheckboxGroup();
  red = new Chechbox("red",cbg,true);
  yellow= new Checkbox("yellow",cbg,false);
  black = new Checkbox("black",cbg,false);
  blue = new Checkbox("blue",cbg,false);
  orange = new checkbox("orange".cbg.false);
  add(red);
  add(yellow);
  add(black);
  add(blue);
  add(orange);
  red.addItemListener(this);
  yellow.addItemListener(this);
  black.addItemListener(this);
  blue.addItemListener(this);
  orange.addItemListener(this);
 }
 publice void itemStartechanged(ItemEvent ie)
 {
  repaint();
 }
 publice void paint (Graphics g)
 {
  msg = cbg.getSelectedCheckbox().getLabel();
  if(msg.compareTo(s1)==0)
  { 
      setBackground(Color.red);
  }
  else if (msg.compareTo(s2)==0)
  {
      setBackground(Color.yellow);
  }
  else if(msg.compareTo(s3)==0)
  {
      setBackground(color.black);
  }
  else if (msg.compareTo(s4)==o)
  {
      setBackground(Color.blue);
  }
  else
  {
      set Background(Color.orange);
  }
 }
}
Java 2: The Complete Reference, Fifth Edition

JAVA PROGRAM TO DISPLAY IP ADDRESSSOF A PARTICULAR HOST


WWW.JAVALEARING.BLOGSPOT.COM
Head First Design Patterns
import java.net.*;
import java.io.*;
 
public class ip_host
{
 public static void main(String args[]) throws Exception
 {
  System.out.println("Enter the host name :");
  String n=new DataInputStream(System.in).readLine();
 
  InetAddress ipadd =InetAddress.getByName(n);
 
  System.out.println("IP address :"+ipadd);
 }
}
Head First Java, 2nd Edition

JAVA PROGRAM TO DISPLAY DOMAIN NAME SERVICE


import java.net.*;
 
class dns
{
 public static void main(String args[]) throws Exception
 {
  try
  {
   InetAddress[] address=InetAddress.getAllByName("java.sun.com");
   for(int j=0;j<address.length;j++)
   System.out.println(address[j]);
  }
 
  catch(Exception e)
  {
   System.out.println("Error in accessing Internet :"+e);
  }
 }
}

MINIMUM OF TWO NUMBER USING CONDITIONAL OPERATOR


class Minof2{
 public static void main(String args[]){
  //taking value as command line argument.
  //Converting String format to Integer value
  int i = Integer.parseInt(args[0]);
  int j = Integer.parseInt(args[1]);
  int result = (i<j)?i:j;
  System.out.println(result+" is a minimum value");
 }
}

PROGRAM TO PRINT SMALLEST AND LARGEST INTEGER


/* Write a program that will read a float type value from the keyboard and print
   the following output.
   ->Small Integer not less than the number.
   ->Given Number.
   ->Largest Integer not greater than the number.
 */
class ValueFormat{
 public static void main(String args[]){
  double i = 34.32; //given number
  System.out.println("Small Integer not greater than the number : "+Math.ceil(i));
  System.out.println("Given Number : "+i);
  System.out.println("Largest Integer not greater than the number : "+Math.floor(i));
 }
}

Write a program to generate 5 Random nos. between 1 to 100


/*Write a program to generate 5 Random nos. between 1 to 100, anD it
  should not follow with decimal point.
 */
class RandomDemo{
 public static void main(String args[]){
  for(int i=1;i<=5;i++){
   System.out.println((int)(Math.random()*100));
  }
 }
}