uNreQuiteD

February 22, 2007

nested loop & methods: diamond

Filed under: basic java — anai lem @ 3:33 am

import javax.swing.*;
class nestedLoopDiamond {

//RIGHT TOP
static void rightTopDiamond()
{
String out=””;
for(int start=1; start <= 5; start++)
{
for(int in=1; in<=start; in++)
out += start;
out+=”\n”;
}
JOptionPane.showMessageDialog(null,out,”1/4 diamond”,-1);
}

//LEFT TOP
static void leftTopDiamond()
{
String out=””;
for(int start=1; start <= 5; start++)
{
out+=”\n”;
for(int spc=1; spc<=5-start; spc++)
out +=” “;
for(int in=1; in<=start; in++)
out +=start;
}
JOptionPane.showMessageDialog(null,out,”1/4 diamond”,-1);
}

//RIGHT BOTTOM
static void rightBottomDiamond()
{
String out=””;
for(int start=5; start >= 1; start–)
{
for(int in=1; in<=start; in++)
out += start;
out+=”\n”;
}
JOptionPane.showMessageDialog(null,out,”1/4 diamond”,-1);
}

//LEFT BOTTOM
static void leftBottomDiamond()
{
String out=””;
for(int start=5; start >= 1; start–)
{
out+=”\n”;
for(int spc=1; spc<=5-start; spc++)
out +=” “;
for(int in=1; in<=start; in++)
out += start;
}
JOptionPane.showMessageDialog(null,out,”1/4 diamond”,-1);
}

//DIAMOND
static void Diamond()
{
String out=””;
for(int start=1; start <= 5; start++)
{
for(int spc=start; spc<5; spc++)
out +=” “;
for(int in=1; in<=start; in++)
out += ” ” + start +” “;
out+=”\n”;
}

for(int start=4; start >= 1; start–)
{
for(int spc=4; spc>=start; spc–)
out +=” “;
for(int in=1; in<=start; in++)
out += ” ” + start +” “;
out+=”\n”;
}
JOptionPane.showMessageDialog(null,out,”diamond”,-1);
}

public static void main(String agrs[]) {
int ans;
do
{
String menu=JOptionPane.showInputDialog(null, “Main Menu \n” +
“[1] 1/4 right top diamond\n” +
“[2] 1/4 left top diamond\n” +
“[3] 1/4 right botom diamond\n” +
“[4] 1/4 left bottom diamond\n” +
“[5] diamond\n” +
“[6] exit”);
ans= Integer.parseInt(menu);
switch(ans)
{
case 1: rightTopDiamond(); break;
case 2: leftTopDiamond(); break;
case 3: rightBottomDiamond(); break;
case 4: leftBottomDiamond(); break;
case 5: Diamond(); break;
}
}while(ans!=6);
JOptionPane.showMessageDialog(null, “Programmed by: Iana Mel”);
}
}

sample output
diam.jpg

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

February 20, 2007

stack: using singly link list

Filed under: data structure — anai lem @ 8:15 am

import javax.swing.*;
class nodeStack
{
Object info;
nodeStack link;
static nodeStack head, next, tail, last;

public nodeStack(Object i, nodeStack l) //CONSTRUCTOR
{ info = i;
link = l; }

//add node
static void doAdd()
{
int i = Integer.parseInt(JOptionPane.showInputDialog(“Add Node\nEnter a number”));
if(head == null)
{
head = new nodeStack(i,null);
tail = head;
}
else
{
next = new nodeStack(i,null);
tail.link = next;
tail=next;
}
}

//display
static void doDisplay()
{
String output=””;
last = tail;
if (head != null)
{
if (head == last)
output += head.info + ” “;
else
{
while(head != last)
{
next = head;
while(next.link != last)
{
next = next.link;
}
output += last.info + ” “;
last = next;
}
output += head.info + ” “;
}
JOptionPane.showMessageDialog(null, “Array List Content\n” + output);
}

else
JOptionPane.showMessageDialog(null, “List is empty”);
}

//delete
static void doDelete()
{
if (head != null)
{
if (head == tail)
head = null;
else
{
next = head;
while(next.link != tail)
{
next = next.link;
}
next.link = null;
tail = null;
tail = next;
}
JOptionPane.showMessageDialog(null, “Tail node deleted”);
}

else
JOptionPane.showMessageDialog(null, “List is empty”);
}

public static void main(String[] args)
{
String menu = “MAIN MENU\n” +
“[1] Add node\n”+
“[2] Display list\n” +
“[3] Delete node\n” +
“[4] Exit”;
int choice=0;
do
{
String str = JOptionPane.showInputDialog(menu);
if(str.equals(“”))
JOptionPane.showMessageDialog(null, “enter a number”);
else
{
choice = Integer.parseInt(str);
if (choice == 1)
doAdd();
else if (choice == 2)
doDisplay();
else if (choice == 3)
doDelete();
else if (choice == 4)
JOptionPane.showMessageDialog(null, “Programmer: eugene”);
}
} while(choice != 4);
} }

sample output:
stackto.jpg

method: passing parameters

Filed under: basic java — anai lem @ 7:55 am

import javax.swing.*;
class startEnd
{
public static void main(String args[])
{
int x = Integer.parseInt(JOptionPane.showInputDialog(“Enter starting number”));
int y = Integer.parseInt(JOptionPane.showInputDialog(“Enter ending number”));
disp(x,y);
}

static void disp(int a, int b)
{
String out=””;
for( ; a<=b; a++)
out = out + a + ” “;
JOptionPane.showMessageDialog(null,out);
}
}

output
labas.jpg

stack: using 1-di array

Filed under: data structure — anai lem @ 7:21 am

class stackArr
{
public static void main(String args[])
{
String str = “3-5*2-6*2+7/2”;

char stck[] = {‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘};
int top = 0, x;
char c;

System.out.print(“STATEMENT : ” + str + “\nPOSTFIX : “);
for(x=0; x<str.length(); x++)
{
c = str.charAt(x);
switch(c)
{

// operators: multiplication division
case ‘/’:
case ‘*’:
{
if(top == 0)
{
stck[top]= c;
top++;
}
else
if(stck[top-1] == ‘/’ || stck[top-1] == ‘*’)
{
top–;
System.out.print(stck[top]);
stck[top]=c;
top++;
}
else
{
stck[top] = c;
top++;
}

break;
} //end case

// operators: addition subtraction
case ‘-‘:
case ‘+’:
{
if(top == 0)
{
stck[top]= c;
top++;
}
else
{
for(int ctr=top-1; ctr>=0; ctr–)
{
if(stck[ctr] == ‘-‘ || stck[ctr] == ‘+’ ||
stck[ctr] == ‘/’ || stck[ctr] == ‘*’)
{
top–;
System.out.print(stck[top]);
stck[top]=’ ‘;
}
} // end loop

stck[top] = c;
top++;
} // end else
break;
} // end case

//operand
default: System.out.print(c);

} //end switch
} //end for

for(int y=top-1; y>=0; y–)
System.out.print(stck[y]);
System.out.println();
}
}
//PROGRAMMED BY: EUGENE

output:
STATEMENT : 3-5*2-6*2+7/2
POSTFIX : 352*-62*-72/+

February 13, 2007

menu: QUEUE manipulation

Filed under: data structure — anai lem @ 3:28 am

import javax.swing.*;
class nodeMenu
{
  Object info;
  nodeMenu link;
  static nodeMenu head, next, tail;

  public nodeMenu(Object i, nodeMenu l) //CONSTRUCTOR
  { info = i;
    link = l;  }

  static void doAdd()
  {
    int i = Integer.parseInt(JOptionPane.showInputDialog(“Add Node\nEnter a number”));
    if(head == null)
      {
       head = new nodeMenu(i,null);
       tail = head;
      }
    else
      {
       next = new nodeMenu(i,null);
       tail.link = next;
       tail=next;
      }
  }

  static void doDisplay()
  {
    String output=””;
   if (head != null)
   {
    next = head;
    while(next!=null)
    {
      output = output + (next  + ” -> ” + next.info + ” ” + next.link + “\n”);
      next = next.link;
    }
    JOptionPane.showMessageDialog(null, “Display List\n” + output);
   }
   else
      JOptionPane.showMessageDialog(null, “List is empty”);
  }

  static void doDelete()
  {
  if(head!=null)
   {
    next = head;
    head = next.link;
    next = null;
    JOptionPane.showMessageDialog(null,”Delete Node\nHEAD node deleted”);
   }
  else
    JOptionPane.showMessageDialog(null,”List is empty”);
  }

  public static void main(String[] args)
  {
    String menu = “MAIN MENU\n” +
                   “[1] Add node\n”+
                   “[2] Display list\n” +
                   “[3] Delete node\n” +
                   “[4] Exit”;
   int choice=0;
 do
 {
  String str = JOptionPane.showInputDialog(menu);
  if(str.equals(“”))
        JOptionPane.showMessageDialog(null, “enter a number”);
  else
  {
   choice = Integer.parseInt(str);
   if (choice == 1)
        doAdd();
   else if (choice == 2)
        doDisplay();
   else if (choice == 3)
        doDelete();
   else if (choice == 4)
      JOptionPane.showMessageDialog(null, “Programmer: eugene”);
  }
 } while(choice != 4);
} }

sample output

queue

Blog at WordPress.com.