390 likes | 938 Views
Some Programming Languages Procedural - procedures produce a result from a set of inputs Object-Oriented - manipulates objects, provides graphical user interfaces ( GUIs) Java -1991- James Gosling (Sun Microsystems) Applications - stand alone programs
E N D
Some Programming Languages Procedural - procedures produce a result from a set of inputs Object-Oriented - manipulates objects, provides graphical user interfaces ( GUIs) Java -1991- James Gosling (Sun Microsystems) Applications - stand alone programs Applets - small program embedded in a web page Introduction to Java
Figure 1.5:Flowchart for Calculating the Average of Three Numbers
Figure 1.8: The Traditional Translation Process Program Translation
Figure 1.9:Compiling and Executing a Java Program Written by programmer
Modular programs are easier to design, understand, debug and modify Modules are small segments or Java classes Classes are units that contain data structures and methods A Method contains a sequence of instructions (and in Java is constructed inside a class). Constructing a Java Program
Give it a meaningful name or identifier or mnemonic. Java identifiers can be letters, digits, underscores(_), or $, but the first character must be a letter. A identifier cannot be a reserved (key) word. Method names begin with a lowercase letter. Class names always begin with a capital letter. Designing Methods
Every Java program must have at least one class Class header contains: key word public or private key word class name of the class Body of the class is enclosed in { } JAVA Classes
Header public static void main(String [ ] args) { “ body” } public - specifies scope static - specifies how the method is created and stored void - will not return any data arguments - type of data sent into the method Main Method
These methods print data or arguments given to them on the screen or standard output device System.out is the standard output object System.out.println(“Hello world!”); print( ) - no new line println( ) - new line \n - escape sequence for new line The print() and println() methods
A VERY Simple Java Program // This program prints a message to the user public class Hello { public static void main(String[ ] args) { System.out.print(“ Hello World!”); } //end of main method } //end of class
Syntax - set of rules for grammatically correct statements Style- indentation, separate lines for different statements, comments, etc. Line comments //This is a comment Block comments /* This comment extends over several lines and gives the purpose of a method or program. */ Programming Syntax and Style
Dialog Boxes • Java provides graphical components in the Swing package, known as javax.swing • Dialog boxes present a user with information and require a response ( such as clicking OK) • Modal box- user must respond and close it before continuing • Modeless- can continue without closing • JOptionPane.showMessageDialog(null, message, title, icon-type); • ( notice capitalization) • JOptionPane.showMessageDialog(null, “Hello World”, SAMPLE, JOptionPane.WARNING_MESSAGE);
Figure 1.16a: showMessageDialog() Dialog Box:WARNING_MESSAGE
Figure 1.16b:showMessageDialog() Dialog Box:QUESTION_MESSAGE
Figure 1.16c:showMessageDialog() Dialog Box:INFORMATION_MESSAGE
An Interactive Java Program import javax.swing.*; // used for graphics public class LuckyOne { public static void main(String[ ] args) { String s1, outMessage; double firstnum, secnum; s1 = JOptionPane.showInputDialog("Enter a number:"); firstnum = Double.parseDouble(s1); System.out.println(“ That’s my Lucky Number! \n”); } //end main System.exit(0); // closes program } //end class LuckyOne
Must save program with same file name as class name Omitting braces and semicolons Misspelling Forgetting to close braces, parenthesis, quotes Common Programming Errors