sid's

OPen Discussion
 
HomePortalCalendarFAQSearchRegisterMemberlistLog in

Share | 
 

 EXAMPLE: LAB2

View previous topic View next topic Go down 
AuthorMessage
Admin
Admin


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

PostSubject: EXAMPLE: LAB2   Tue 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
View user profile http://sidnazuha.forumotion.com
Admin
Admin


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

PostSubject: Q2   Tue 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
View user profile http://sidnazuha.forumotion.com
 
EXAMPLE: LAB2
View previous topic View next topic 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: