sid's
Would you like to react to this message? Create an account in a few clicks or log in to continue.

sid's

OPen Discussion
 
HomePortalSearchLatest imagesRegisterLog in

 

 EXAMPLE: LAB2

Go down 
AuthorMessage
Admin
Admin



Posts : 50
Join date : 2007-08-10
Age : 44
Location : Kuala Lumpur

EXAMPLE: LAB2 Empty
PostSubject: EXAMPLE: LAB2   EXAMPLE: LAB2 Icon_minitimeTue Jan 29, 2008 6:54 pm

import javax.swing.*;

public class RainFall
{

public static void main(String[] args)

{

double average;

String m1 = JOptionPane.showInputDialog("Enter month>>");

String str = JOptionPane.showInputDialog("Amount rain (mm)");
double rain_m1=Double.parseDouble(str);

String m2 = JOptionPane.showInputDialog("Enter month>>");

str = JOptionPane.showInputDialog("Amount rain (mm)");
double rain_m2=Double.parseDouble(str);

String m3 = JOptionPane.showInputDialog("Enter month>>");

str = JOptionPane.showInputDialog("Amount rain (mm)");
double rain_m3=Double.parseDouble(str);

average = (rain_m1+rain_m2+rain_m3)/3;

JOptionPane.showMessageDialog(null,"The average rainfall for "+m1+ ", "+
m2+" and "+m3+" is " +average,"RainFall",JOptionPane.INFORMATION_MESSAGE);
}
}


Last edited by on Wed Jan 30, 2008 8:55 am; edited 1 time in total
Back to top Go down
https://sidnazuha.board-directory.net
Admin
Admin



Posts : 50
Join date : 2007-08-10
Age : 44
Location : Kuala Lumpur

EXAMPLE: LAB2 Empty
PostSubject: Q2   EXAMPLE: LAB2 Icon_minitimeTue Jan 29, 2008 6:56 pm

import javax.swing.*;

public class AddInteger
{

public static void main(String[] args)
{
int digit=0,sum=0;

String str = JOptionPane.showInputDialog("Enter an Integer between 0 to 1000>>"); // number
int input=Integer.parseInt(str);


if (input>=0 && input<=1000){

while (input != 0) { // more digits?

digit = input % 10; // extract digit
System.out.println(digit);
input = input / 10; // remove digit
sum=sum+digit;
}
}
else{
JOptionPane.showMessageDialog(null,"Incorrect Input","Add Integer",JOptionPane.ERROR_MESSAGE);
}


System.out.println("Total is : " +sum);
}
}
Back to top Go down
https://sidnazuha.board-directory.net
 
EXAMPLE: LAB2
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
sid's :: TEACHING MATERIAL :: UPM-SAK3100-
Jump to: