Agenda Array accessing Automatic initialization of an array

Slides:



Advertisements
Similar presentations
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Arrays Chapter 7.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Functions + Overloading + Scope
4. Java language basics: Function
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
Chapter 10 – Exception Handling
Testing and Debugging.
Introduction to Exceptions in Java
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Arrays ICS 111: Introduction to Computer Science I
Peer Instruction 6 Java Arrays.
Exceptions & exception handling
Pass by Reference, const, readonly, struct
Chapter 6 Arrays Solution Opening Problem
Exceptions & exception handling
Can store many of the same kind of data together
Example: Finding the Mode
Defining New Types of Objects, part 3
An Introduction to Java – Part I, language basics
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
TRY CATCH BLOCK By Kosala Rajapaksha.
Arrays in Java What, why and how Copyright Curt Hill.
1) C program development 2) Selection structure
Can store many of the same kind of data together
Dynamic Data Structures and Generics
Object Oriented Programming in java
Arrays.
Chapter 7: User-Defined Functions II
Chapter 6 Arrays.
Can store many of the same kind of data together
Object Oriented Programming
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Arrays.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Peer Instruction 4 Control Loops.
Generic Programming.
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
How do you do the following?
Introduction to java Part I By Shenglan Zhang.
First Semester Review.
CHAPTER 6 Testing and Debugging.
Presentation transcript:

Agenda Array accessing Automatic initialization of an array Array exceptions Array parameter Enhanced for loop presentations

Array –accessing an element An array element is accessed by including its index in brackets after the array name. System.out.println(friends[2]); //displays ?

Array – changing an element If we want to change the Friends array, element 1 to “Sunshine”, how? Friends[0] = “Sunshine”;

Automatic initialization of an array Only with numeric array(double or int), all elements are auto initialized to 0. double[] doubleArray= new double[10]; But not with String array

Which of the following correctly initializes an array arr to contain 4 elements, each with value 0? Int [] arr = {0, 0, 0, 0}; Int[] arr = new int[4]; for (int i=0; i < arr; i++) arr [i] = 0;

What is exception? Errors that are not detected by the compiler may generate a run-time error. A run-time error, also called an exception, halts program execution at the statement that cannot be executed.

Array run time errors NullPointerException double x[]; => declared only x[0] = 3.5; => error ArrayIndexOutOfBoundException A run-time error when an invalid index is used.

Run time error for array Friends[4] = “Mercury”; => ? A run-time error is generated when an invalid index is used. For example, the error(exception) ArrayIndexOutOfboundsException is thrown when the following statement tries to execute:

Parameters passing to methods Pass by reference - Arrays, objectsd Pass by value - primitive data types

Pass by reference int[] list = { 1, 2, 3, 4}; changeArray(list); System.out.print(“The changed list is “); for(int i=0; i< list.length; i++) System.out.print(list[i] + “ “);

Pass by reference list 3 4 list 3 4 b list 6 7 b list 6 7 Before the method call: 1 22 3 4 list At start of the method call: 1 22 3 4 b list Just before exiting the method: 4 5 6 7 b After exiting the method: list 4 5 6 7

Pass by value public static void tryChanging(int aNum) { aNum = 456; } int origNum = 123; tryChanging(origNum);

Pass by Value Before the method call: origNum 123 At start of the method call: origNum 123 aNum 123 origNum Just before exiting the method: 123 aNum 456 After exiting the method: origNum 123

Method with array and int params public static void tryChanging(int[] numbers, int aNum) { numbers[1] = 123; aNum = 456; }

int[] myNums = {5, 8, 3}; System. out int[] myNums = {5, 8, 3}; System.out.println(myNums[1] + " " + myNums[0]); tryChanging(myNums, myNums[0]);

Enhanced for loop Auto loop through all the elements of an array. Done without specifying the length of the array and without any dummy integer index. Syntax for(type dummyName: Array name) dummyName is read-only

Example of enhanced for loop int[] aArry = {1, 2, 3, 4}; for (int aElem : aArry) System.out.println(aElem+ " "); => For each element in the aArry, print it out = for(int i=0; i < aArry.length; i++) System.out.println(aArry[i] + “ “);

Practice Exercise 1 —EvensAndOdds Create an EvensAndOdds application that generates 25 random integers between 0 and 99 and then displays all the evens on one line and all the odds on the next line. Application output should look similar to: ODD: 21 21 87 39 45 7 75 87 9 27 Even: 4 36 44 98 0 40 82 32 0 26

Practice Exercise 2 Palindrome Create a Palindrome application that prompts the user for a string and then displays a message indicating whether or not the string is a palindrome. A palindrome is a word or phrase that is spelled the same backwards and forwards. For example, “mom” is a palindrome, as well as “kayak” and “Never odd or even”. Turn in the exercise 1 before the break Turn in the exercise 2 for extra bonus

Presentations Algorithm – written in pseudo code, your approach for the problem. Flowchart – a diagram representations. Source code – should be bug free and has output as intended