uNreQuiteD

February 22, 2007

overriding methods

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

import javax.swing.*;
class overrideLoop {
  static  String out=”Sequence\n\n”;

  static void display(int end)
  {
    for(int start=1; start <= end; start++)
     {
      for(int in=1; in<=start; in++)
         out += “*”;
      out+=”\n”;
     }
    JOptionPane.showMessageDialog(null,out);
  }

  static void display(String s)
  {
    for(int ctr=1; ctr<=10; ctr++)
      out += s + ” “;
    JOptionPane.showMessageDialog(null,out,”Alternative Method”,1);
  }

 public static void main(String agrs[])  {
  String s=””;
    try   {
      s = JOptionPane.showInputDialog(null, “Enter a number”, “Overriding Methods”,3);
      int y = Integer.parseInt(s);
      display(y);
     }
    catch(Exception e)   {
      JOptionPane.showMessageDialog(null, e + “\n\nCalling alternative method display”);
      display(s);
     }
  }
}

output:
om.jpg

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

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

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

November 24, 2006

Filed under: object oriented progg — anai lem @ 2:05 am

//SUPERCLASS
class methodHere
{
 int factor (int x)
 {
  int f=1;                                
  if(x>0)
    f = x * factor(x-1);
  return f;
 }
}

//SUBCLASS FACTORIAL EXTENDING SUPERCLASS METHODHERE
class factorial extends methodHere
{
 public static void main(String args[])
 {
   factorial fl = new factorial();
   int y = fl.factor(5);
   System.out.println(“Factorial of 5: ” + y);
 }
}

output
                    Factorial of 5: 120

employee inheritance

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

 //superclass person

class person
{
  void info(String name, int age)
  {
    System.out.println(“Name :” + name);
    System.out.println(“Age  :” + age);
  }

  int gross(int rate, int hrs)
  {
    int gr = rate * hrs;
    System.out.println(“Gross : ” + gr);
    return gr;
  }
}

 //subclass employee

class employee extends person
{
  void net(int gross)
  {
     double netIncome = gross – (gross * .10);
     System.out.println(“Net income : ” + netIncome);
  }

  public static void main(String args[])
  {
     employee e = new employee();
     e.info(“IANA MEL”, 20);
     int g = e.gross(200,8);
     e.net(g);
  }
}

output:

 Name :IANA MEL
Age :20
Gross : 1600
Net income : 1440.0

November 23, 2006

fibonacci – method

Filed under: object oriented progg — anai lem @ 8:02 am

class fibonacci
{
  void fibo()
  {
   int x=1, y=1, temp=x+y;;
   System.out.print(x + ” ” + y + ” ” + temp +” “);
   while(temp<13)
   {
     y=x;
     x=temp;
     temp = x + y;
     System.out.print(temp + ” “);
   }
  }

  public static void main(String args[])
  {
     fibonacci f = new fibonacci();
     f.fibo();
  }
}

//e3371@yahoo.com 

Blog at WordPress.com.