uNreQuiteD

January 16, 2007

if: odd or even

Filed under: basic java — anai lem @ 11:15 am

import javax.swing.*;
class evenOdd
{
  public static void main(String args[])
  {
  int num = Integer.parseInt(JOptionPane.showInputDialog(“Enter a number”));
  int abs=num;

  if(num<0)
     abs *= -1;

  int sqr = num * num;

  if(num % 2 == 0)
    JOptionPane.showMessageDialog(null,num +” is even\nSquared value = ” + sqr);
  else
    JOptionPane.showMessageDialog(null,num +” is odd\nAbsolute value = ” + abs);
  }
}

output

 p.jpg

link list

Filed under: data structure — anai lem @ 11:00 am

import javax.swing.*;
class Node
{
  int no;
  Node ptr;

  //constructor of class Node
  Node(int n, Node p)
  {
    no = n;
    ptr = p;
  }
//display list
static void printList(Node list){
  Node node =list;
  String out=””;
  while (node !=null)
   {
    out += node + ” –>  [” + node.no + “] ” + node.ptr + “\n” ;
    node =node.ptr;
   }
   JOptionPane.showMessageDialog(null,”link list \n”+out);
 }
//main method
public static void main(String args[])
{
int noNodes = Integer.parseInt(JOptionPane.showInputDialog(“number of nodes: “));
if (noNodes>=2 && noNodes<=5)
{
  int ctr = 1;
  Node head, dummy, node;
  int value = Integer.parseInt(JOptionPane.showInputDialog(“number ” + ctr));

//1st node created
    node = new Node(value, null);
    head = node;

  ctr++;
//2nd upto last node created
  while(ctr<=noNodes)
  {
    value = Integer.parseInt(JOptionPane.showInputDialog(“number ” + ctr));
    dummy = new Node(value,null);
    node.ptr = dummy;
    node = dummy;
    ctr++;
  }

//call static method printList
  printList(head);
 }
}}

 output

put.jpg

if structure – odd / even

Filed under: basic java — anai lem @ 10:20 am

import javax.swing.JOptionPane;
class ifStructure
{
public static void main(String arg[])
{
String str = JOptionPane.showInputDialog(“Enter a number”);
int no = Integer.parseInt(str);
if(no%2 == 0)
JOptionPane.showMessageDialog(null, no + ” is even \nSquared value: ” + no*no);
else
{
int num = no;
if(no

setter & getter methods

Filed under: object oriented progg — anai lem @ 1:03 am

import javax.swing.JOptionPane;

class Person
{
  String name;
  int age;

  public static void main(String args[])
  {

  Person p = new Person();
  p.setName(JOptionPane.showInputDialog(“Enter name”));
  p.setAge(Integer.parseInt(JOptionPane.showInputDialog(“Enter age”)));
  JOptionPane.showMessageDialog(null, “Name: ” + p.getName() + “\nAge: ” + p.getAge());
  }

    public int getAge() {
        return age;
    }

    public void setAge(int value) {
        age = value;
    }

    public java.lang.String getName() {
        return name;
    }

    public void setName(java.lang.String value) {
        name = value;
    }
}

out.jpg

Create a free website or blog at WordPress.com.