Professional Documents
Culture Documents
Chapter 2
Introduction to Java
Applications
} // end of main()
} // end of class Welcome1
} // end of main()
} // end of class Welcome1
} // end of main()
} // end of class Welcome1
} // end of main()
} // end of class Welcome1
} // end of main()
} // end of class Welcome1
} // end of main()
} // end of class Welcome1
} // end of main()
} // end of class Welcome1
} // end of main()
} // end of class Welcome1
} // end of main()
} // end of class Welcome1
System.exit( 0 )
} // end of main()
} // end of class Welcome1
import javax.swing.JOptionPane
System.exit( 0 )
} // end of main()
} // end of class Welcome1
Addition
2.) Define your class name, and right away place the
opening and closing brackets--with the comment.
System.exit( 0 );
} // end of main()
} // end of class Addition
System.exit( 0 );
} // end of main()
These two are “String” references. That means they
} // end of class Addition
have the potential to point to objects of type String.
However, at this point, they point to nothing.
4.) Include any local variables you will need in this
They are A
method. empty
localreferences.
variable is visible and accessible only
within the method.
Java I--Copyright © 2000-2004 Tom Hunter
import javax.swing.JOptionPane;
d
Java I--Copyright © 2000-2004 Tom Hunter
• This is the hierarchy for
the JOptionPane.
• We will cover “inheritance”
starting in Chapter 8, but you
need to begin learning these API class libraries.
• The Class JOptionPane has several methods.
A class’s methods are its capabilities.
• For now, you should know that method
showInputDialog()
receives a String argument, and
returns a String result.
Java I--Copyright © 2000-2004 Tom Hunter
import javax.swing.JOptionPane;
System.exit( 0 );
} // end of main()
} // end of class Addition
JOptionPane.showMessageDialog( null,
“The Sum is: ” + number1 + number2,
“Screwy Result”, JOptionPane.WARNING_MESSAGE );
int num1;
• You declare a variable and
initialize it on the same line.
num1 = 2;
• This is a declaration. At this point,
the name num1 refers to a location
{a pointer} in the computer’s RAM
int num1=2;
where this variable is stored.
• Because an int is declared, we
know that four bytes are set aside.
• Still, nothing is stored in it yet.
Java I--Copyright © 2000-2004 Tom Hunter
Primitive Data Types
size
• When we assign a type [ int, String] to a
variable, we are not only declaring a memory
location.
• We also decide how big of a number or character
is able to be stored in that variable.
• Multiplication *
• What happens if you multiply variables of different types?
int x = 2;
double y = 3.889, sum = 0.000;
sum = y * x;
• Division /
• Division can lead to unexpected results:
If both operands are integers, then the result of the
division is also an integer.
Any fractional part of the division is discarded.
Therefore: 17/3 = 5
• Modulus %
• The modulus operator is confusing at first, but
eventually it becomes your good friend.
In contrast to the division operator, it returns the
remainder of any division. The modulus operator can
only be used when both operands are integers.
17 % 3 = 2
You say this “17 modulus 3 equals 2”
equality ==
Note that two equal signs are always used.
The single equal sign [ = ] is only used for
assignment, that is, assigning the value on the right to the
variable on the left.
num1 = 33;
equality ==
not equal !=
greater than or equal >=
less than or equal <=
• The
Onlystatement
one statementiscan be made
executed conditional
if the without
expression is true.
brackets. If you wish to conditionally execute more than
one statement, you use brackets to create a block.
statement3;
statement4; } //
end of else
if( expression )
statement;
else if( expression )
statement;
else
statement;