Programming in Java - 4
Programming in Java - 4
Session 2
length area
program perimeter
width
Example 4.7
public static void main(String[] args)
{
int L , W , area , perimeter;
L =Integer.parseInt(JOptionPane.showInputDialog("enter the length of rectangle="));
W =Integer.parseInt(JOptionPane.showInputDialog("enter the width of rectangle="));
area=L*W;
perimeter=(L+W)*2;
JOptionPane.showMessageDialog(null,"area="+area+"\nperimeter="+perimeter);
}
Example 4.8: Program to calculate
the area and perimeter of circle
radius area
program perimeter
Constant and Variable
• Variable may be takes many values.
• Constant takes one value.
money in $
$ price program money in Lira Turkey
Example 4.9
public static void main(String[] args)
{
double L , D , Dprice;
D =Double.parseDouble(JOptionPane.showInputDialog("money in Dolar="));
Dprice=Double.parseDouble(JOptionPane.showInputDialog("Dolar Price="));
L=D*Dprice;
JOptionPane.showMessageDialog(null,"money in Lera="+L);
}
Homework
1) Write a program to calculate the area and perimeter of triangle
a, b , c are the side of triangle
Perimeter= a+b+c
2) Write a program that prompts the user to enter a time in second and
converts this time to minutes and second. for example when I enter 200sec
the program displays 3min and 20sec.