Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1 Introduction 25.2 Primitive Data Types an...
Objectives <ul><li>In this chapter, you will learn: </li></ul><ul><ul><li>To understand primitive types and logical operat...
25.1 Introduction <ul><li>In this chapter </li></ul><ul><ul><li>Differences between C, C++, and Java </li></ul></ul><ul><u...
25.2 Primitive Data Types and Keywords <ul><li>Primitive data types </li></ul><ul><ul><li>char ,   byte ,   short ,   int ...
25.2 Primitive Data Types and Keywords (II)
25.2 Primitive Data Types and Keywords (III) <ul><li>Keywords </li></ul><ul><ul><li>Reserved names, cannot be used as iden...
25.3 Logical Operators <ul><li>Logical operators </li></ul><ul><ul><li>Form complex conditions and control structures </li...
25.3 Logical Operators (II)
25.3 Logical Operators (III) <ul><li>Logical Operators (continued) </li></ul><ul><ul><li>Boolean logical exclusive OR ( ^ ...
25.3 Logical Operators (IV)
25.3 Logical Operators (V) <ul><li>More GUI Classes ( javax.swing ) </li></ul><ul><ul><li>JTextArea </li></ul></ul><ul><ul...
25.3 Logical Operators (VI) <ul><li>More GUI classes </li></ul><ul><ul><li>showMessageDialog(null, myScroller, titleString...
Logical-Operators.java (Part 1 of 2)
Logical-Operators.java (Part 2 of 2)
Program Output
25.4 Method Definitions <ul><li>Method definition format </li></ul><ul><ul><li>return-value-type  method-name (   paramete...
25.4 Method Definitions (II) <ul><li>Program control </li></ul><ul><ul><li>When method call encountered </li></ul></ul><ul...
25.4 Method Definitions (III) <ul><li>Calling methods </li></ul><ul><ul><li>Three ways </li></ul></ul><ul><ul><ul><li>Meth...
25.4 Method Definitions (IV) <ul><li>More GUI components </li></ul><ul><ul><li>Content Pane - on-screen display area </li>...
SquareInt.java (Part 1 of 2)
SquareInt.java (Part 2 of 2) Program Output
25.5 Java API Packages <ul><li>As we have seen </li></ul><ul><ul><li>Java has predefined, grouped classes called packages ...
25.5 Java API Packages (II)
25.5 Java API Packages (III)
25.5 Java API Packages (IV)
25.5 Java API Packages (V)
25.5 Java API Packages (VI)
25.5 Java API Packages (VII)
25.5 Java API Packages (VIII)
25.6 Random Number Generation <ul><li>Math.random()   </li></ul><ul><ul><li>Returns a random  double , greater than or equ...
RandomInt.java Program Output
RollDie.java (Part 1 of 2)
RollDie.java (Part 1 of 2) Program Output
25.7 Example: A Game of Chance <ul><li>Redo &quot;craps&quot; simulator from Chapter 5 </li></ul><ul><li>Rules </li></ul><...
25.7 Example: A Game of Chance (II) <ul><li>User input </li></ul><ul><ul><li>Till now, used message dialog and input dialo...
25.7 Example: A Game of Chance (III) <ul><li>Interface  ActionListener </li></ul><ul><ul><li>Requires that you define meth...
25.7 Example: A Game of Chance (IV) <ul><li>Class  JButton  (continued) </li></ul><ul><ul><li>We registered  this  applet ...
25.7 Example: A Game of Chance (V) <ul><li>Methods of class  Container </li></ul><ul><ul><li>Recall that the Content Pane ...
Craps.java (Pat 1 of 5)
Craps.java (Pat 2 of 5)
Craps.java (Pat 3 of 5)
Craps.java (Pat 4 of 5)
Craps.java (Pat 5 of 5)
Program Output
25.8 Methods of Class  JApplet <ul><li>Methods of Class  JApplet </li></ul><ul><ul><li>init, start, stop, paint, destroy <...
25.8 Methods of Class  JApplet   (II) First line of  JApplet  methods (descriptions Fig. 25.14) public void init()   publi...
25.9 Defining and Allocating Arrays <ul><li>Arrays </li></ul><ul><ul><li>Specify type, use  new  operator </li></ul></ul><...
25.10  Examples Using Arrays <ul><li>new </li></ul><ul><ul><li>Dynamically creates arrays </li></ul></ul><ul><li>Method  l...
InitArray.java
Program Output
InitArray.java
Program Output
InitArray.java (Part 1 of 2)
InitArray.java (Part 2 of 2) Program Output
SumArray.java Program Output
StudentPoll.java (Part 1 of 2)
StudentPoll.java (Part 2 of 2) Program Output
Histogram.java
Program Output
RollDie.java (Part 1 of 2)
RollDie.java (Part 2 of 2) Program Output
25.11 References and Reference Parameters <ul><li>Passing arguments to methods </li></ul><ul><ul><li>Call-by-value: pass c...
25.12  Multiple-Subscripted Arrays <ul><li>Multiple-Subscripted Arrays </li></ul><ul><ul><li>Represent tables </li></ul></...
25.12  Multiple-Subscripted Arrays (II) <ul><li>Definition (continued) </li></ul><ul><ul><li>Initializer lists </li></ul><...
InitArray.java (Part 1 of 2)
InitArray.java (Part 2 of 2) Program Output
y given as a character which will be stored as it is in the char type variable c. Code of the program :  public class  con...
Upcoming SlideShare
Loading in …5
×

basic concepts

1,056
-1

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,056
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

basic concepts

  1. 1. Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1 Introduction 25.2 Primitive Data Types and Keywords 25.3 Logical Operators 25.4 Method Definitions 25.5 Java API Packages 25.6 Random Number Generation 25.7 Example: A Game of Chance 25.8 Methods of Class JApplet 25.9 Defining and Allocating Arrays 25.10 Examples Using Arrays 25.11 References and Reference Parameters 25.12 Multiple-Subscripted Arrays
  2. 2. Objectives <ul><li>In this chapter, you will learn: </li></ul><ul><ul><li>To understand primitive types and logical operators as they are used in Java. </li></ul></ul><ul><ul><li>To introduce the common math methods available in the Java API. </li></ul></ul><ul><ul><li>To be able to create new methods. </li></ul></ul><ul><ul><li>To understand the mechanisms used to pass information between methods. </li></ul></ul><ul><ul><li>To introduce simulation techniques using random number generation. </li></ul></ul><ul><ul><li>To understand array objects in Java. </li></ul></ul><ul><ul><li>To understand how to write and use methods that call themselves. </li></ul></ul>
  3. 3. 25.1 Introduction <ul><li>In this chapter </li></ul><ul><ul><li>Differences between C, C++, and Java </li></ul></ul><ul><ul><li>Java's logical operators and methods </li></ul></ul><ul><ul><li>Packages that comprise Applications Programming Interface (API) </li></ul></ul><ul><ul><li>Craps simulator </li></ul></ul><ul><ul><li>Random numbers in Java </li></ul></ul><ul><ul><li>Arrays in Java </li></ul></ul>
  4. 4. 25.2 Primitive Data Types and Keywords <ul><li>Primitive data types </li></ul><ul><ul><li>char , byte , short , int , long , float , double , boolean </li></ul></ul><ul><ul><li>Building blocks for more complicated types </li></ul></ul><ul><ul><ul><li>All variables must have a type before being used </li></ul></ul></ul><ul><ul><ul><li>Strongly typed language </li></ul></ul></ul><ul><ul><li>Primitive types portable, unlike C and C++ </li></ul></ul><ul><ul><ul><li>In C/C++, write different versions of programs </li></ul></ul></ul><ul><ul><ul><ul><li>Data types not guaranteed to be identical </li></ul></ul></ul></ul><ul><ul><ul><ul><li>ints may be 2 or 4 bytes, depending on system </li></ul></ul></ul></ul><ul><ul><ul><li>WORA - Write once, run anywhere </li></ul></ul></ul><ul><ul><li>Default values </li></ul></ul><ul><ul><ul><li>boolean gets false , all other types are 0 </li></ul></ul></ul>
  5. 5. 25.2 Primitive Data Types and Keywords (II)
  6. 6. 25.2 Primitive Data Types and Keywords (III) <ul><li>Keywords </li></ul><ul><ul><li>Reserved names, cannot be used as identifiers </li></ul></ul><ul><ul><li>Used to implement features </li></ul></ul>
  7. 7. 25.3 Logical Operators <ul><li>Logical operators </li></ul><ul><ul><li>Form complex conditions and control structures </li></ul></ul><ul><ul><li>Logical AND ( && ) </li></ul></ul><ul><ul><ul><li>true if both conditions true </li></ul></ul></ul><ul><ul><li>Logical OR ( || ) </li></ul></ul><ul><ul><ul><li>true if either condition true </li></ul></ul></ul><ul><ul><ul><li>true if both conditions true (inclusive) </li></ul></ul></ul><ul><ul><ul><li>If left condition true , skips right condition </li></ul></ul></ul><ul><ul><li>Boolean logical AND ( & ) , boolean logical inclusive OR ( | ) </li></ul></ul><ul><ul><ul><li>Act like counterparts, but always evaluate both expressions </li></ul></ul></ul><ul><ul><ul><li>Useful if expression performs action: </li></ul></ul></ul><ul><ul><ul><li>birthday == true | ++age >= 65 </li></ul></ul></ul>
  8. 8. 25.3 Logical Operators (II)
  9. 9. 25.3 Logical Operators (III) <ul><li>Logical Operators (continued) </li></ul><ul><ul><li>Boolean logical exclusive OR ( ^ ) </li></ul></ul><ul><ul><ul><li>true if exactly one condition true </li></ul></ul></ul><ul><ul><ul><li>false if both conditions true </li></ul></ul></ul><ul><ul><li>Logical NOT (negation) </li></ul></ul><ul><ul><ul><li>Unary operator (one operand) </li></ul></ul></ul><ul><ul><ul><ul><li>All other logical operators binary (two operands) </li></ul></ul></ul></ul><ul><ul><ul><li>Reverses condition </li></ul></ul></ul><ul><ul><ul><li>If true , returns false </li></ul></ul></ul><ul><ul><ul><li>If false , returns true </li></ul></ul></ul><ul><ul><ul><li>!= - &quot;does not equal&quot; </li></ul></ul></ul><ul><ul><ul><li>if (grade != sentinelValue) </li></ul></ul></ul>
  10. 10. 25.3 Logical Operators (IV)
  11. 11. 25.3 Logical Operators (V) <ul><li>More GUI Classes ( javax.swing ) </li></ul><ul><ul><li>JTextArea </li></ul></ul><ul><ul><ul><li>Create an area where text can be displayed </li></ul></ul></ul><ul><ul><ul><li>Provide ( rows , columns ) to constructor to specify size </li></ul></ul></ul><ul><ul><ul><li>JTextArea myArea; //declares object type </li></ul></ul></ul><ul><ul><ul><li>myArea = new JTextArea( 17, 20 ); //initialize </li></ul></ul></ul><ul><ul><li>myArea.setText( myString ); </li></ul></ul><ul><ul><ul><li>Sets the text of myArea to myString </li></ul></ul></ul><ul><ul><li>JScrollPane </li></ul></ul><ul><ul><ul><li>Creates a window that can scroll </li></ul></ul></ul><ul><ul><ul><li>JScrollPane myScroller = </li></ul></ul></ul><ul><ul><ul><li>new JScrollPane ( myArea ); </li></ul></ul></ul><ul><ul><ul><li>Declaration and initialization, allows myArea to have scrolling </li></ul></ul></ul>
  12. 12. 25.3 Logical Operators (VI) <ul><li>More GUI classes </li></ul><ul><ul><li>showMessageDialog(null, myScroller, titleString, type); </li></ul></ul><ul><ul><ul><li>Second argument indicates that myScroller (and attached myArea ) should be displayed in message dialog </li></ul></ul></ul>
  13. 13. Logical-Operators.java (Part 1 of 2)
  14. 14. Logical-Operators.java (Part 2 of 2)
  15. 15. Program Output
  16. 16. 25.4 Method Definitions <ul><li>Method definition format </li></ul><ul><ul><li>return-value-type method-name ( parameter-list ) { declarations and statements } </li></ul></ul><ul><ul><li>Method-name: any valid identifier </li></ul></ul><ul><ul><li>Return-value-type: data type of the result </li></ul></ul><ul><ul><ul><li>void - method returns nothing </li></ul></ul></ul><ul><ul><ul><li>Can return at most one value </li></ul></ul></ul><ul><ul><li>Parameter-list: comma separated list, defines parameters </li></ul></ul><ul><ul><ul><li>Method call must have proper number and type of parameters </li></ul></ul></ul><ul><ul><li>Definitions and statements: method body (block) </li></ul></ul><ul><ul><ul><li>Variables can be defined inside blocks (can be nested) </li></ul></ul></ul><ul><ul><ul><li>Method cannot be defined inside another function </li></ul></ul></ul>
  17. 17. 25.4 Method Definitions (II) <ul><li>Program control </li></ul><ul><ul><li>When method call encountered </li></ul></ul><ul><ul><ul><li>Control transferred from point of invocation to method </li></ul></ul></ul><ul><ul><li>Returning control </li></ul></ul><ul><ul><ul><li>If nothing returned: return; </li></ul></ul></ul><ul><ul><ul><ul><li>Or until reaches right brace </li></ul></ul></ul></ul><ul><ul><ul><li>If value returned: return expression ; </li></ul></ul></ul><ul><ul><ul><ul><li>Returns the value of expression </li></ul></ul></ul></ul><ul><ul><li>Example user-defined method: </li></ul></ul><ul><ul><li>public int square( int y ) { </li></ul></ul><ul><ul><li>return y * y </li></ul></ul><ul><ul><li>} </li></ul></ul>
  18. 18. 25.4 Method Definitions (III) <ul><li>Calling methods </li></ul><ul><ul><li>Three ways </li></ul></ul><ul><ul><ul><li>Method name and arguments </li></ul></ul></ul><ul><ul><ul><ul><li>Can be used by methods of same class </li></ul></ul></ul></ul><ul><ul><ul><ul><li>square( 2 ); </li></ul></ul></ul></ul><ul><ul><ul><li>Dot operator - used with objects </li></ul></ul></ul><ul><ul><ul><ul><li>g.drawLine( x1, y1, x2, y2 ); </li></ul></ul></ul></ul><ul><ul><ul><li>Dot operator - used with static methods of classes </li></ul></ul></ul><ul><ul><ul><ul><li>Integer.parseInt( myString ); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>More Chapter 26 </li></ul></ul></ul></ul>
  19. 19. 25.4 Method Definitions (IV) <ul><li>More GUI components </li></ul><ul><ul><li>Content Pane - on-screen display area </li></ul></ul><ul><ul><ul><li>Attach GUI components to it to be displayed </li></ul></ul></ul><ul><ul><ul><li>Object of class Container ( java.awt ) </li></ul></ul></ul><ul><ul><li>getContentPane </li></ul></ul><ul><ul><ul><li>Method inherited from JApplet </li></ul></ul></ul><ul><ul><ul><li>Returns reference to Content Pane </li></ul></ul></ul><ul><ul><ul><li>Container c = getContentPane(); </li></ul></ul></ul><ul><ul><li>Container method add </li></ul></ul><ul><ul><ul><li>Attaches GUI components to content pane, so they can be displayed </li></ul></ul></ul><ul><ul><ul><li>For now, only attach one component (occupies entire area) </li></ul></ul></ul><ul><ul><ul><li>Later, learn how to add and layout multiple components </li></ul></ul></ul><ul><ul><ul><li>c.add( myTextArea ); </li></ul></ul></ul>
  20. 20. SquareInt.java (Part 1 of 2)
  21. 21. SquareInt.java (Part 2 of 2) Program Output
  22. 22. 25.5 Java API Packages <ul><li>As we have seen </li></ul><ul><ul><li>Java has predefined, grouped classes called packages </li></ul></ul><ul><ul><li>Together, all the packages are the Applications Programming Interface (API) </li></ul></ul><ul><ul><li>Fig 25.10 has a list of the packages in the API </li></ul></ul><ul><li>Import </li></ul><ul><ul><li>Import statements specify location of classes </li></ul></ul><ul><ul><li>Large number of classes, avoid reinventing the wheel </li></ul></ul>
  23. 23. 25.5 Java API Packages (II)
  24. 24. 25.5 Java API Packages (III)
  25. 25. 25.5 Java API Packages (IV)
  26. 26. 25.5 Java API Packages (V)
  27. 27. 25.5 Java API Packages (VI)
  28. 28. 25.5 Java API Packages (VII)
  29. 29. 25.5 Java API Packages (VIII)
  30. 30. 25.6 Random Number Generation <ul><li>Math.random() </li></ul><ul><ul><li>Returns a random double , greater than or equal to 0.0 , less than 1.0 </li></ul></ul><ul><li>Scaling and shifting </li></ul><ul><ul><li>n = a + (int) ( Math.random() * b ) </li></ul></ul><ul><ul><li>n = random number </li></ul></ul><ul><ul><li>a = shifting value </li></ul></ul><ul><ul><li>b = scaling value </li></ul></ul><ul><ul><li>In C we used % , but in Java we can use * </li></ul></ul><ul><ul><li>For a random number between 1 and 6, </li></ul></ul><ul><ul><li>n = 1 + (int) ( Math.random() * 6 ) </li></ul></ul>
  31. 31. RandomInt.java Program Output
  32. 32. RollDie.java (Part 1 of 2)
  33. 33. RollDie.java (Part 1 of 2) Program Output
  34. 34. 25.7 Example: A Game of Chance <ul><li>Redo &quot;craps&quot; simulator from Chapter 5 </li></ul><ul><li>Rules </li></ul><ul><ul><li>Roll two dice </li></ul></ul><ul><ul><ul><li>7 or 11 on first throw, player wins </li></ul></ul></ul><ul><ul><ul><li>2, 3, or 12 on first throw, player loses </li></ul></ul></ul><ul><ul><ul><li>4, 5, 6, 8, 9, 10 - value becomes player's &quot;point&quot; </li></ul></ul></ul><ul><ul><li>player must roll his point before rolling 7 to win </li></ul></ul>
  35. 35. 25.7 Example: A Game of Chance (II) <ul><li>User input </li></ul><ul><ul><li>Till now, used message dialog and input dialog </li></ul></ul><ul><ul><ul><li>Tedious, only show one message/ get one input at a time </li></ul></ul></ul><ul><ul><li>Now, we will use event handling for more complex GUI </li></ul></ul><ul><li>extends keyword </li></ul><ul><ul><li>Class inherits data and methods from another class </li></ul></ul><ul><ul><li>A class can also implement an interface </li></ul></ul><ul><ul><ul><li>Keyword implements </li></ul></ul></ul><ul><ul><ul><li>Interface - specifies methods you must define in your class </li></ul></ul></ul><ul><li>Event handling </li></ul><ul><ul><li>Event: user interaction (i.e., user clicking a button) </li></ul></ul><ul><ul><li>Event handler: method called in response to an event </li></ul></ul>
  36. 36. 25.7 Example: A Game of Chance (III) <ul><li>Interface ActionListener </li></ul><ul><ul><li>Requires that you define method actionPerformed </li></ul></ul><ul><ul><ul><li>actionPerformed is the event handler </li></ul></ul></ul><ul><li>Class JTextField </li></ul><ul><ul><li>Can display or input a line of text </li></ul></ul><ul><li>Class JButton </li></ul><ul><ul><li>Displays a button which can perform an action if pushed </li></ul></ul><ul><ul><li>Method addActionListener( this ); </li></ul></ul><ul><ul><ul><li>Specifies this applet should listen for events from the JButton object </li></ul></ul></ul><ul><ul><li>Each component must know which method will handle its events </li></ul></ul><ul><ul><ul><li>Registering the event handler </li></ul></ul></ul>
  37. 37. 25.7 Example: A Game of Chance (IV) <ul><li>Class JButton (continued) </li></ul><ul><ul><li>We registered this applet with our JButton </li></ul></ul><ul><ul><ul><li>The applet &quot;listens&quot; for events from the </li></ul></ul></ul><ul><ul><li>actionPerformed is the event handler </li></ul></ul><ul><li>Event-driven programming </li></ul><ul><ul><li>User's interaction with GUI drives program </li></ul></ul><ul><li>final </li></ul><ul><ul><li>Defines a variable constant </li></ul></ul><ul><ul><ul><li>Cannot be modified </li></ul></ul></ul><ul><ul><ul><li>Must be initialized at definition </li></ul></ul></ul><ul><ul><ul><li>const int MYINT = 3; </li></ul></ul></ul><ul><ul><ul><li>Use all uppercase for final variables </li></ul></ul></ul>
  38. 38. 25.7 Example: A Game of Chance (V) <ul><li>Methods of class Container </li></ul><ul><ul><li>Recall that the Content Pane is of class Container </li></ul></ul><ul><ul><li>Method setLayout </li></ul></ul><ul><ul><ul><li>Define layout managers (determine position and size of all components attached to container) </li></ul></ul></ul><ul><ul><ul><li>FlowLayout - Most basic layout manager </li></ul></ul></ul><ul><ul><ul><ul><li>Items placed left to right in order added to container </li></ul></ul></ul></ul><ul><ul><ul><ul><li>When end of line reached, continues on next line </li></ul></ul></ul></ul><ul><ul><ul><ul><li>c = getContentPane(); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>c.setLayout( new FlowLayout() ); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Initialized with object of class FlowLayout </li></ul></ul></ul></ul>
  39. 39. Craps.java (Pat 1 of 5)
  40. 40. Craps.java (Pat 2 of 5)
  41. 41. Craps.java (Pat 3 of 5)
  42. 42. Craps.java (Pat 4 of 5)
  43. 43. Craps.java (Pat 5 of 5)
  44. 44. Program Output
  45. 45. 25.8 Methods of Class JApplet <ul><li>Methods of Class JApplet </li></ul><ul><ul><li>init, start, stop, paint, destroy </li></ul></ul><ul><ul><li>Called automatically during execution </li></ul></ul><ul><ul><li>By default, have empty bodies </li></ul></ul><ul><ul><li>Must define yourself, using proper first line </li></ul></ul><ul><ul><ul><li>Otherwise, will not be called automatically </li></ul></ul></ul><ul><ul><ul><li>See Figure 25.14 for proper first lines </li></ul></ul></ul><ul><li>Method repaint </li></ul><ul><ul><li>Dynamically change appearance of applet </li></ul></ul><ul><ul><li>Cannot call paint (do not have a Graphics object) </li></ul></ul><ul><ul><li>repaint(); calls update which passes Graphics object for us </li></ul></ul><ul><ul><ul><li>Erases previous drawings and calls paint </li></ul></ul></ul>
  46. 46. 25.8 Methods of Class JApplet (II) First line of JApplet methods (descriptions Fig. 25.14) public void init() public void start() public void paint( Graphics g ) public void stop() public void destroy()
  47. 47. 25.9 Defining and Allocating Arrays <ul><li>Arrays </li></ul><ul><ul><li>Specify type, use new operator </li></ul></ul><ul><ul><li>Two steps: </li></ul></ul><ul><ul><ul><li>int c[]; //definition </li></ul></ul></ul><ul><ul><ul><li>c = new int[ 12 ]; //initialization </li></ul></ul></ul><ul><ul><li>One step: </li></ul></ul><ul><ul><ul><li>int c[] = new int[12]; </li></ul></ul></ul><ul><ul><li>Primitive elements initialized to zero or false </li></ul></ul><ul><ul><ul><li>Non-primitive references are null </li></ul></ul></ul><ul><ul><li>Multiple definitions: </li></ul></ul><ul><ul><li>String b[] = new String[ 100 ], x[] = new String[ 27 ]; </li></ul></ul><ul><ul><li>Also: </li></ul></ul><ul><ul><li>double[] array1, array2; </li></ul></ul><ul><ul><li>Put brackets after data type </li></ul></ul>
  48. 48. 25.10 Examples Using Arrays <ul><li>new </li></ul><ul><ul><li>Dynamically creates arrays </li></ul></ul><ul><li>Method length </li></ul><ul><ul><li>Returns length of the array </li></ul></ul><ul><ul><li>myArray.length </li></ul></ul><ul><li>Initializer lists </li></ul><ul><ul><li>int myArray[] = { 1, 2, 3, 4, 5 }; </li></ul></ul><ul><ul><ul><li>new operator not needed, provided automatically </li></ul></ul></ul>
  49. 49. InitArray.java
  50. 50. Program Output
  51. 51. InitArray.java
  52. 52. Program Output
  53. 53. InitArray.java (Part 1 of 2)
  54. 54. InitArray.java (Part 2 of 2) Program Output
  55. 55. SumArray.java Program Output
  56. 56. StudentPoll.java (Part 1 of 2)
  57. 57. StudentPoll.java (Part 2 of 2) Program Output
  58. 58. Histogram.java
  59. 59. Program Output
  60. 60. RollDie.java (Part 1 of 2)
  61. 61. RollDie.java (Part 2 of 2) Program Output
  62. 62. 25.11 References and Reference Parameters <ul><li>Passing arguments to methods </li></ul><ul><ul><li>Call-by-value: pass copy of argument </li></ul></ul><ul><ul><li>Call-by-reference: pass original argument </li></ul></ul><ul><ul><ul><li>Improve performance, weaken security </li></ul></ul></ul><ul><li>In Java, cannot choose how to pass arguments </li></ul><ul><ul><li>Primitive data types passed call-by-value </li></ul></ul><ul><ul><li>References to objects passed call-by-reference </li></ul></ul><ul><ul><ul><li>Original object can be changed in method </li></ul></ul></ul><ul><ul><li>Arrays in Java treated as objects </li></ul></ul><ul><ul><ul><li>Passed call-by-reference </li></ul></ul></ul>
  63. 63. 25.12 Multiple-Subscripted Arrays <ul><li>Multiple-Subscripted Arrays </li></ul><ul><ul><li>Represent tables </li></ul></ul><ul><ul><ul><li>Arranged by m rows and n columns ( m by n array) </li></ul></ul></ul><ul><ul><ul><li>Can have more than two subscripts </li></ul></ul></ul><ul><ul><li>Java does not support multiple subscripts directly </li></ul></ul><ul><ul><ul><li>Create an array with arrays as its elements </li></ul></ul></ul><ul><ul><ul><li>Array of arrays </li></ul></ul></ul><ul><li>Definition </li></ul><ul><ul><li>Double brackets </li></ul></ul><ul><ul><li>int b[][]; b = new int[ 3 ][ 3 ]; </li></ul></ul><ul><ul><ul><li>Defines a 3 by 3 array </li></ul></ul></ul>
  64. 64. 25.12 Multiple-Subscripted Arrays (II) <ul><li>Definition (continued) </li></ul><ul><ul><li>Initializer lists </li></ul></ul><ul><ul><ul><li>int b[][] = { { 1, 2 }, { 3, 4 } }; </li></ul></ul></ul><ul><ul><li>Each row can have a different number of columns: </li></ul></ul><ul><ul><li>int b[][]; b = new int[ 2 ][ ]; // allocate rows b[ 0 ] = new int[ 5 ]; // allocate columns for row 0 b[ 1 ] = new int[ 3 ]; // allocate columns for row 1 </li></ul></ul><ul><ul><li>Notice how b[ 0 ] is initialized as a new int array </li></ul></ul>4 3 2 1
  65. 65. InitArray.java (Part 1 of 2)
  66. 66. InitArray.java (Part 2 of 2) Program Output
  67. 67. y given as a character which will be stored as it is in the char type variable c. Code of the program : public class  conversion{    public static void  main(String[] args){      boolean  t =  true ;      byte  b = 2;      short  s = 100;      char  c = 'C';      int  i = 200;      long  l = 24000;      float  f = 3.14f;      double  d = 0.000000000000053;     String g = &quot;string&quot;;     System.out.println(&quot;Value of all the variables like&quot;);     System.out.println(&quot;t = &quot; + t );     System.out.println(&quot;b = &quot; + b );     System.out.println(&quot;s = &quot; + s );     System.out.println(&quot;c = &quot; + c );     System.out.println(&quot;i = &quot; + i );     System.out.println(&quot;l = &quot; + l );     System.out.println(&quot;f = &quot; + f );     System.out.println(&quot;d = &quot; + d );     System.out.println(&quot;g = &quot; + g );     System.out.println();     //Convert from boolean to byte.     b = ( byte )(t?1:0);     System.out.println(&quot;Value of b after conversion : &quot; + b);     //Convert from boolean to short.     s = ( short )(t?1:0);     System.out.println(&quot;Value of s after conversion : &quot; + s);     //Convert from boolean to int.     i = ( int )(t?1:0);     System.out.println(&quot;Value of i after conversion : &quot; + i);     //Convert from boolean to char.     c = ( char )(t?'1':'0');     System.out.println(&quot;Value of c after conversion : &quot; + c);     c = ( char )(t?1:0);     System.out.println(&quot;Value of c after conversion in unicode : &quot; + c);     //Convert from boolean to long.     l = ( long )(t?1:0);     System.out.println(&quot;Value of l after conversion : &quot; + l);     //Convert from boolean to float.     f = ( float )(t?1:0);     System.out.println(&quot;Value of f after conversion : &quot; + f);     //Convert from boolean to double.     d = ( double )(t?1:0);     System.out.println(&quot;Value of d after conversion : &quot; + d);     //Convert from boolean to String.     g = String.valueOf(t);     System.out.println(&quot;Value of g after conversion : &quot; + g);     g = (String)(t?&quot;1&quot;:&quot;0&quot;);     System.out.println(&quot;Value of g after conversion : &quot; + g);      int  sum = ( int )(b + i + l + d + f);     System.out.println(&quot;Value of sum after conversion : &quot; + sum);   } }
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×