1 / 68

INF120 Basics in JAVA Programming AUBG, COS dept, Fall semester 2013

INF120 Basics in JAVA Programming AUBG, COS dept, Fall semester 2013 Reference books: Malik D.S. , Java Programming, From Problem Analysis to Program Design, Cengage Learning, 4e 2010 Farrell J. Java Programming, Cengage Learning, 5e 2010

beth
Download Presentation

INF120 Basics in JAVA Programming AUBG, COS dept, Fall semester 2013

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. INF120Basics in JAVA ProgrammingAUBG, COS dept, Fall semester 2013Reference books:Malik D.S., Java Programming, From Problem Analysis to Program Design, Cengage Learning, 4e 2010 Farrell J. Java Programming, Cengage Learning, 5e 2010 Y.Daniel Liang, Introduction to JAVA Programming, Brief version, Pearson IE, Prentice Hall, 9e 2013Any Java book available in AUBG libraryCourse lecturer: Assoc. Prof. Stoyan Bonev, PhD

  2. INF120 Basics in JAVA Programming AUBG, COS dept, Fall semester 2013 Lecture 03 Title: Building Java programs using Command Window Reference: 1

  3. Lecture Contents: • Anatomy of a Java program (reminder) • More on elementary Java • Input/Output dialogs • Programming Style and Documentation • Building Java programs using DOS command lines

  4. A Simple Java Program Listing 1.1 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  5. Two More Simple Examples WelcomeWithThreeMessages Run ComputeExpression Run

  6. Anatomy of a Java Program • Class name • Main() method • Statements • Statement terminator • Reserved words • Comments • Blocks

  7. Class Name Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  8. Main Method Line 2 defines the main method. In order to run a class, the class must contain a method named main. The program is executed from the main() method. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  9. Statement A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!"); in the program below is a statement to display the greeting "Welcome to Java!“. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  10. Statement Terminator Every statement in Java ends with a semicolon (;). //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  11. Reserved words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  12. Blocks A pair of braces in a program forms a block that groups components of a program.

  13. Don’t confuse names parentheses, curly braces, square brackets, and angle brackets Java Programming, Fifth Edition

  14. Special Symbols

  15. { … } // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  16. ( … ) // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  17. ; // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  18. // … // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  19. " … " // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

  20. Programming Style and Documentation • Appropriate Comments • Naming Conventions • Proper Indentation and Spacing Lines • Block Styles

  21. Appropriate Comments Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program.

  22. Naming Conventions • Choose meaningful and descriptive names. • Variables and method names: • Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea.

  23. Naming Conventions, cont. • Class names: • Capitalize the first letter of each word in the name. For example, the class name ComputeArea, and the class name ComputeExpression. • Constants: • Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI , and MAX_VALUE

  24. Proper Indentation and Spacing • Indentation • Indent two spaces. • Spacing • Use blank line to separate segments of the code.

  25. Block Styles Use end-of-line style for braces.

  26. . More on Elementary Java

  27. JOptionPane - Input For now Two ways of obtaining input are to be discussed. • Using the Scanner class (console input) • And methods like nextInt(), nextDouble(), hasNext(), … • Using JOptionPane input dialogs • And method showInputDialog() – to introduce now

  28. Problem: Computing Loan Payments This program lets the user enter the interest rate, number of years, and loan amount, and computes monthly payment and total payment. ComputeLoan Run

  29. Getting Input from Input Dialog Boxes String inp; inp=JOptionPane.showInputDialog("Enter an input"); // OR String inp = JOptionPane.showInputDialog( "Enter an input");

  30. Getting Input from Input Dialog Boxes String string = JOptionPane.showInputDialog( null, “Prompting Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE);

  31. Two Ways to Invoke the Method There are several ways to use the showInputDialog() method. For the time being, you only need to know two ways to invoke it. One is to use a statement as shown in the example: String string = JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE); where x is a string for the prompting message, and y is a string for the title of the input dialog box. The other is to use a statement like this: JOptionPane.showInputDialog(x); where x is a string for the prompting message.

  32. Converting Strings to Integers The input returned from the input dialog box is a string. If you type a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number. To convert a string into an int value, you can use the static parseInt() method in the Integer class as follows: int intValue = Integer.parseInt(intString); where intString is a numeric string such as “123”.

  33. Converting Strings to Doubles To convert a string into a double value, you can use the static parseDouble() method in the Double class as follows: double doubleValue =Double.parseDouble(doubleString); where doubleString is a numeric string such as “123.45”.

  34. Problem: Computing Loan Payments Using Input Dialogs Same as the program for computing loan payments, except that the input is entered from the input dialogs and the output is displayed in an output dialog. ComputeLoanUsingInputDialog Run

  35. JOptionPane - Output For now Two ways of sending output are to be discussed. • Using the System class, the out object • And methods System.out.print(), System.out.println() • Using JOptionPane message dialogs • And method showMessageDialog() – to introduce now

  36. Displaying Text in a Message Dialog Box you can use the showMessageDialog( ) method in the JOptionPane class. JOptionPane is one of the many predefined classes in the Java system, which can be reused rather than “reinventing the wheel.”

  37. The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!", "Display Message", JOptionPane.INFORMATION_MESSAGE);

  38. Two Ways to Invoke the Method There are several ways to use the showMessageDialog method. For the time being, all you need to know are two ways to invoke it. One is to use a statement as shown in the example: JOptionPane.showMessageDialog(null, x, y, JOptionPane.INFORMATION_MESSAGE); where x is a string for the text to be displayed, and y is a string for the title of the message dialog box. The other is to use a statement like this: JOptionPane.showMessageDialog(null, x); where x is a string for the text to be displayed.

  39. . Building Java programs Or How Java works? What processing takes place?

  40. Compiling Java Source Code You can port a source program to any machine with appropriate compilers. The source program must be recompiled, however, because the object program can only run on a specific machine. Nowadays computers are networked to work together. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine, as shown below. Java Virtual Machine is a software that interprets Java bytecode.

  41. Executing a Java program

  42. JVM emulation run on a physical machine

  43. JVM handles translations

  44. Java Popularity • Pure Java includes 3 software facilities: • JRE • JVM • JDK

  45. JRE • The Java Runtime Environment (JRE) provides • the libraries, • the Java Virtual Machine, and • other components to run applets and applications written in Java.

  46. JVM - Overview • Java Virtual Machine is a program which executes certain other programs, namely those containing Java bytecode instructions. • JVM is distributed along with Java Class Library, a set of standard class libraries (in Java bytecode) that implement the Java application programming interface (API). These libraries, bundled together with the JVM, form the Java Runtime Environment (JRE). • JVMs are available for many hardware and software platforms. The use of the same bytecode for all JVMs on all platforms allows Java to be described as a write once, run anywhere programming language, versus write once, compile anywhere, which describes cross-platform compiled languages. • Oracle Corporation, the owner of the Javatrademark, produces the most widely used JVM, named HotSpot, that is written in the C++ programming language.

  47. JRE – Execution Environment • Oracle's Java execution environment is termed the Java Runtime Environment, or JRE. • Programs intended to run on a JVM must be compiled into Java bytecode, a standardized portable binary format which typically comes in the form of .class files (Java class files). A program may consist of many classes in different files. For easier distribution of large programs, multiple class files may be packaged together in a .jar file (short for Java archive). • The Java application launcher, java, offers a standard way of executing Java code. • The JVM runtime executes .class or .jar files, emulating the JVM instruction set by interpreting it or using a just-in-time compiler (JIT) such as Oracle's HotSpot. JIT compiling, not interpreting, is used in most JVMs today to achieve greater speed

  48. JVM • JVM architecture. Source code is compiled to Java bytecode, which is verified, interpreted or JIT-compiled for the native architecture. The Java APIs and JVM together make up the Java Runtime Environment (JRE).

  49. JDK contents The JDK has as its primary components a collection of programming tools, including:

More Related