uNreQuiteD

December 7, 2006

2-di array: zeroDown

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

/*
2-di array.
test if the values below the diagonal of the 2-di array (3×3) are all equal to zero.

 programmed by: iana mel
*/

import javax.swing.JOptionPane;
class zeroDown
{
  public static void main(String args[])
  {
    int ar[][] = {{4,6,3},
                  {0,5,7},
                  {0,0,6}};
    int a,b, valid = 1;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
      {
       if(a==b)
          break;
       if(ar[a][b] != 0)
         valid = 0;
      }
      if (valid == 0)
        break;
    }

    String s=””;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
          s += ar[a][b] +”  “;
      s+=”\n”;
    }

    if(valid == 1)
       JOptionPane.showMessageDialog(null,s + “\n\n VALID”);
    else
       JOptionPane.showMessageDialog(null,s + “\n\n INVALID”);
  }
}

OUTPUT

out12.jpg

2-di array: zeroUp

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

/*
2-di array.
test if the values above the diagonal of the 2-di array (3×3) are all equal to zero.

programmed by: iana mel
*/

import javax.swing.JOptionPane;
class zeroUp
{
  public static void main(String args[])
  {
    int ar[][] = {{4,0,0},
                  {5,1,0},
                  {3,4,6}};
    int a,b, valid = 1;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
      {
       if(a==b || a>=b)
          continue;
       if(ar[a][b] != 0)
         valid = 0;
      }
      if (valid == 0)
        break;
    }

    String s=””;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
          s += ar[a][b] +”  “;
      s+=”\n”;
    }

    if(valid == 1)
       JOptionPane.showMessageDialog(null,s + “\n\n VALID”);
    else
       JOptionPane.showMessageDialog(null,s + “\n\n INVALID”);
  }
}

 OUTPUT

out2.jpg

2-di array: zeroDown

Filed under: Uncategorized — anai lem @ 7:41 am

/*
2-di array.
test if the values below the diagonal of the 2-di array (3×3) are all equal to zero.
*/

import javax.swing.JOptionPane;
class zeroDown
{
  public static void main(String args[])
  {
    int ar[][] = {{4,6,3},
                  {0,5,7},
                  {0,0,6}};
    int a,b, valid = 1;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
      {
       if(a==b)
          break;
       if(ar[a][b] != 0)
         valid = 0;
      }
      if (valid == 0)
        break;
    }

    String s=””;
    for(a=0; a<=2; a++)
    {
      for(b=0; b<=2; b++)
          s += ar[a][b] +”  “;
      s+=”\n”;
    }

    if(valid == 1)
       JOptionPane.showMessageDialog(null,s + “\n\n VALID”);
    else
       JOptionPane.showMessageDialog(null,s + “\n\n INVALID”);
  }
}

OUTPUT

out11.jpg

HARRY POTTER & THE AMAZING WIZARD

Filed under: data structure — anai lem @ 6:29 am

/*
This lab exercise was downloaded from a page which i forgot. my apology.  The downloaded file was edited by the programmer.

HARRY POTTER & THE AMAZING WIZARD.
Declare and initialize a 20-element array. In each element place one of the following characters:
‘W’ – Wizard
‘S’ — Sword
‘D’ — Demon
‘B’ — Blank – Save spot.

Example an array can look like this:
W S S D B D W B B D W S W D B D S W B S

You ask the player to select a location in Hogarth Tower. (Choose a number from 0 – 19) That places the player at a position in the array. In the example if the user enters a 4 as a starting position (starting at 0). So, the entry at that position is a B (Blank).

Based on the rules below the player can then move North (add 1 to the array index) or South (subtract 1 from the array index). Here are the rules:

(1) For each Blank (B) spot, output the message ‘Safe Place’; 2 points is awarded.
(2) For each Wizard (W) spot, output the message ‘You picked up a Wizard’; 3 Points is Awarded.
(3) For each Sword (S) spot, output the message ‘You picked up a Sword’; 4 Points is awarded.
(4) For each Demon (D) encountered, display the message ‘A demon has attacked’; subtract 5 points.

The game is over when (a) the player types in 99 this ends the game. Print out the accumulated points. Or (b) when the points are below 0 – the player is killed. Display ‘You are killed’ if the point garnered by the player is below zero.

Make sure the player does not go beyond the array boundary. The boundary has is limited by the size of the array 19 elements.
*/
/*programmed by: iana mel – school of information technology, dwcl*/
import javax.swing.JOptionPane;
class harryPot
{
   static int score(char c)
    {
        switch(c)
        {
        case 66: // ‘B’
            JOptionPane.showMessageDialog(null,”SAFE PLACE. +2 pts”);
            return 2;

        case 87: // ‘W’
            JOptionPane.showMessageDialog(null,”YOU PICKED UP A WIZARD. +3 pts”);
            return 3;

        case 83: // ‘S’
            JOptionPane.showMessageDialog(null,”YOU PICKED UP A SWORD. +4 pts”);
            return 4;

        case 68: // ‘D’
            JOptionPane.showMessageDialog(null,”A DEMON HAS ATTACKED. -5 pts”);
            return 5;
        }
        return 0;
    }

    public static void main(String args[])
    {

        char ac[] = { ‘W’, ‘S’, ‘S’, ‘D’, ‘B’, ‘D’, ‘W’, ‘B’, ‘B’, ‘D’,
                               ‘W’, ‘S’, ‘W’, ‘D’, ‘B’, ‘D’, ‘S’, ‘W’, ‘B’, ‘S’};
        int i = 0, j=0, k, l;

        String s = JOptionPane.showInputDialog(“Choose a number from 0 – 19″);
        k = Integer.parseInt(s);

        if(k < 0 || k > 19)
           JOptionPane.showMessageDialog(null,”INVALID POSITION. \nPROGRAM RUN TERMINATED.”);
        else
         do
         {
           if(k<0)
           {
               JOptionPane.showMessageDialog(null,”You’re already at index 0. \nYou can’t move South anymore.”);
               k=0;
           }
           else if(k>19)
           {
               JOptionPane.showMessageDialog(null,”You’re already at index 19. \nYou can’t move North anymore.”);
               k=19;
           }
           else
           {
            l = score(ac[k]);
            if(l < 5)
                i += l;
            else
                i -= l;
            if(i < 1)
            {
                JOptionPane.showMessageDialog(null,”YOU ARE KILLED!”);
                break;
            }
           }
                s =JOptionPane.showInputDialog(“Score: ” + i + “\n\n[1] north \n[2] south \n[99]exit \n\n   Choose :  “);
                j = Integer.parseInt(s);
                if(j == 1)
                    k++;
                else if(j == 2)
                    k–;
        } while(j != 99);

        if(i > 0)
            JOptionPane.showMessageDialog(null,”TOTAL SCORE = ” + i);
    }
}

OUTPUT:

out.JPG

homeowner Sales

Filed under: basic java — anai lem @ 2:22 am

/*
This lab problem was downloaded and edited from Thomas L. Brown’s page at
http://boisdarc.tamu-commerce.edu/~tombrown/lab/cs431/lab1fa06

Problem Definition:
Design a program that inputs data about a home to be sold and produces output about the home and a potential sales transaction.

Input (Standard system input–the keyboard):
A homeowner name (String), home price and sales commission rate double; for example .06 for 6% rate).

Output (Standard output–the terminal screen):
The homeowner name, home price, commission rate, and computed amounts for selling cost and the net sale amount to the homeowner.

Processing:
The selling cost is the commission rate times the home price; and the net sale amount is the home price minus the selling cost.
*/

/*programmed by: iana mel*/
import javax.swing.JOptionPane;
class sales
{
public static void main(String args[])
{
String homeownerName, hPrice, comRate;
double homePrice, commissionRate, sellingCost, netsaleAmount;

homeownerName = JOptionPane.showInputDialog(“Enter home owner name”);
hPrice = JOptionPane.showInputDialog(“Enter home price”);
comRate =JOptionPane.showInputDialog(“Enter commission rate \n[example .06 for 6% rate]”);

homePrice = Double.parseDouble(hPrice);
commissionRate = Double.parseDouble(comRate);
sellingCost = commissionRate * homePrice;
netsaleAmount = homePrice – sellingCost;
JOptionPane.showMessageDialog(null,”Owner: ” + homeownerName + “\nHome Price: ” + homePrice +
“\nCommission rate: ” + commissionRate + “\nSelling cost: ” + sellingCost +
“\nNet Sale Amount: ” + netsaleAmount);
}
}

OUTPUT
output.jpg

December 5, 2006

quadratic formula

Filed under: basic java — anai lem @ 5:27 am

/*Create a program that will display the result of the equation
         qF =  ax^2 + bx + c
where
         a=5, b=2 and c=3.
The program should accept the value for x.  Use JOptionPane.showInputDialog for the input value and JOptionPane.showMessageDialog for displaying the result of qF.  Don’t forget to include the library on top of your java program
        import javax.swing.JOptionPane;
*/

import javax.swing.JOptionPane;
class quadFormula {
  public static void main(String args[])
  {
    int x, a=5, b=2, c=3, qF;
    String str = JOptionPane.showInputDialog(“Enter a number”);
    x = Integer.parseInt(str);
    qF = (a*x*x) + (b * x) + c;
    JOptionPane.showMessageDialog(null, “Computed Value: ” + qF);
  }
}

output
output.JPG

Blog at WordPress.com.