1 / 18

Java Programming Basics

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

Download Presentation

Java Programming Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 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 SE-1010Dr. Mark L. Hornick

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

  4. 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 becalled directly on a class – a class instance(that is, an object) does not have to exist. 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. • 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-1010Dr. Mark L. Hornick

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

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

  8. Class diagramsOperations 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. public class MyMainClass { public static void main( String[] args) { … } } MyMainClass <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. SE-2030 Dr. Mark L. Hornick

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

  10. UML Sequence DiagramsGroup 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

  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. SE-1010Dr. Mark L. Hornick

  12. We already know a little about calling other class’s methods: WinPlotterplotter = new WinPlotter();plotter.moveTo(100, 100);Stringstring1 = “abcdefg”;StringuppercaseStr1;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 notstatic, 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. SE-1010Dr. Mark L. Hornick

  13. 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, 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 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(Stringtitle, Stringmessage) { // method’s instructions go here… JOptionPane.showMessageDialog( null, message, title, 2 ); return true; } } SE-1010Dr. Mark L. Hornick

  14. 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 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. public class MyApplication { publicstaticboolean 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! 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 Stringvariable printWelcomeMessage( text, “hi” ); } public static void printWelcomeMessage(String title, String message ) { // method instructions go here… JOptionPane.showMessageDialog( null, message, title, 2 ); } SE-1010Dr. Mark L. Hornick

  16. The datatype of the actual argumentmust 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 Stringvariable printWelcomeMessage( text, “hi” ); } public static void printWelcomeMessage(String title, String message ) { // method instructions go here… JOptionPane.showMessageDialog( null, message, title, 2 ); } 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 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 This way of passing the value of arguments is called a pass-by-value, or call-by-value, scheme. The parameter identifier message receives the value “there” during the 1st call and “hi” during the 2nd call 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 SE-1010Dr. Mark L. Hornick

More Related