Java Basics Review
Download
Report
Transcript Java Basics Review
Java Programming
Basics
SE-1010
Dr. Mark L. Hornick
1
A Java program is composed of
one or more classes
One of the classes in the program must be
designated as the main class
class Basic Jav a Program
MyMainClass
+
main(String[]) : void
It must have a main() method
It can have (but doesn’t have to) other methods as well
When the main() method is called, the instructions
within the method begin to execute in sequence
The program terminates (quits, exits, closes) when the
main() method finishes executing
SE-1010
Dr. Mark L. Hornick
2
A program template for simple Java
applications
SE-1010
Dr. Mark L. Hornick
3
main() method components
package somePackageName; // defines the package for this class
import javax.swing.*; // allows classes in package to be used
public class MyMainClass{
public static void main( String[ ] args ) {
// This is the main() method’s BODY.
// Other Java instructions go here…
int x; // Here we declare a variable named x of type int
x = 2;
int y = x + x;
}
}
Note: The main() method is a static
method. Static methods can be
called directly on a class – a class instance
(that is, an object) does not have to exist.
SE-1010
Dr. Mark L. Hornick
4
Programs contain comments which state
the purpose of the program, explain the
meaning of code, and provide other
descriptions to help programmers use and
understand the code.
1.
2.
3.
Comments are not executed
A comment is any sequence of text that begins
with the marker /* and ends with the marker */.
A comment is also defined by the text that follows
the // marker to the end of the current line.
SE-1010
Dr. Mark L. Hornick
5
UML Review
Class diagrams
A UML Class Diagram represents one or more classes and
their relationships to one another
UML is not specific to Java, so a Class Diagram really represents
the generic concept of a class without regard to what language
implements the actual class
The name of the class always appears at the top of a Class Diagram
rectangle
MyMainClass
public class MyMainClass
{
}
SE-2030
Dr. Mark L. Hornick
6
Class diagrams
Operations
A Class Diagram can show class methods or operations
Syntax: [visibility] <name>([ [in|out] param:type]*] [:<return_type>]
MyMainClass
public class MyMainClass
{
public static void main( String[] args) {
…
}
?????
}
What goes here?
SE-2030
Dr. Mark L. Hornick
7
Class diagrams
Operations
A Class Diagram can show class methods or operations
Syntax: [visibility] <name>([ [in|out] param:type]*] [:<return_type>]
The method is underlined if it is static.
MyMainClass
public class MyMainClass
{
<attributes go here>
+ main( args: String[]): void
public static void main( String[] args) {
…
}
}
A Class diagram does not show much
detail – it just shows the class’s
methods (and attributes). It does not
show individual instructions within a
method.
SE-2030
Dr. Mark L. Hornick
8
A UML Sequence diagram shows
messages being sent to Objects
Objects can send many messages between one another
during the course of execution of a program
A given S.D. shows just a sequence of messages for a
specific circumstance
The name of the object and its class type appears at the
top of a Sequence Diagram rectangle
Syntax 1: [<object_name> ]: <class_name>
plotter: WinPlotter
WinPlotter plotter;
SE-2030
Dr. Mark L. Hornick
9
UML Sequence Diagrams
Group Activity
Draw the Sequence Diagram corresponding to the following code:
public class MyApp
{
public static int main (String[] args) {
Account myAcct = new Account();
myAcct.deposit(100.0);
myAcct.deposit(50.0);
float balance = getBalance();
}
}
SE-2030
Dr. Mark L. Hornick
10
Methods within a class are used to
contain Java instructions that
perform a specific function
A way of grouping related Java
instructions together
A technique for organizing your
program into a logical structure
A way to keep methods from getting
too long (doing too much)
The words subroutine and function are alternate, older
(deprecated) terms for method that were used in the days
before Object-oriented programming.
SE-1010
Dr. Mark L. Hornick
11
We already know a little about calling other
class’s methods:
WinPlotter plotter = new WinPlotter();
plotter.moveTo(100, 100);
String string1 = “abcdefg”;
String uppercaseStr1;
uppercaseStr1 = string1.toUpper();
JOptionPane.showMessageDialog( null, string1, “hello”, 2);
String, JOptionPane and WinPlotter are all class identifiers.
plotter, string1 and uppercaseStr1 are all object identifiers.
showMessageDialog is a static method, which means that you can and should call it via the
class – you don’t need to create a JOptionPane object. The moveTo and toUpper methods are
not static, so these methods have to be called through objects – instances of the class in which
the methods are defined.
Actual arguments are passed to a method within the parentheses of a method invocation. Literal
values (like “hello”) or identifiers (like string1, that reference values) can be passed to a method
within the parenthesis. A method may not take any arguments at all, like in toUpper().
If a method returns a value as a result of being called, that value can be
SE-1010
“captured” by assigning it to an object identifier.
Dr. Mark L. Hornick
12
This is the Java syntax for defining
our own methods within our class:
We need to give our method a name, which should be a verb or verb phrase, since
a method implies an action. The first letter is lowercase.
If our method expects actual arguments to be supplied when it is called, we have
to specify each argument’s specific datatype.
We also specify identifiers that the method will use locally (within the method) to represent the
values supplied when the method is called. These identifiers are called the formal arguments.
public class MyApplication {
// main() method not shown because of space limitations
public static boolean printWelcomeMessage(String title, String message) {
// method’s instructions go here…
JOptionPane.showMessageDialog( null, message, title, 2 );
return true;
}
}
SE-1010
Dr. Mark L. Hornick
13
More syntax details for defining our
own methods within our class
Visibility modifier; “public” means that this method can actually be called from the
“outside” – another class or object. “private” methods can’t be called from outside.
“static” means that an instance of the class in which this method is defined
does not have to exist; the method can be called directly on the class. If “static”
is not present, the method must be called through an object.
A method can return any datatype, such as int, float, String, WinPlotter, etc.
“void” means that the method returns no value at all back to the caller.
public class MyApplication {
public static boolean printWelcomeMessage(String title, String message ) {
}
}
// method instructions go here…
JOptionPane.showMessageDialog( null, message, title, 2 );
return true; // always indicate “success”
Don’t forget the
SE-1010
matching braces!
Dr. Mark L. Hornick
14
When you call a method that declares formal
arguments, you pass actual arguments to
the method that indicates what value the
formal argument should have for that call.
public static void main(String args[]) {
// here we pass an String constant as an argument
printWelcomeMessage( “Here is a message” , “Hello” );
String text = “SE1010" ;
// here we pass a String variable
printWelcomeMessage( text, “hi” );
}
public static void printWelcomeMessage(String title, String message ) {
// method instructions go here…
JOptionPane.showMessageDialog( null, message, title, 2 );
}
SE-1010
Dr. Mark L. Hornick
15
The datatype of the actual argument
must be compatible with the datatype of
the matching formal argument
public static void main(String args[]) {
// here we pass an String constant as an argument
printWelcomeMessage( “Hello”, “class” );
String text = “SE1010" ;
// here we pass a String variable
printWelcomeMessage( text, “hi” );
If the formal
argument specifies
a String, then the
actual argument
must be a String!
}
public static void printWelcomeMessage(String title, String message )
{
// method instructions go here…
JOptionPane.showMessageDialog( null, message, title, 2 );
}
SE-1010
Dr. Mark L. Hornick
16
When a method is called, the value of the
actual argument is passed (copied) to the
matching formal argument
public static void main(String args[]) {
// here we pass an String constant as an argument
printWelcomeMessage( “Hello”, “there” );
String text = “SE1010" ;
// here we pass a String variable
printWelcomeMessage( text, “hi” );
The variable
identifier text has
the value “SE1010”
and is passed as an
argument
}
public static void printWelcomeMessage(String title, String message ) {
// method instructions go here…
JOptionPane.showMessageDialog( null, message… );
The parameter
}
identifier message
receives the value
This way of passing the value of
“there” during the 1st
arguments is called a pass-bycall and “hi” during
the 2nd call
value, or call-by-value, scheme.
SE-1010
Dr. Mark L. Hornick
17
Quiz before Lab 2
What is a message argument?
What is a return value?
What method must exist in order for a program to be runnable?
Name the datatypes that represent integer values
Name the datatypes that represent real (or floating-point) values
Explain why certain datatypes (like byte) cannot express numbers
as large as others (like long)
Give examples of the two types of Java comments
SE-1010
Dr. Mark L. Hornick
18