Thursday, February 02, 2006

JAWABAN LAB4

soalan1
import javax.swing.*;
import java.text.*;

public class lab_dip_4_soalan1
{
public static void main(String args[])
{
int input;
String output="";

input=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a value : "));

while(input!=0)
{
output+=input+"\t ";
input--;
}

JOptionPane.showMessageDialog(null,output,"USING WHILE",JOptionPane.INFORMATION_MESSAGE); System.exit(0);
}
}

soalan2
import javax.swing.*;
import java.text.*;

public class lab_dip_4_soalan2
{
public static void main(String args[])
{
int sum=0;
int input;

do
{
input=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter an integer value\nProgram exit if input is 0\n"));

sum+=input;
}while(input!=0);

JOptionPane.showMessageDialog(null,"Sum : "+sum,"USING DO-WHILE",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}


soalan3
import javax.swing.*;
import java.text.*;

public class lab_dip_4_soalan3
{
public static void main(String args[])
{
int item;
String deli;
int bil_item;
double item_price,price;
DecimalFormat p=new DecimalFormat("0.00");

item=Integer.parseInt(JOptionPane.showInputDialog(null,"My Favourite Bakery\n\nCODE BREAD\n210 Mexican Bun\n122 Chocolate Donut\n768 Sausage Bun\n234 Cheese Stick \n\n[Enter Code] \nI want to buy :"));

bil_item=Integer.parseInt(JOptionPane.showInputDialog(null,"How many do you want to buy?"));

switch(item)
{
case 210: item_price=1.90;
deli="Mexican Bun";
break;
case 122: item_price=1.80;
deli="Chocolate Donut";
break;
case 768: item_price=1.50;
deli="Sausage Bun";
break;
case 234: item_price=1.00;
deli="Cheese Stick";
break;
default: item_price=0.00;
bil_item=0;
deli="No such thing at our bakery...";
}

price=item_price*bil_item;

JOptionPane.showMessageDialog(null,deli+"\n\nBuy = "+bil_item+"\n\nPrice = RM "+p.format(price),"USING SWITCH STATEMENT",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}

No comments: