Input & Output
Dr. Ali Allam Introduction to Programming (BIS227E)
Arithmetic Operators
(continued)
Arithmetic Operators
Operator Name Explanation Example
5+2→7
Addition / Add x to y, if they are numbers
x+y "Ali" + "Allam" →
Concatenation Concatenate x and y, if they are Strings
"AliAllam"
x–y Subtraction Subtract (y) from (x) 7–4→3
x*y Multiplication Multiply (x) by (y) 6 * 3 → 18
9/2→4
x/y Division Result of dividing (x) by (y)
9 / 2.0 → 4.5
x%y Modulus Remainder of dividing (x) by (y) 9%2→1
Unary Arithmetic Operators
The five previous operators are called binary operators. These
operators need two operands (two values). For example, x + y
Now, you will be introduced to unary operators. This means that these
operators need only one operand (one value only) !
Typically, these unary operators are the Increment and Decrement.
The increment operator is used to increment a variable's value by 1.
The decrement operator is used to decrement a variable's value by 1.
Unary Arithmetic Operators
Unary Operator Name Explanation
x++ Post-increment Returns x, then increments x by one
++x Pre-increment Increments x by one, then returns x
x-- Post-decrement Returns x, then decrements x by one
--x Pre-increment Decrements x by one, then returns x
Example (1)
public class Prog1
{
public static void main(String[ ] args)
{ Output Console
int a = 5;
int b = 10; a = 6 and b = 9
a++; // same as ++a a = 5 and b = 10
--b; // same as b-- x = 6 and y = 10
System.out.println("a = " + a +" and b = " +b);
int x = a--; // assign a to x, then decrement a
int y = ++b; // increment b, then assign it to y
System.out.println("a = " + a +" and b = " +b);
System.out.println("x = " + x +" and y = " +y);
}
}
Augmented Assignment Operators
Operator Name Example Explanation
+= Addition assignment x += 8 x = x+8
-= Subtraction assignment x -= 8 x = x-8
*= Multiplication assignment x *= 8 x = x*8
/= Division assignment x /= 8 x = x/8
%= Modulus assignment x %=8 x = x%8
Input Statements
Input Statements
❑ Typically, there are two ways to read an input from the user:
1. Read an input from the Console: using the library → java.util.*
Scanner input = new Scanner(System.in);
System.out.print("Enter your GPA: ");
double gpa = input.nextDouble();
2. Read an input from a Dialog Box: using the library → javax.swing.*
String x = JOptionPane.showInputDialog("Enter your GPA: ");
double gpa = Double.parseDouble(x);
Example (2): Read the input from the Console
import java.util.*;
public class Prog2A
{
public static void main(String[ ] args)
{
Scanner input = new Scanner(System.in); Output Console
System.out.print("Enter your name: ");
String name = input.next(); Enter your name: Ali
System.out.print("Enter your age: "); Enter your age: 28
int age = input.nextInt(); Enter your GPA: 3.22
System.out.print("Enter your GPA: "); Your name is Ali
double gpa = input.nextDouble(); You are 28 years
System.out.println("Your name is " +name); Your GPA is 3.22
System.out.println("You are " +age+ " years");
System.out.println("Your GPA is " +gpa);
}
}
Example (2): Read the input from a Dialog Box
import javax.swing.*;
public class Prog2B Important Note: any
{
input value entered
public static void main(String[ ] args)
{ by the user is treated
String name = JOptionPane.showInputDialog("Enter your name: "); as a String even if
String ageStr = JOptionPane.showInputDialog("Enter your age: "); the input consists of
int age = Integer.parseInt(ageStr);
numeric digits.
String gpaStr = JOptionPane.showInputDialog("Enter your GPA: ");
double gpa = Double.parseDouble(gpaStr);
JOptionPane.showMessageDialog(null, "Your name is " +name
+"\nYou are " +age+ " years\nYour GPA is " +gpa);
}
}
Read the input from a Dialog Box
Example (3): Add two input float numbers
import javax.swing.*;
public class Prog3A
{
public static void main(String[ ] args)
{
String xStr = JOptionPane.showInputDialog("Enter the first number: ");
float x = Float.parseFloat(xStr);
String yStr = JOptionPane.showInputDialog("Enter another number: ");
float y = Float.parseFloat(yStr);
float sum = x+y;
JOptionPane.showMessageDialog(null, "The sum is: " +sum);
}
}
Example (3): Add two input float numbers
How about removing the unnecessary String variables from our code. It would look
much better ☺
import javax.swing.*;
public class Prog3B
{
public static void main(String[ ] args)
{
float x = Float.parseFloat( JOptionPane.showInputDialog("Enter the first number: ") );
float y = Float.parseFloat( JOptionPane.showInputDialog("Enter another number: ") );
float sum = x+y;
JOptionPane.showMessageDialog(null, "The sum is: " +sum);
}
}
Example (4): Calculate the sum, product and
average of three input integers
import javax.swing.*;
public class Prog4
{
public static void main(String[ ] args)
{
int x = Integer.parseInt( JOptionPane.showInputDialog("Enter the first number: ") );
int y = Integer.parseInt( JOptionPane.showInputDialog("Enter the second number: ") );
int z = Integer.parseInt( JOptionPane.showInputDialog("Enter the third number: ") );
int sum = x+y+z;
int prod = x*y*z;
double avg = sum/3.0; // Be aware of integer division!
JOptionPane.showMessageDialog(null, "Sum=" +sum+ "\nProduct=" +prod + "\nAverage=" +avg);
}
}
Example (5): Calculate the number of dozens
and the left over of a given number of apples
import javax.swing.*;
public class Prog5
{
public static void main(String[ ] args)
{
int apples = Integer.parseInt( JOptionPane.showInputDialog("Enter the number of apples: ") );
int dozens = apples/12; // taking advantage of integer division!
int remainder = apples%12;
JOptionPane.showMessageDialog(null, apples+ " apples contain " +dozens + " dozens and "
+remainder+ " extra remaining apples");
}
}
We can also change the default appearance of the
message box.
Can you notice the
title bar and the icon
shown in these
message boxes?
JOptionPane.showMessageDialog( )
import javax.swing.*;
public class prog1
{
public static void main(String[ ] args)
{
JOptionPane.showMessageDialog(null, "This is an error message !", "Invalid", 0);
JOptionPane.showMessageDialog(null, "This is an information message !", "Remember", 1);
JOptionPane.showMessageDialog(null, "This is a warning message !", "Be Careful", 2);
JOptionPane.showMessageDialog(null, "This is a questioning message !", "How and Why", 3);
JOptionPane.showMessageDialog(null, "This is a plain message !", "Our Vision", -1);
}
}