html5-img
1 / 23

Java Programming Basics

Java Programming Basics. Computer of the future?. Algorithms are implemented in Java methods. A method is a named collection of Java instructions that implement an algorithm The method for actually computing the number of days between two dates might be named computeDays

howie
Télécharger la présentation

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

  2. Computer of the future? SE-1011 Dr. Mark L. Hornick

  3. Algorithms are implemented in Java methods • A method is a named collection of Java instructions that implement an algorithm • The method for actually computing the number of days between two dates might be named computeDays • The method names - or method identifiers - are made up by you (…but you have to follow some rules…) SE-1011 Dr. Mark L. Hornick

  4. Good and bad method identifiers • Good method names are • meaningful • use verbs that imply an action • use camel-case format • Bad method names are • Vague or meaningless • don’t use verbs • use an unconventional format • computeDays • printResult • getStartDate • days • getIt • method1 • PRINTit • x109 SE-1011 Dr. Mark L. Hornick

  5. Java is also case-sensitive… • This means that the following method identifiers are considered separate and distinct: • getStartDate • getStartdate Avoid creating identifiers that differ only in capitalization SE-1011 Dr. Mark L. Hornick

  6. Java’s reserved words cannot be used as identifiers abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized Don’t create identifiers that differ from reserved wordsonly in capitalization (e.g. Continue) SE FocusDr. Mark L. Hornick

  7. Algorithms range from trivially simple to incredibly complex • An extremely simple program may consist of a single algorithm that just prints something to the screen • Realistic programs typically implement multiple algorithms of varying complexity SE-1011 Dr. Mark L. Hornick

  8. If an algorithm is complex, it is usually broken into multiple methods • Normally, each method handles a single task • this keeps things organized and simpler • Sub-tasks are delegated to subordinate methods • Structuring a program into tasks and sub-tasks is a skill you’ll develop as you gain experience creating Java programs SE-1011 Dr. Mark L. Hornick

  9. Related methods are grouped together in named classes • Normally, the methods with a class share information, but hide information from other classes • This keeps information from being changed or corrupted accidentally • However, there are ways for methods of one class to send or receive information from methods of another class SE-1011 Dr. Mark L. Hornick

  10. Good and bad class names • Good class names are • meaningful • use nouns that imply the purpose • Start with an uppercase letter • Bad class names are • Vague or meaningless • don’t use nouns • use an unconventional format • DayCalculator • Lab2Program • TVController • days • Calculate • Class1 • x109 SE-1011 Dr. Mark L. Hornick

  11. Every Java program must have a class that contains a primary method, named main • This name main (not a verb) exists for historical reasons, and cannot be changed • The class containing the main method can be named anything (that makes sense), like DayCalculator • The class containing the main method, regardless of it’s actual name, is referred to as the main class SE-1011 Dr. Mark L. Hornick

  12. How a Java program gets executed DayCalculator SE-1011 Dr. Mark L. Hornick

  13. Summary: The basic Java Program To create a runnable program, we must first define a main class that represents our program • We can give the main class any reasonable name, like “MyMainClass” When we “run” the program, the Java Virtual Machine (VM) accesses the main class we defined Then the VM sends a message to our program’s main class that tells it to run. • The “run” message is sent by the VM as a call to a method named main that our program’s main class must have defined. SE-1011 Dr. Mark L. Hornick

  14. A Java program is composed of one or more classes • One of the classes in the program must be the main class • That is, it must have a method called main() • The main class itself can have any (valid) name • All of the Java instructions for a class and a class’s methods must reside in a file having the same name as the class • DayCalculator.java must be the name of the file containing all the Java instructions for a class named DayCalculator. SE-1011 Dr. Mark L. Hornick

  15. Java Edit-Compile-Run Cycle • Step One: Create a program with an editor. • This is where you type in the Java instructions, using a text editor (or something like Eclipse), and save the program to a file. • The name of the class has to match the name of the file containing the class and have the .java file extension. • This is called a source file. SE-1011 Dr. Mark L. Hornick

  16. Java Edit-Compile-Run Cycle • Step 2: Compile the source file. • The process of compiling the source file creates a bytecodefile, which is not human-readable. • The name of the compiler-generated bytecode file will have the suffix .class while its prefix is the same as the source file’s. • DayCalculator.java is compiled, creating DayCalculator.class, the bytecode (or class) file SE-1011 Dr. Mark L. Hornick

  17. Java Edit-Compile-Run Cycle • Source file vs. its bytecode file: SE-1011 Dr. Mark L. Hornick

  18. Java Edit-Compile-Run Cycle • Step 3: Execute the bytecode file. • The java interpreter (VM) will go through the bytecode file and execute the instructions in it. • If an error occurs while running the program, the interpreter will catch it and stop its execution. • The VM starts execution at the bytecode instructions that correspond to the Java statementpublic static void main() SE-1011 Dr. Mark L. Hornick

  19. Java Edit-Compile-Run Cycle • The result after the interpreter executes the instructions in the bytecode file. SE-1011 Dr. Mark L. Hornick

  20. Besides Java instructions, programs also contain comments which state the purpose of the program, explain the meaning of code, and provide other descriptions to help others to understand your code. • Comments are not compiled or executed • Comments are just text you make up • Comments augment the program by providing more understandable, human-friendly information that help readers of your program understand what you have written. SE-1011 Dr. Mark L. Hornick

  21. A program template to use as the starting point for all Java applications SE-1011 Dr. Mark L. Hornick

  22. In Java, classes are further grouped into logical packages • Packages provide a further means of organizing related classes • You create package names yourself • Package names start with lowercase • Classes in the same package reside in the same file folder, where the folder is the package name SE-1011 Dr. Mark L. Hornick

  23. Quiz 1 tomorrow at start of Lab Learning outcomes since the start of the course: • Define the term program. • Define the term algorithm. • Why do we use pseudocode? • Why do we use flowcharts? • How does a Java pseudocode variable differ from a variable you use in Math? • List some possible pseudocode variables. • Given psuedocode, draw the corresponding flowchart. • Given a flowchart, write the corresponding pseudocode. • What two types of control-flow does a diamond represent in a flowchart? SE-1011 Dr. Mark L. Hornick

More Related