230 likes | 365 Views
Object Oriented Systems Lecture 01 First Java Programming Jaeki Song. Objectives. Learn about programming Be introduced to object-oriented programming concepts Learn about Java Analyze a Java application that uses console output. What is a program?.
E N D
Object Oriented Systems Lecture 01 First Java Programming Jaeki Song
Objectives • Learn about programming • Be introduced to object-oriented programming concepts • Learn about Java • Analyze a Java application that uses console output
What is a program? • A computer program is a set of instructions that enable the computer to solve a problem or perform a task • Display a message on the screen: “How many hours did you work?” • A computer’s CPU can only process instructions that are written in machine language • A stream of binary numbers
What is a Program made of? • Language elements • Keywords (reserved words) • There are words that have a special meaning in the programming language • E.g.: public, class, static, etc • Operators • Symbols or words that perform operation son one or more operands • E.g.: “=“, “*”, etc • Punctuation • Most programming languages require the use of punctuation characters such as the beginning or ending of a statement • E.g.: semicolon in Java – similar to a period in English
What is a Program made of? • Programmer-defined names • Unlike key words, these are words or names that are defined by the programmer • E.g.: names of variables • Syntax • Rules that must be followed when writing a program
Programming Approaches • Procedural programming • Divide a problem into smaller sub-problems • Each sub-problem is analyzed and a solution for the sub-problem is obtained • Also known as top-down design, stepwise refinement and modular programming • Object-oriented programming • Object-oriented Design (OOD) • Identify components called object • Specify the relevant data for each object and possible operations to be performed on that data • In OOD, the final program is a collection of interacting objects • A programming language that implements OOD is called an OOP language
Introduction to Object Concepts • Object-oriented programming differs from traditional procedural programming • Basic concepts • Objects • Classes • Inheritance • Polymorphism
What is Java? • Designed in the early of 1990s by Sun Microsystems • Code name “Green” • Used in consumer devices such as an intelligent television “set-up” boxes • The language was designed to be simple and architecture-neutral, so that it could be executed on a variety of hardware • Rewrite the program: architecture-neutral, real-time, reliable, and secure • Applets, now called Java provides animation and interactivity on the World Wide Web • Web browsers have provided the opportunities to run Java applets • The fastest growing language
Java Language • Standard language used for programming, creating applets, servlets, JavaBeans, and enterprise components • Java is simple • Java is object-oriented language • Java is distributed • Java is portable • Java is multithreaded
Java Environment Source code is stored on a disk In a file with a name ending in .java Java source code Compiler creates byte codes that are stored on a disk a file with a name ending in .class Java compiler Java virtual machine Java interpreter JVM (named java.exe) performs security chekcs and translates byte codes to machine language, which executes Computer OS
Java Program Types • Applets • Programs embedded in Web page • Java applications • Called Java stand-alone programs • Console applications • Support character output • Windowed applications • Menus • Toolbars • Dialog boxes
First Java Application • Eclipse • Tutorial public class First { public static void main (String [ ] args) { System.out.println (“First Java Application”) } }
Understanding First Class • Literal string • Will appear in output exactly as entered • Written between double quotation marks • Arguments • Pieces of information passed to method • Method • Requires information to perform its task • Define Java class using any name or identifier • Requirements for identifiers • Must begin with: • Letter of English alphabet • Or non-English letter (such as α or π) • Cannot begin with digit
Understanding First Class (continued) • Requirements for identifiers • Can only contain: • Letters • Digits • Underscores • Dollar signs • Cannot be Java reserved keyword (p. 10) • Cannot be true, false, or null • Access modifier • Defines how class can be accessed
Java Naming Conventions • Packages • The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names • Use dots to separate the parts • E.g.: com.sun.eng, com.objectcentral.javatools • Classes • Class (and interface) names should be nouns descriptive of the purpose of the class • Names are in mixed case, beginning with a capital and with the first letter of each internal word capitalized • Use complete words and avoid abbreviations • E.g.: Point, Shape, MovieEditor, ClientList
Java Naming Conventions (continued) • Methods • Methods should be verbs descriptive of the purpose of the method • Names are mixed case with the first letter lowercase and the first letter of each internal word capitalized • There are prefix conventions for general types of methods, such as using get and set for getters and setters • E.g.: getOrigin, findSmallest, drawGraph, saveMoney • Variables • Except when used as constants, all variables are named using mixed case with a lowercase first letter, and with intenral words starting with capital letters • Use one-letter variable names only for temporary variables • E.g.: myMovie, editedMovie, backgroundColor • Constants • Names should be all uppercase with words seperated by underscores (“_”) • e.g.: MAX_SIZE, TERM_LIMIT
main( ) Method • Static • Reserved keyword • Means method accessible and usable • Even though no objects of class exist • void • use in main() method header • Does not indicate main() method empty • Indicates main() method does not return value when called • Doesn’t mean main() doesn’t produce output
Adding Comments to a Java Class • Types of Java comments • Line comments • Start with two forward slashes (//) • Continue to end of current line • Do not require ending symbol • Block comments • Start with forward slash and asterisk (/*) • End with asterisk and forward slash (*/)
Programming Errors • Syntax error • Result from errors in cod construction • E.g.: mistyping, omitting some necessary punctuation, using an opening brace without a corresponding closing brace • Logical error • Occur when a program does not perform the way it was intended to • Run-time error • Cause a program to terminate abnormally • E.g. • Input error: the user enters an unexpected input value that the program cannot handle • Division by zero
Using Java Swing Class • Refers to the new library of GUI • A component set that makes up all the objects of GUI • Displays output using windows or dialog boxes • Input Dialog and Output Dialog • Use packages • Predefined classes grouped into categories of related classes called packages (sometimes called java class libraries or java applications programming interface (API)) • JOptionPane • Defined in a package called javax.swing
GUI Output • JOptionPane • Produce dialog boxes • Dialog box • GUI object resembling window • Messages placed for display • Package • Group of classes • import statement • Use to access built-in Java class
Output Dialog • showMessageDialog ( null, “string”); • A method of class JOptionPane • Two arguments • Syntax JOptionPane.showMessageDialog(null, “string”);
Common Errors to Avoid • Mismatched braces, quotation marks, or parentheses • Misspelling key words • Using capital letters in key words • Java is a case-sensitive • All key words are written in lower case in Java • Using a key word as a variable name • Using inconsistent spelling • Forgetting the semicolon at the end of a statement • Not using the required import statement