uNreQuiteD

January 25, 2007

EXCEPTION

Filed under: object oriented progg — anai lem @ 12:48 am

// EXCEPTION
import javax.swing.*;
class exceptProg {
public static void main(String args[])  {
try
{
int no = Integer.parseInt(JOptionPane.showInputDialog(“enter a number:”));
int factorial=1;
for(int y = 1; y <= no; y++)
factorial *= y;
JOptionPane.showMessageDialog(null,”FACTORIAL : ” + factorial);
}
catch(Exception e)    {
JOptionPane.showMessageDialog(null,”ERROR\n” + e);
}
finally    {
JOptionPane.showMessageDialog(null,”END EXECUTION”);
}
} }

SAMPLE OUTPUT

out.jpg

January 23, 2007

FOR loop: POWER

Filed under: basic java — anai lem @ 1:39 am

/*
Create a program that will compute the power of a number given the base
and exponent. Do this program using a for loop.
programmed by: eugene
spec fr: JEDI – PROGRAMMING LANGUAGE 1
*/

import javax.swing.*;
class powers
{
public static void main(String args[])
{
int base = Integer.parseInt(JOptionPane.showInputDialog(“ENTER BASE:”));
int power = Integer.parseInt(JOptionPane.showInputDialog(“ENTER EXPONENT:”));
int result=1;
for(int x=1; x

Polymorphism

Filed under: object oriented progg — anai lem @ 12:31 am

/*SAMPLE POLYMORPHISM CLASS
compiled and edited by eugene.
reference:JEDI – Introduction to Programming 1 / pp 184-186
*/

class person
{
String name;
public String getName()
{
System.out.println(“person name:” + name);
return name;
}
}

class student extends person
{
public String getName()
{
System.out.println(“student name:” + name);
return name;
}
}

class Employee extends person
{
public String getName()
{
System.out.println(“employee name:” + name);
return name;
}
public static void main(String args[])
{
person ref;
student st = new student();
Employee em = new Employee();

ref = st;
ref.name = “iana”;
String temp = ref.getName();
System.out.println(temp+”\n”);

ref = em;
ref.name = “mel”;
temp = ref.getName();
System.out.println(temp+”\n”);

ref = new person();
ref.name = “iana mel”;
temp = ref.getName();
System.out.println(temp);
}
}

output

student name:iana
iana
employee name:mel
mel

person name:iana mel
iana mel

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

January 9, 2007

selection structure – if

Filed under: basic java — anai lem @ 9:58 am

import javax.swing.JOptionPane;
class selectStruct
{
public static void main(String args[])
{
int choice;
String str;
str = JOptionPane.showInputDialog(“Main Menu \n” +
                                  “[1] Name \n” +
                                  “[2] School \n” +
                                  “[3] Course \n” +
                                  “[4] Exit”);
choice =Integer.parseInt(str);
if(choice ==1)
  JOptionPane.showMessageDialog(null, “iana mel f. galang”);
else if(choice ==2)
  JOptionPane.showMessageDialog(null, “DWCL”);
else if(choice ==3)
  JOptionPane.showMessageDialog(null, “Business Administration”);
else if(choice ==4)
  JOptionPane.showMessageDialog(null, “by: Eugene”);
}
}

output

in.jpg

Create a free website or blog at WordPress.com.