Professional Documents
Culture Documents
Classes
String
JOptionPane
String
String - That object contains data, namely the
sequence of characters that make up the string. It
also contains subroutines. All of these
subroutines are in fact functions.
For example, length is a subroutine that computes
the length of a string. Suppose that str is a
variable that refers to a String. For example, str
might have been declared and assigned a value
as follows:
String str;
str = "Seize the day!";
System.exit( 0 )
} // end of main()
} // end of class Welcome1
import javax.swing.JOptionPane;
public class Welcome4
{
public static void main( String args[] )
{
JOptionPane.showMessageDialog( null,
Hi Java! );
System.exit( 0 );
} // end of main()
} // end of class Welcome4
Build An Application
Addition
When you are building an Application, there is a
set template for design that you automatically
follow.
Get in the habit of doing exactly as will be done
on the next few slides.
import javax.swing.JOptionPane;
import javax.swing.JOptionPane;
public class Addition
{
import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
System.exit( 0 );
} // end of main()
} // end of class Addition
import javax.swing.JOptionPane;
System.exit( 0 );
} // end of main()
} // end of class Addition
import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
int
number1,
number2,
sum;
System.exit( 0 );
} // end of main()
} // end of class Addition
Notice,
int does not
start with a
capital
letter.
import javax.swing.JOptionPane;
String argument
is received.
by the
method.
import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
int
number1,
number2,
sum;
firstNumber = JOptionPane.showInputDialog( First Num );
secondNumber = JOptionPane.showInputDialog( Second Num );
import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
int
number1,
number2,
sum;
firstNumber = JOptionPane.showInputDialog( First Num );
secondNumber = JOptionPane.showInputDialog( Second Num );
import javax.swing.JOptionPane;
public class Addition
{
public static void main( String args[] )
{
String firstNumber,
secondNumber;
int
number1,
number2,
sum;
firstNumber = JOptionPane.showInputDialog( First Num );
secondNumber = JOptionPane.showInputDialog( Second Num );
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
sum = number1 + number2;
import javax.swing.JOptionPane;
public class Addition
The the
method
class JOptionPane
takes four
icons,showMessageDialog
you have five alternateofconstants
to choose from:
{ For
public static void main( String args[] )
arguments:
JOptionPane.PLAIN_MESSAGE
{
null -- this willString
be explained
in a later chapter
firstNumber,
JOptionPane.ERROR_MESSAGE
secondNumber;
The Sum is: + sum --this
converts the int sum into a String and
JOptionPane.INFORMATION_MESSAGE
int
number1,
concatenates it with thenumber2,
String The Sum is:
JOptionPane.WARNING_MESSAGE
sum;
Results is the message
displayed in the title bar.
JOptionPane.QUESTION_MESSAGE
JOptionPane.PLAIN_MESSAGE
defines the icon.
firstNumber = JOptionPane.showInputDialog(
First Num );
secondNumber
= JOptionPane.showInputDialog(
Second Num );
In Java, Constants
are always all upper
case, with words separated by underscores.
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber);
sum = number1 + number2;
JOptionPane.showMessageDialog( null, The Sum is: + sum,
Results, JOPtionPane.PLAIN_MESSAGE );
System.exit( 0 );
} // end of main()
} // end of class Addition