1 / 81

INF120 Basics in JAVA Programming AUBG, COS dept

INF120 Basics in JAVA Programming AUBG, COS dept. Lecture 03 Title: Building Java programs using Command Line Window Reference: 1. Lecture Contents:. Anatomy of a Java program (reminder) More on elementary Java Input/Output dialogs Programming Style and Documentation

aden
Download Presentation

INF120 Basics in JAVA Programming AUBG, COS dept

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. INF120 Basics in JAVA Programming AUBG, COS dept Lecture 03 Title: Building Java programs using Command Line Window Reference: 1

  2. 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

  3. 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!"); } }

  4. Two More Simple Examples WelcomeWithThreeMessages Run ComputeExpression Run

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

  6. 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!"); } }

  7. 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!"); } }

  8. 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!"); } }

  9. 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!"); } }

  10. 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!"); } }

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

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

  13. Special Symbols

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

  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. Programming Style and Documentation • Appropriate Comments • Naming Conventions • Proper Indentation and Spacing Lines • Block Styles

  20. 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.

  21. 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. • 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. E.g. the constant PI, and MAX_VALUE

  22. Proper Indentation, Spacing, Block Styles • Indentation • Indent two spaces. • Spacing • Use blank line to separate segments of the code. • Use end-of-line style for braces.

  23. . More on Elementary Java

  24. 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

  25. 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

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

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

  28. 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”.

  29. 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”.

  30. 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

  31. 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

  32. 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.”

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

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

  35. 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.

  36. Executing a Java program

  37. JVM emulation run on a physical machine

  38. JVM handles translations

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

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

  41. 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.

  42. 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

  43. 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).

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

  45. JDK contents • appletviewer – this tool can be used to run and debug Java applets without a web browser • apt – the annotation-processing tool4 • extcheck – a utility which can detect JAR-file conflicts • idlj – the IDL-to-Java compiler. This utility generates Java bindings from a given Java IDL file. • java – the loader for Java applications. This tool is an interpreter and can interpret the class files generated by the javac compiler. Now a single launcher is used for both development and deployment. The old deployment launcher, jre, no longer comes with Sun JDK, and instead it has been replaced by this new java loader. • javac – the Java compiler, which converts source code into Java bytecode • javadoc – the documentation generator, which automatically generates documentation from source code comments • jar – the archiver, which packages related class libraries into a single JAR file. This tool also helps manage JAR files.

  46. JDK contents (cont.) • javah – the C header and stub generator, used to write native methods • javap – the class file disassembler • javaws – the Java Web Start launcher for JNLP applications • JConsole – Java Monitoring and Management Console • jdb – the debugger • jhat – Java Heap Analysis Tool (experimental) • jinfo – This utility gets configuration information from a running Java process or crash dump. (experimental) • jmap – This utility outputs the memory map for Java and can print shared object memory maps or heap memory details of a given process or core dump. (experimental) • jps – Java Virtual Machine Process Status Tool lists the instrumented HotSpot Java Virtual Machines (JVMs) on the target system. (experimental) • jrunscript – Java command-line scriptshell

  47. JDK contents (cont.) • jstack – utility which prints Java stack traces of Java threads (experimental) • jstat – Java Virtual Machine statistics monitoring tool (experimental) • jstatd – jstat daemon (experimental) • keytool – tool for manipulating the keystore • pack200 – JAR compression tool • policytool – the policy creation and management tool, which can determine policy for a Java runtime, specifying which permissions are available for code from various sources • VisualVM – visual tool integrating several command-line JDK tools and lightweight[] performance and memory profiling capabilities • wsimport – generates portable JAX-WS artifacts for invoking a web service. • xjc – Part of the Java API for XML Binding (JAXB) API. It accepts an XML schema and generates Java classes

  48. Creating, Compiling, and Running Programs

  49. Creating, Compiling & Running Java How is the required processing realized? • From the Command Line Window • From within IDEs

More Related