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

February 20, 2007

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

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

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

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

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

December 7, 2006

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

November 30, 2006

gross net tax

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

/* Create a program that will compute the gross and netpay of an employee where rate = 100 and hrs worked = 8. The formula for gross, tax and net is as follows:
gross = rate * hrs worked
tax = 5% of gross
net = gross – tax
*/

class grossNet
{
public static void main(String args[])
{
double gross, net, tax, rate = 100, hrsWorked=8;
gross = rate * hrsWorked;
tax = gross * .05;
net = gross – tax;
System.out.println(“GROSS = ” + gross);
System.out.println(“TAX = ” + tax);
System.out.println(“NET = ” + net);
}
}

output
GROSS = 800.0
TAX = 40.0
NET = 760.0

November 28, 2006

Operators / * – +

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

/*
Compute and display the values for x, y and its average.
x = 5a
—- – 4a
10a

y = 2(a^2) + 6a
*/

class compute {
public static void main(String args[]) {
int a = 5;
int x = ((5*a) / (10*a)) – (4*a);
int y = (2*a*a) + (6*a);
int ave = (x + y )/2;
System.out.println(“x = ” + x);
System.out.println(“y = ” + y);
System.out.println(“average = ” + ave);
}
}

Output:
x = -20
y = 80
average = 30

Next Page »

Blog at WordPress.com.