Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Grouping Objects - Introduction to Programming in Java - Lecture Slides, Slides of Java Programming

In this course, Introduction to Programming in Java, we learned all programming concepts and implement them in java. Key points in these lecture slides are: Grouping Objects, Call-By-Value, Arrays, Collections, Iterators, Parameter Passing, Similar But Different, Grouping Objects, Notebook, Fixed-Size Collections

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

106 documents

1 / 78

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objectives
discuss call-by-value and call-by-reference,
arrays, collections, and iterators
5. Grouping Objects
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e

Partial preview of the text

Download Grouping Objects - Introduction to Programming in Java - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

  • Objectives
    • discuss call-by-value and call-by-reference,

arrays, collections, and iterators

  1. Grouping Objects

Topics

• 1. Parameter Passing

• 2. Arrays: Similar but Different

• 3. Call-by-Reference with Classes

• 4. Grouping Objects

• 5. A Notebook Example

• 6. Iteration (looping)

• 7. An Auction Example

• 8. Fixed-size Collections

• 9. More Information

What is Call-by-Reference?

• An example for an imaginary language:

function foo()

integer x := 2;

bar(x);

print(“x is” + x);

function bar( ref integer w)

w := 5;

“x is 5” is printed

continued

  • Call-by-reference creates a link from the variable in the called function (e.g. w in bar()) to the original variable (e.g. x in foo()) - when w changes, x is also changed

Call-by-Value Example

public class SimpleCalls { public static void main(String[] args) { int x = 3; System.out.println("1. x = " + x); squareBad (x); // x = squareGood (x); System.out.println("2. x = " + x); } // end of main()

continued

private static void squareBad(int x) { System.out.println("sqBad 1. x = " + x); x = xx; System.out.println("sqBad 2. x = " + x); } private static int squareGood(int x) { System.out.println("sqGood 1. x = " + x); x = xx; System.out.println("sqGood 2. x = " + x); return x; } } // end of SimpleCalls class

static is used so that main() can call

these methods without creating an object first;

it has nothing to do with parameter passing

2. Arrays: Similar but Different

• Java arrays look like C arrays, but...

• Java arrays do not support pointer

manipulation.

• Arrays are objects , and so are passed to

methods using call-by-reference.

Declaring and Allocating Arrays

  • Some coding styles: int c[] = new int[12]; // creates a 12 element int array c[0] = 2 ;
  • or int c[]; // declares an array; no memory yet c[0] = 2 ; // ERROR! c = new int[12]; // allocates memory c[0] = 2; // OK - declare the type (e.g. int) - allocate memory with new

continued

A Different Syntax

• Instead of:

int c [] = new int[12];

• Can write:

int [] c = new int[12];

UseArray.java

import javax.swing.JOptionPane; public class UseArray { public static void main(String[] args) { int n[]; // declare array name n = new int[10]; // allocate memory to array // no values stored in n[], so will contain 0's String output = "Cell Value\n"; for(int i = 0; i < n.length; i++) output += "n[" + i + "] == " + n[i] + "\n"; JOptionPane.showMessageDialog( null, output, "Using an Array", JOptionPane.INFORMATION_MESSAGE ); } // end of main() } // end of UseArray class

two

steps

Notes

• n is declared: its type is specified

int n[]

• n is allocated memory with new

n = new int[10];

• n.length

– length always holds the length of the array

object (i.e. 10 in this case)

object

n

Using an Array

• Square brackets are used to access an array

element:

n[i]

• Array elements are used like ordinary variables

  • on the left of an assignment: n[0] = 3;
  • in an expression: x = n[1] – 3; n[i]++;

Example

for( int i = 0; i < n.length; i++) { System.out.println(i + ": " + n[i]); } int i = 0; while(i < n.length) { System.out.println(i + ": " + n[i]); i++; } for loop version while loop version

i only exists inside

the loop

Passing Arrays to Methods

• Arrays are Java objects

– they are passed to methods using

call-by-reference

– i.e. changes to an array inside a method

affects the original

– no return or pointers are required