Programming in JAVA
Session 2
Dr. Fatima Kelish
Outline
• String type
• Dialog boxes
• Constant and variable
• Program to add two numbers
• Program to calculate the area and perimeter of rectangle
• Program to calculate the area and perimeter of circle
• Program to change an amount of money form $ to Syrian pounds.
String Type
• String is one or more characters for example:
“Ahmad” “abc9” “H@@?” “M” “938” “ “
• String variable declaration
String s1 , s2 , s ;
• String assign statement:
s1 = “mohamad”;
s2=“ samer”;
• Strings join:
s=s1+s2;
Example 4.1
public static void main(String[] args)
{
String s1,s2,s;
s1 = "mohammad";
s2 = " samer";
s = s1 + s2;
System.out.println(s);
s1 = "5";
s2 = "3";
s = s1 + s2;
System.out.println(s);
}
Example 4.2
public static void main(String[] args)
{
int x, y, z;
x = 10;
y = 20;
z = x + y;
String s;
s = "first number =" + x + " second number=" + y + " result=" + z;
System.out.println(s);
}
Dialog boxes
• Windows using to view messages
• Included in JOptionPane class
• Including statement
import javax.swing.JOptionPane;
• Input dialog
JOptionPane.showInputDialog
• Output dialog
JOptionPane.showMessageDialog
Similar to System.out.println()
Example 4.3
public static void main(String[] args)
{
String s1, s2, s;
s1 = JOptionPane.showInputDialog("first=");
s2 = JOptionPane.showInputDialog("second=");
s = s1 + s2;
System.out.println(s);
}
Example 4.4
public static void main(String[] args)
{
String s1, s2;
int x, y, z;
s1 = JOptionPane.showInputDialog("first=");
x = Integer.parseInt(s1);
s2 = JOptionPane.showInputDialog("second=");
y = Integer.parseInt(s2);
z = x + y;
System.out.println(z);
}
Example 4.5
public static void main(String[] args)
{
int x, y, z;
x =Integer.parseInt(JOptionPane.showInputDialog("first="));
y =Integer.parseInt(JOptionPane.showInputDialog("second="));
z = x + y;
System.out.println(z);
}
Example 4.6
public static void main(String[] args)
{
int x, y, z;
x =Integer.parseInt(JOptionPane.showInputDialog("first="));
y =Integer.parseInt(JOptionPane.showInputDialog("second="));
z = x + y;
JOptionPane.showMessageDialog(null,z);
}
Example 4.7: Program to calculate
the area and perimeter of rectangle
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.
int b , a = 10; final int b, a=10;
b=a*3; b=a*3;
a=50; a=50;
• Why we use the constant:
- Using names is easier than using values.
- When we want to change the value of the constant we have to change
the declaration statement only.
Example 4.8
public static void main(String[] args)
{
double R , area , perimeter;
final double P=3.14;
R =Double.parseDouble(JOptionPane.showInputDialog("Radius="));
area=P*Math.pow(R,2); // area=P*R*R;
perimeter=2*P*R;
JOptionPane.showMessageDialog(null,"area="+area+"\nperimeter="+perimeter);
}
Example 4.9: Program to change an amount
of money from $ to Syrian pounds.
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.