Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics."— Presentation transcript:

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

2 SE-1010 Dr. 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 class 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

3 SE-1010 Dr. Mark L. Hornick 3 A program template for simple Java applications

4 SE-1010 Dr. Mark L. Hornick 4 main() method components 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. 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; }

5 SE-1010 Dr. 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.

6 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 SE-2030 Dr. Mark L. Hornick 6 MyMainClass public class MyMainClass { }

7 Class diagrams Operations A Class Diagram can show class methods or operations Syntax: [visibility] ([ [in|out] param:type]*] [: ] SE-2030 Dr. Mark L. Hornick 7 MyMainClass public class MyMainClass { public static void main( String[] args) { … } ????? What goes here?

8 Class diagrams Operations A Class Diagram can show class methods or operations Syntax: [visibility] ([ [in|out] param:type]*] [: ] The method is underlined if it is static. SE-2030 Dr. Mark L. Hornick 8 MyMainClass public class MyMainClass { public static void main( String[] args) { … } + 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.

9 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: [ ]: SE-2030 Dr. Mark L. Hornick 9 plotter: WinPlotterWinPlotter plotter;

10 UML Sequence Diagrams Group Activity Draw the Sequence Diagram corresponding to the following code: SE-2030 Dr. 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(); }

11 SE-1010 Dr. 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.

12 SE-1010 Dr. 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.

13 SE-1010 Dr. 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, 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.

14 SE-1010 Dr. 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 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.

15 SE-1010 Dr. 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 ); }

16 SE-1010 Dr. 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 ); }

17 SE-1010 Dr. 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 1 st call and “hi” during the 2 nd call

18 SE-1010 Dr. 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


Download ppt "SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics."

Similar presentations


Ads by Google