Lab#3 - WordPress.com

advertisement
Object Oriented
Programming (OOP)
LAB # 3
TA. Maram
& TA. Mubaraka
TA. Kholood
& TA. Aamal
JOptionPane
 JOptionPane is an easy way to do dialog boxes,
messages or inputs.
You have to import it at the beginning of the program:
 Java:
import javax.swing.JOptionPane;
The arguments for JOptionPane
for a simple message box:
Java:
JOptionPane.showMessageDialog (
null, "Put your message here" );
to show text in different rows:
JOptionPane.showMessageDialog (
null, "Put \nyour \nmessage \nhere" );
the \n switches to next line
The arguments for JOptionPane
 An example of using the simple message box
The arguments for JOptionPane
 To show different messages with different icons, you'll need 4
arguments instead of 2.
 Java:
JOptionPane.showMessageDialog (
null, "Messagehere", "Title here", JOptionPane.IconName);
 There are different kinds of icons for a dialog box, just replace the
last argument:
The arguments for JOptionPane
 You can also use JOptionPane for dialog boxes for input,
and assign them to variables, just put:
 Java:
name= // The variable
JOptionPane.showInputDialog ( "put your message here"
);
 Notice that the variable here is String and you can
convert it to integer as the following:
 int x=Integer.parseInt(name);
 note: after using JOptionPane, Dont forget to exit
System.exit(0);
Example#1
 Using JOptionPane, Write a program that asks a user to
insert two numbers and displays the summation of them.
More Exercises on Java
Exercise #1
 Develop a Java application that will determine whether a departmentstore customer has exceeded the credit limit on a charge account. For
each customer, the following facts are available:
 a) account number,
 b) balance at the beginning of the month,
 c) total of all items charged by the customer this month,
 d) total of all credits applied to the customer’s account this month and
 e) allowed credit limit.
 The program should input each of these facts from input dialogs as integers,
calculate the new balance (= beginning balance + charges – credits),
display the new balance and determine whether the new balance exceeds
the customer’s credit limit. For those customers whose credit limit is
exceeded, the program should display the message “Credit limit
exceeded.”
Solution#1
Testing #1
Exercise #2
 A palindrome is a sequence of characters that reads
the same backward as forward. For example, each of
the following five-digit integers is a palindrome: 12321,
55555, 45554 and 11611. Write an application that reads
in a five-digit integer and determines whether it is a
palindrome. If the number is not five digits long, display
an error message dialog indicating the problem to the
user. When the user dismisses the error dialog, allow the
user to enter a new value.
Solution #2
Testing #2
Exercise#3 (Homework)
 A company wants to transmit data over the telephone, but is
concerned that its phones may be tapped. It has asked you to write
a program that will encrypt its data so that the data may be
transmitted more securely. All of its data is transmitted as four-digit
integers. Your application should read a four-digit integer entered
by the user in an input dialog and encrypt it as follows: Replace
each digit with the result of adding 7 to the digit and getting the
remainder after dividing the new value by 10. Then swap the first
digit with the third, and swap the second digit with the fourth. Then
print the encrypted integer. Write a separate application that inputs
an encrypted four-digit integer and decrypts it to form the original
number.
Download