java programming basics

18
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics

Upload: hollee-mcneil

Post on 30-Dec-2015

21 views

Category:

Documents


1 download

DESCRIPTION

Java Programming Basics. A Java program is composed of one or more classes. One of the classes in the program must be designated as the main class It must have a main() method It can have (but doesn’t have to) other methods as well - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Java Programming Basics

SE-1010Dr. Mark L. Hornick

1

Java Programming Basics

Page 2: Java Programming Basics

SE-1010Dr. Mark L. Hornick

2

A Java program is composed of one or more classes

One of the classes in the program must be designated as the main classIt 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 sequenceThe program terminates (quits, exits, closes) when the

main() method finishes executing

class Basic Jav a Program

MyMainClass

+ main(String[]) : void

Page 3: Java Programming Basics

SE-1010Dr. Mark L. Hornick

3

A program template for simple Java applications

Page 4: Java Programming Basics

SE-1010Dr. Mark L. Hornick

4

main() method components

Note: The main() method is a static method. Static methods can becalled directly on a class – a class instance(that is, an object) does not have to exist.

package somePackageName; // defines the package for this classimport javax.swing.*; // allows classes in package to be usedpublic 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;

}}

Page 5: Java Programming Basics

SE-1010Dr. Mark L. Hornick

5

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. Comments are not executed

2. A comment is any sequence of text that begins with the marker /* and ends with the marker */.

3. A comment is also defined by the text that follows the // marker to the end of the current line.

Page 6: Java Programming Basics

UML ReviewClass diagrams

A UML Class Diagram represents one or more classes and their relationships to one anotherUML 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

SE-2030Dr. Mark L. Hornick

6

MyMainClasspublic class MyMainClass {}

Page 7: Java Programming Basics

Class diagramsOperations

A Class Diagram can show class methods or operationsSyntax: [visibility] <name>([ [in|out] param:type]*] [:<return_type>]

SE-2030Dr. Mark L. Hornick

7

MyMainClasspublic class MyMainClass{

public static void main( String[] args) {…

}}

?????

What goes here?

Page 8: Java Programming Basics

Class diagramsOperations

A Class Diagram can show class methods or operationsSyntax: [visibility] <name>([ [in|out] param:type]*] [:<return_type>]

The method is underlined if it is static.

SE-2030Dr. Mark L. Hornick

8

MyMainClasspublic class MyMainClass{

public static void main( String[] args) {…

}}

<attributes go here>

+ main( args: String[]): void

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.

Page 9: Java Programming Basics

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>

SE-2030Dr. Mark L. Hornick

9

plotter: WinPlotter WinPlotter plotter;

Page 10: Java Programming Basics

UML Sequence DiagramsGroup Activity

Draw the Sequence Diagram corresponding to the following code:

SE-2030Dr. Mark L. Hornick

10

public class MyApp{ public static int main (String[] args) { Account myAcct = new Account(); myAcct.deposit(100.0); myAcct.deposit(50.0); float balance = getBalance();

}}

Page 11: Java Programming Basics

SE-1010Dr. Mark L. Hornick

11

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.

Page 12: Java Programming Basics

SE-1010Dr. Mark L. Hornick

12

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.

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 “captured” by assigning it to an object identifier.

plotter, string1 and uppercaseStr1 are all object identifiers.

Page 13: Java Programming Basics

SE-1010Dr. Mark L. Hornick

13

This is the Java syntax for defining our own methods within our class:

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; }

}

We need to give our method a name, which should be a verb or verb phrase, sincea method implies an action. The first letter is lowercase.

If our method expects actual arguments to be supplied when it is called, we haveto specify each argument’s specific datatype.

We also specify identifiers that the method will use locally (within the method) to represent thevalues supplied when the method is called. These identifiers are called the formal arguments.

Page 14: Java Programming Basics

SE-1010Dr. Mark L. Hornick

14

More syntax details for defining our own methods within our class

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 matching braces!

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 defineddoes 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.

Page 15: Java Programming Basics

SE-1010Dr. Mark L. Hornick

15

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 );

}

Page 16: Java Programming Basics

SE-1010Dr. Mark L. Hornick

16

The datatype of the actual argument must be compatible with the datatype of the matching formal argument

If the formal argument specifies a String, then the actual argument must be a String!

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” ); }public static void printWelcomeMessage(String title, String message ) { // method instructions go here… JOptionPane.showMessageDialog( null, message, title, 2 );}

Page 17: Java Programming Basics

SE-1010Dr. Mark L. Hornick

17

When a method is called, the value of the actual argument is passed (copied) to the matching formal argument

This way of passing the value of arguments is called a pass-by-value, or call-by-value, scheme.

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” ); }public static void printWelcomeMessage(String title, String message ) { // method instructions go here… JOptionPane.showMessageDialog( null, message… );}

The variable identifier text has the value “SE1010” and is passed as an argument

The parameter identifier message receives the value “there” during the 1st call and “hi” during the 2nd call

Page 18: Java Programming Basics

SE-1010Dr. Mark L. Hornick

18

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