Download
introduction to java n.
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to Java PowerPoint Presentation
Download Presentation
Introduction to Java

Introduction to Java

111 Views Download Presentation
Download Presentation

Introduction to Java

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Introduction to Java

  2. A Brief History of OOP and Java • Early high level languages • _________________________ • More recent languages • BASIC, Pascal, C, Ada, Smalltalk, C++, Java • ______________ operating system upgrades prompted the development of the C language • Powerful language … but not the best intro to programming • Difficult for ________________________ • Does not fit modern programming strategies

  3. A Brief History of OOP and Java • Simula-67 was a language which facilitated the modeling of real-world objects • New language feature called the “_________” • A class was ______________ through “inheritance” • This was the foundation needed for Object Oriented Programming, OOP • Smalltalk-80 used this concept • This is the first truly object-oriented language

  4. A Brief History of OOP and Java • The C language was extended to include “classes” and in 1983 became _________ • The Java language originally conceived to control __________________________ using the OOP concept • Thus, was meant to be platform (or ____________) independent • Soon this was seen as well suited for running small programs (_____________) on Internet web pages • By 1995, Netscape browsers began supporting Java applets • This did much to establish Java as a major language

  5. A Brief History of OOP and Java • Note typical implementation of a program on different platforms Executable code for UNIX platform Compiler for UNIX platform Source program Executable code for Windows platform Compiler for Windows Platform Compiler for Mac OS Platform We need ______________________________ Executable code for Mac OS platform

  6. A Brief History of OOP and Java • Contrast compiler with interpreter • Compiler runs ________________, translates to machine code for commands of whole program • _______________ translates one command at a time • Java combines these two • Java compiler generates intermediate “___________” • An ____________________ is developed for each platform to interpret the bytecode • Interpreter called the Java Virtual Machine or JVM

  7. Phases of Java Programs • Edit the source code • filename._____________ • Compile the source code into bytecodes • filename.__________ • Load the .class file into memory • applications: stored and executed from user's own computer • ________________: loaded from a computer somewhere into a browser, executed by the browser

  8. Phases of Java Programs • Verify bytecodes • check for ___________ and potential security restrictions • Computer interprets bytecodes • ___________ bytecode at a time

  9. Disk Disk Interpreter Compiler Editor Class Loader . . . . . . . . . . . . . . . . . . Program is created in the editor and stored on disk. Phase 1 Compiler creates bytecodes and stores them on disk. Phase 2 Primary Memory Phase 3 Class loader puts bytecodes in memory. Disk Primary Memory Bytecode Verifier Phase 4 Bytecode verifier confirms that all bytecodes are valid and do not violate Java’s security restrictions. Primary Memory Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes. Phase 5

  10. Programming with "Ready to Program" • Find the "Ready" option on the program menu or click on the"_________" icon

  11. Programming with "Ready to Program" • An empty ___________ window appears

  12. A Java class with a method called main() is a Java __________. Java Programs • A Java program consists of one or more classes • Classes consist of a collection of The name of the file and the name of the ________ must, have the same name

  13. Creating a Java Application • The "Ready" environment will give you skeleton or boilerplate format for programs • Click on File, New, and HSA Console option • A dialog box asksfor the ________ of the class

  14. Bold Face Java _______ Black Identifiers in Java _________ library ____________Comments Blue___________ in your program Creating a Java Application • The appropriate boilerplate text appears in the edit window – note the color coding RedQuoted Strings

  15. Note that a dialog box will ask you for the name • Be sure to give it exactly the __________________ Creating a Java Application • Fill in the necessary commands • Save theprogram

  16. Creating a Java Application • To run a Java program • Press the button or • Press Ctrl+R or • Press F1 • The consoleprogram showsa ___________window

  17. Creating a Java Application • Make sure to ________________ the program before quitting • The "Ready" environment will remind you • To exit the "Ready" environment • Click the X close or • Choose File, Exit or • Use Ctrl-Q

  18. Dialog box shows how many errors _______________ of error given in the status bar _________ line(s) highlighted Creating a Java Application • Errors in the program • Syntax errors are found for you by the compiler

  19. Introduction to Java Application Programs • Java is an ____________ oriented programming language • Uses objects to carry out the tasks • Sends to the objects to perform the tasks • Objects interact with each other to do the tasks • An actual object is called an ____________of a class • The class is the declaration of or blueprint for the object

  20. Introduction to Java Application Programs • Object oriented programs: • A collection of object interactions that solve a problem • Note the similarity of this definition to the definition for a program

  21. Comments • Begins class definition for class Welcome1 • Every Java program has at least one user-defined class Java program // Welcome1.java// A first program in Java import hsa.*; public class Welcome1 { public static void main (String args[]) { Stdout.println("Welcome to Java Programming"); } // end method main } // end class Welcome1 Program Output

  22. Java program • Saving files • File name is class nameand .java extension • Welcome1.java // Welcome1.java// A first program in Java import hsa.*; public class Welcome1 { public static void main (String args[]) { Stdout.println("Welcome to Java Programming"); } // end method main } // end class Welcome1 • Parenthesis indicate main is a _______________ • Java applications contain one or more methods • Part of every Java application • Applications begin executing at main

  23. Class Declaration • Syntax:class ClassName extends Object{ Declarations of class members} • Note the extends clause • specifies that the ClassName _________________ attributes and behaviors of Object • also it will ___________ new attributes and behaviors • ClassName is the subclass or derived class • _________________ is the superclass or base class

  24. Class Members • Specified between outermost set of curly braces { … } • Members can be • ___________________ that store values • methods which perform ______________ on the variables • The main method’s declarationpublic static void main (String [ ] args){ a list of Java statements} • First statement of the program executed is first of these statements • Program terminates at last of these statements

  25. Java program public static void main( String args[] ) • Exactly one method must be called main • Methods can __________________ and return information • ____________ means main returns no information • For now, mimic main's first line

  26. Class specification Declarations • Example Actually ________ the Console object variable name for a ________ object

  27. Calling an Object Method • Example Method name Object name Parameters

  28. Java Program Stdout.println( "Welcome to Java Programming!" ); • Instructs computer to perform an action • Prints string of characters • _____________ Stdout • Output object • Print to output window • _____________ Stdout.println • Object calls method to display line of text • Argument inside parenthesis • Statements must end with ___________

  29. Java Program • Java program using dialog box • Note • JOptionPane ___________ • ________ call • Output dialog box • Program Output

  30. A Simple Program: Printing a Line of Text • ___________ statements • Locate the classes we use • Tells compiler to load JOptionPane from __________________package import javax.swing.JOptionPane; // import class JOptionPane

  31. A Simple Program: Printing a Line of Text JOptionPane.showMessageDialog( null, "Welcome\nto\nJava\nProgramming!" ); • Call method showMessageDialog of class JOptionPane • Requires ______ arguments • For now, first argument always null • Second argument is _________ to display • showMessageDialog is a ________ method of class JOptionPane • static methods called using _____________, dot (.) then method name

  32. A Simple Program: Printing a Line of Text JOptionPane.showMessageDialog( null, "Welcome\nto\nJava\nProgramming!" ); • Executing these lines displays the dialog box • Automatically includes an _________ button • Hides or dismisses dialog box • Title bar has string Message

  33. A Simple Program: Printing a Line of Text System.exit( 0 ); // terminate the program • Calls static method ______________ of class System • Terminates application • Use with any application displaying a GUI • Because method is ___________, needs class name and dot (.) • Identifiers starting with capital letters usually class names • Argument of 0 means application ended successfully • Non-zero usually means an error occurred • Class System part of package java.lang • No ___________ statement needed • java.lang automatically imported in every Java program

  34. Sample Program • Program 2.9 from text • Note the features • Use of javax.swing routines • JOptionPane objects used • Input __________ boxes • ___________ message boxes • Conversion of string to _____________

  35. Sample Program • Program 2.9 from text Program Output

  36. Another Java Application: Adding Integers // read in first number from user as a string firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); // read in second number from user as a string secondNumber = JOptionPane.showInputDialog( "Enter second integer" ); // convert numbers from type String to type int number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); • Reads ______________ from the user, representing the numbers to be added • Method JOptionPane.showInputDialog displays • Message called a prompt - directs user to perform an action • Argument appears as _______________ • Then strings are converted to integers.

  37. Another Java Application: Adding Integers // display the results JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); • Use showMessageDialog to display results • "The sum is " + sum • Uses the operator + to ______________ the string literal "The sum is" and sum • Concatenation of a String and _______________ • Results in a new string

  38. Another Java Application: Adding Integers // display the results JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); • Different version of showMessageDialog • Requires ____ arguments (instead of two as before) • First argument: null for now • Second: string to display • Third: string in ___________________________ • Fourth: type of message dialog • JOptionPane.PLAIN_MESSAGE - no icon • JOptionPane.ERROR_MESSAGE • JOptionPane.INFORMATION_MESSAGE • JOptionPane.WARNING_MESSAGE • JOptionPane.QUESTION_MESSAGE

  39. Values Held by Variables • Primitive-type variables • store a __________ of the specified type (int, double) • Reference-type variables • store an _____________ of memory location where value is stored • thought of as a _____________ for the object that actually stores the values

  40. Variable Declaration Syntax • Syntax:type variable_name;ortype variable_name = expression; • Note • type must be known to the compiler • variable_name must be a _______________ • expression is evaluated and assigned to variable_name location • In the first form, a ___________________________ (0, false, or null, depending on type)

  41. Constants • Value of object _____________ • for oft used math values such as PI • for values which will not change for a given program • improve readability of program • facilitate program maintenance • Declaration syntax:final type CONSTANT_NAME =expression; • ____________ is a Java keyword, makes a constant • type must be known by compiler • CONSTANT_NAME must be valid identifier • expression evaluated • should be placed at ___________ of class or method

  42. Primitive Types • Also known as "__________ types" • int, byte, short, and long for integer values • float and double for real/fractional values • char for letters, digits, symbols, punctuation • boolean for true and false • Literals: • values of one of these types • 'A', -7, 3.5, true

  43. Reference Types • Needed to represent windows, buttons, inventory, students, etc. • objects ________________________ than can be represented with simple types • Create a class and you have created a new type whose name is the name of the class • Types __________________ are called reference types

  44. Java Provided Reference Types • Over _______ classes already available in Java • Examples: • String – for constant sequences of characters • StringBuffer – for variable sequences of characters • BigInteger – for integers of "unlimited" size • BigDecimal – for fractional numbers of "unlimited" size

  45. Creating Reference Type Values:Constructors • Primitive types use literals for their values • Meanings of literals built into compiler • Reference types have no ___________________________________ • Values must be created with the _____ operator • Example:Integer integerValue = new Integer(321);

  46. Default Value: null • Default value for reference typesScreen theScreen = null; //orScreen theScreen; • value used if none is specified in declaration • indicates variable does _________________ to a value of that type • can later be assigned values created with ______________

  47. Constructing an Object • Use of new to create a reference type • Class provides one or more methods • called _______________ • Syntax: new ClassName (arguments, …) • ClassName is name of a _____________ type • arguments is a sequence of values separated by commas • types of arguments match those permitted by ClassName

  48. Ready to Program I/O Classes • Stdin • A ___________________ class • Read all Java ___________ data types from standard input • Possible to read several primitives from one line • Stdout • This is a non-instantiated class • Output ___________ data to standard output • Write data to fixed length fields and with a fixed number of decimal places.

  49. Stdin • static byte readByte () • static short readShort () • static int readInt () • static long readLong () • static float readFloat () • static double readDouble () • static boolean readBoolean () • static char readChar () • static String readString () • static String readLine () • Returns the entire line of input read from the keyboard without the Return.

  50. static void print (byte b) static void print (short s) static void print (int i) static void print (long l) static void print (float f) static void print (double d) static void print (boolean b) static void print (char c) static void print (String s) static void println (byte b) static void println (short s) static void println (int i) static void println (long l) static void println (float f) static void println (double d) static void println (boolean b) static void println (char c) static void println (String s) Stdout