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

A PROGRAM TO DISPLAY MESSAGE ACCORDING TO MARKS OBTAINED.


/* Write a program to display a greet message according to
   Marks obtained by student.
 */
class SwitchDemo{
 public static void main(String args[]){
  int marks = Integer.parseInt(args[0]); //take marks as command line
  argument.
   switch(marks/10){
    case 10:
    case 9:
    case 8:
     System.out.println("Excellent");
     break;
    case 7:
     System.out.println("Very Good");
     break;
 
 
    case 6:
     System.out.println("Good");
     break;
    case 5:
     System.out.println("Work Hard");
     break;
    case 4:
     System.out.println("Poor");
     break;
    case 3:
    case 2:
    case 1:
    case 0:
     System.out.println("Very Poor");
     break;
    default:
     System.out.println("Invalid value Entered");
   }
 }
}