1 / 63

Review Java

Review Java. Objectives. To understand the essentials of object-oriented programming in Java To review the primitive data types of Java, how to use the control structures of Java

skyler
Download Presentation

Review Java

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. Review Java

  2. Objectives • To understand the essentials of object-oriented programming in Java • To review the primitive data types of Java, how to use the control structures of Java • To learn how to use predefined classes such as Math, JOptionPane, String, StringBuffer, and StringTokenizer • To review how to write and document your own Java classes Appendix A: Introduction to Java

  3. Chapter Objectives (continued) • To understand how to use arrays in Java • To learn how to perform I/O in Java using simple dialog windows • To learn how to perform I/O in Java using streams Appendix A: Introduction to Java

  4. Compiling and Executing a Java Program Appendix A: Introduction to Java

  5. Classes and Objects • The class is the fundamental programming unit • Every program is written as a collection of classes • Class definitions are stored in separate files with the extension .java and the file name must be the same as the class name • A class is a named description for a group of entities • A class is a general description of a group of entities that all have the same characteristics; each entity is an object Appendix A: Introduction to Java

  6. The Java API • Java consists of small core language augmented by an extensive collection of packages • Each package contains a collection of related Java classes, such as: • Swing • AWT • util Appendix A: Introduction to Java

  7. Import Statements Class Comment Class Name class { Data Members Methods (incl. Constructor) } Review of Java fundamentals Template for Class Definition

  8. Primitive Data Types and Reference Variables • Java distinguishes two kinds of entities • Primitive types: data is stored in primitive type variables • Objects: are associated with reference variables which store an object’s address Appendix A: Introduction to Java

  9. Primitive Data Types • Represent numbers, characters, and Boolean values • Integers: byte, short, int, and long • Real numbers: float and double • Characters: char Appendix A: Introduction to Java

  10. 9 For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 70 ASCII Encoding

  11. Variable declaration Format: <data type> <variable_name>; If more than one variable has the same data type: <data type> <name1>, <name2>..;

  12. Assignment statement <variable name> = <expression>; Example: x =2*5+6-1;

  13. Variable names • It must be a legal identifier. • It must not be a keyword, a boolean literal (true or false), or the reserved word null. • It must be unique within its scope.

  14. Variable name (cont.) • Legal identifier:be composed of letters, numbers, _ and $. Identifiers may only begin with a letter, _, or $. • Keyword: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html • Java styles: • Variablenames begin with a lowercase letter • Class names begin with an uppercase letter

  15. Review question Which of the following is not a valid Java identifier? a. myValue b. $_AAA1 c. width d. m_x Appendix A: Introduction to Java

  16. Review question Which of the following is not a valid Java identifier? a. myValue b. $_AAA1 c. width d. m_x Appendix A: Introduction to Java

  17. Review question Which of the following is a correct variable declaration statement? a. int x - float y; b. int x: float y; c. intx,y; d. Long int x; Appendix A: Introduction to Java

  18. Review question Which of the following is a correct variable declaration statement? a. int x - float y; b. int x: float y; c. intx,y; d. Long int x; Appendix A: Introduction to Java

  19. Constant and variables • Constant: • Value it contains doesn’t change final int MONTHS_IN_YEAR = 12; • Variables: • Value it contains may vary double loanAmount; • loanAmount =0; • loanAmount = 1000.99;

  20. Integer division and type casting • Integer division: • Integer/integer = integer, 7/2 = 3 • Integer/double = double, 7/2.0 = 3.5 • Double/integer = double, 7.0/2 = 3.5 • Type casting: a process that converts a value of one data type to another data type.Implicit casting Explicit casting

  21. Type casting (cont.) • Implicit casting: • Operand is converted from a lower to a higher precision • Higher precision: a data type with a larger range of values • Double has a higher precision than float • Int has a higher precision than short • Operand: can be a constant, variable, method call or another arithmetic expression

  22. Type casting (cont.) • Explicit casting • (<data type>) <expression> • Example: float result; result = (float) ((3+5)/6); and result = ((float) (5+3))/6;

  23. Type Compatibility and Conversion • Widening conversion: operations involving mixed-type operands, the numeric type of the smaller range is converted to the numeric type of the larger range • In an assignment operation, a numeric type of smaller range can be assigned to a numeric type of larger range Appendix A: Introduction to Java

  24. Control Statements: Simple Choice Statement if (<boolean expression>) <block>; else <block>; if (<boolean expression>) single statement; else single statement;

  25. Boolean expression • Boolean expression: is a conditional expression that is evaluated to either true or false. • Conditional expression: is a three part expression: <exp.> <relational operators> <exp.> • Boolean expressions can be combined by boolean operators

  26. Relational Operators

  27. Boolean operators && means AND || means OR ! means NOT

  28. Pre-review question • How many times the method readData() will be called in the following code segnment? inti;i = 0; while ( i <= 4 ) { readData(); i = i + 1; } // end while 5 Appendix A: Introduction to Java

  29. Pre-review question . can be used to traverse a two-dimensional array. a. A do while statement. b. A for statement. c. Two nested for statements. d. Three nested for statements. Appendix A: Introduction to Java

  30. Pre-review question . can be used to traverse a two-dimensional array. a. A do while statement. b. A for statement. c. Two nested for statements. d. Three nested for statements. Appendix A: Introduction to Java

  31. The While Loop while(<boolean expression>){ // Repeat multiple statements. statement 1 statement 2 statement 3 ... }

  32. The Do-While Loop do { // Repeat multiple statements. statement 1 statement 2 statement 3 ... } while(<boolean expression); • Note that the statements in the body of the loop are always executed at least one. • Note the final semicolon, which is required.

  33. The For-Loop Outline // Repeat multiple statements. for(initialization; condition; post-body update){ // Statements to be repeated. statement 1 statement 2 statement 3 ... } • Commonly used with increment • and decrement operators.

  34. Increment and Decrement • Used as a shorthand for add-one-to and subtract-one-from: • value = value+1; • value += 1; • value++; • Prefix and postfix forms: • ++value; • --value; • value--;

  35. <modifiers> <data type> <name> ; Attributes (Data Member) Declaration Modifiers Data Type Name • private String ownerName ; Note: There’s only one modifier in this example.

  36. <modifier> <return type> <method name> ( <parameters> ){ • <statements> • } Method Declaration Modifier Return Type Method Name Parameter • public void setOwnerName ( String name ){ • ownerName = name; • } Statements

  37. public <class name> ( <parameters> ){ <statements> } Constructor • A constructor is a special method that is executed when a new instance of the class is created. Modifier Class Name Parameter • public Bicycle ( ) { • ownerName = “Unassigned”; • } Statements

  38. argument Arguments and Parameters • An argument is a value we pass to a method. • A parameter is a placeholder in the called method to hold the value of the passed argument. class Sample { public static void main(String[] arg) { Account acct = new Account(); . . . acct.add(400); . . . } . . . } class Account { . . . public void add(double amt) { balance = balance + amt; } . . . }

  39. Arguments and Parameters • An argument is a value we pass to a method. • A parameter is a placeholder in the called method to hold the value of the passed argument. class Sample { public static void main(String[] arg) { Account acct = new Account(); . . . acct.add(400); . . . } . . . } class Account { . . . public void add(double amt) { balance = balance + amt; } . . . } parameter

  40. Referencing and Creating Objects • You can declare reference variables that reference objects of specified types • Two reference variables can reference the same object • The new operator creates an instance of a class • A constructor executes when a new object is created Appendix A: Introduction to Java

  41. Methods • Programmers use methods to define a group of statements that perform a particular operation • The modifier static indicates a static or class method • A method that is not static is an instance method • All method arguments are call-by-value • If the argument is a primitive type, its value is passed to the method • The method can’t modify the argument value and have the modification remain after return from the method Appendix A: Introduction to Java

  42. Methods (continued) • If the argument is of a class type, the value of the reference variable is passed, not the value of the object itself • Reference variables point to the object and any modification to the object will remain after return from the method Appendix A: Introduction to Java

  43. Review question Consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue( x, y ); Which of the following statements is false? a. A method is called with its name and arguments inside parentheses. b. x and y are parameters. c. Copies of x and y are passed to the method calculateValue(). d. x and y are arguments Appendix A: Introduction to Java

  44. Review question Consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue( x, y ); Which of the following statements is false? a. A method is called with its name and arguments inside parentheses. b. x and y are parameters. c. Copies of x and y are passed to the method calculateValue(). d. x and y are arguments Appendix A: Introduction to Java

  45. The Class Math • Provides a collection of methods that are useful for performing common mathematical operations Appendix A: Introduction to Java

  46. Escape Sequences • An escape sequence is a sequence of two characters beginning with the character \ • Represents characters or symbols that have a special meaning in Java Appendix A: Introduction to Java

  47. The String Class • String class defines a data type that is used to store a sequence of characters • You cannot modify a String object • If you attempt to do so, Java will create a new object that contains the modified character sequence Appendix A: Introduction to Java

  48. Comparing Objects • You can’t use the relational operators or equality operators to compare the values stored in strings or other objects Appendix A: Introduction to Java

  49. Examples We can do this because String objects are immutable.

  50. The StringBuffer Class • Stores character sequences • Unlike a String object, the contents of a StringBuffer object can be changed

More Related