- •Lecture 1 Programs and algorithms
- •1.1. How a computer operates?
- •1.2. Algorithms and programming languages
- •If (condition) then ... Or if (condition) then ... Else ...
- •1.3. Summary
- •1.4. Exercises
- •Lecture 2 Fundamentals of programming I
- •2.1. The language
- •2.2. Quick start: first program and basics of syntax
- •2.3. Data in a program (variables and literals)
- •2.4. Operations on data (operators and expressions)
- •2.5. Summary
- •2.6. Exercises
- •Lecture 3 Fundamentals of programming II
- •3.1. Making decisions
- •3.2. Iterations
- •3.3. Arrays
- •3.4. Functions
- •3.5. Summary
- •3.6. Exercises
- •Lecture 4 Java and object-oriented methodology
- •4.1. What is Java
- •4.1.1 Java as a universal programming language
- •4.1.2. Java as a cross-platform language
- •4.1.3. Java as a universal environment for gui programming
- •4.1.4. Java as a universal environment for accessing data bases
- •4.1.5. Java as a universal multimedia programming environment
- •4.1.6. Java as a universal means for client-server programming
- •4.1.7. Java in a distributed environment
- •4.1.8. Java as an environment for building applications from ready-to-use components.
- •4.1.9. Java as the environment for xml processing
- •4.1.10. Micro Java
- •4.1.11. Why is Java worth learning?
- •4.2. Introduction to objects
- •4.4. The first program
- •5.2. Literals
- •5.3. Types of variables. Declarations.
- •Type_name variable_name;
- •Identifiers
- •Naming conventions:
- •5.4. More on operators and expressions
- •5.5. Numeric promotions
- •5.6. Summary
- •5.7. Exercises
- •Lecture 6 Objects
- •6.1. Objects and references
- •6.2. The class String
- •6.3. Useful examples
- •6.4. Summary
- •7.2. Defining attributes of objects
- •7.3. Defining operations on objects (methods)
- •7.4 Defining methods of object creation (constructors)
- •7.5. Example
- •7.6. Inheritance
- •7.7. Summary
- •7.8. Exercises
- •Lecture 8 Classes II
- •8.1. Accessing class members. The variable this.
- •8.2. Static members
- •AClassName.AFieldName
- •8.3. Explicit initialization
- •8.4. Packages and imports
- •8.5. Scope of an identifier. Local variables. Access control.
- •8.6. Structure of a program. Running an application.
- •8.7. Summary
- •8.8. Exercises
- •Lecture 9 Decisions
- •9.1. A brief survey of control statements.
- •9.2. Comparison operators and expressions
- •9.3. Logical operators and expressions
- •9.4. Making decisions: the if and if-else statements.
- •9.5. Multivariant selections done with the switch statement.
- •9.6. The conditional operator ?:
- •9.7. Summary
- •9.8. Exercises
6.3. Useful examples
The following instruction: String s = JOptionPane.showInputDialog("Message"); shows a dialog box with the message "Message" requesting input from the user. After clicking the "OK" button the entered text is stored in the variable s. By closing the dialog box (or by clicking the "Cancel" button) the null value is returned. The instruction: JOptionPane.showMessageDialog(null, "Message"); brings up an information-message dialog titled "Message". To use these dialogs the statement import javax.swing.JOptionPane; must be put in the first line of a program. Dialogs enable interaction with a user. The next program shows how to do it.
import javax.swing.JOptionPane;
public class GreetMsg {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog("Give your name");
if (name == null) name = "";
JOptionPane.showMessageDialog( null, "Hello " + name + "!");
System.exit(0);
}
}
I
t
shows an input dialog requesting input from the user. If we cancel
the dialog without giving a name, the result of the method invocation
JOptionPane.showInputDialog(...)
stored in the variable name
will be null. In such cases we assign to it a reference to an empty
string ("").
The
next call JOptionPane.showMessageDialog(..)
shows a message dialog with the given name.
Second
example concatenates a number of strings given by a user in a dialog
box. Then it displays all of them (one per single line) in a message
dialog.
Closing the dialog box terminates gathering of input
data (strings) from the user. The program does not accept an empty
string. Clicking the "OK" button with no string given as
input results in displaying the message dialog with request for
correct input (or for canceling the operation by clicking "Cancel").
The figure shows the result of running the program with names of some
animals given as input.
import javax.swing.JOptionPane;
public class ShowStrings {
public static void main(String[] args) {
String out = "";
String inp = null;
while ( (inp = JOptionPane.showInputDialog( "Type in something" )) != null) {
if (inp.equals(""))
JOptionPane.showMessageDialog( null,
"Input some text or close the dialog"
);
else out = out + '\n' + inp;
}
if (out.equals("")) out = "No text was given";
JOptionPane.showMessageDialog(null, out);
System.exit(0);
}
}
Note that:
the expression in the while loop uses the fact that the value of the assignment expression is equal to the value of its left argument; it is enclosed in braces because of the low precedence of the assignment operator =.
consecutive lines of output message are created by appending the newline character '\n'
strings are compared using the method equals()
Both programs use invocation of System.exit(0) which terminates the execution of the program. Usually a program terminates after all instructions in the method main have been executed. However, it is not true for programs with graphical user interfaces (more on this later). The dialog boxes are elements of the graphical user interface, so programs using them require explicit termination with call to System.exit(...). How can a user introduce numbers into a program (using dialogs)? If we write a number in a text field of a dialog it is treated as text and in this form it is available in the program. Of course, we cannot carry out arithmetical operations on strings representing numbers. We must convert them to the form in which numbers are represented in the computer.
A string representing a number may be converted to a number with the following method invocation: aNumber = Integer.parseInt(aString); where: aNumber - a variable of the type int aString - an expression of type String
As an example, consider the following program which adds two numbers given by the user:
import javax.swing.*;
public class ParseInt {
public static void main(String[] args) {
String s1 = JOptionPane.showInputDialog("Give the first number");
String s2 = null;
if (s1 != null) {
s2 = JOptionPane.showInputDialog("Give the second number");
if (s2 != null) {
int l1 = Integer.parseInt(s1);
int l2 = Integer.parseInt(s2);
JOptionPane.showMessageDialog(null, "Sum: " + (l1 + l2));
}
}
System.exit(1);
}
}
Run this program with various input strings. Try out strings which do not represent numbers (or even an empty string).
