Object Oriented Programming

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Lecture 4 More on Java® Data Types, Control Structures.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
Java Unit 9: Arrays Declaring and Processing Arrays.
The University of Texas – Pan American
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
From C++ to Java A whirlwind tour of Java for C++ programmers.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
1 Creating Classes & Applications Chapter Six. 2 Defining Classes l class MyClassName { // new class l class myClassName extends SuperClass { –// your.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
A DVANCED P ROGRAMMING C HAPTER 8: A RRAYS Dr Shahriar Bijani Spring 2016.
Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Lecture 4b Repeating With Loops
Arrays Chapter 7.
Arrays 3/4 By Pius Nyaanga.
Command Line Arguments
Java Course Review.
Paul Ammann & Jeff Offutt
Counted Loops.
The University of Texas – Pan American
Data Structures Array - Code.
Advanced Programming Chapter 8: Arrays
Starting Out with Java: From Control Structures through Objects
Java How to Program, Late Objects Version, 10/e
Outline Altering flow of control Boolean expressions
Arrays.
An Introduction to Java – Part I, language basics
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
CS150 Introduction to Computer Science 1
Data Structures Array - Code.
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
class PrintOnetoTen { public static void main(String args[]) {
Single-Dimensional Arrays chapter6
Scope of variables class scopeofvars {
Arrays in Java.
CSS161: Fundamentals of Computing
Repetition Statements (Loops) - 2
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
CS100A Sections Dec Loop Invariant Review C Review and Example
Introduction to java Part I By Shenglan Zhang.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Control Statements:.
Presentation transcript:

Object Oriented Programming www.hndit.com Object Oriented Programming Lecture 06

For-each Loop www.hndit.com Purpose The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or for-each For-each loop Equivalent for loop for (type var :arr) { body-of-loop } for(int i=0; i <arr.length; i++) { type var = arr[i];

Example - Adding all elements of an array www.hndit.com Example - Adding all elements of an array Here is a loop written as both a for-each  loop and a basic for loop. double[ ] ar = {1.2, 3.0, 0.8}; int sum = 0; for (double d : ar) { sum += d; }

for (int i = 0; i < ar.length; i++) { sum += ar[i]; } www.hndit.com And here is the same loop using the basic for. It requires an extra iteration variable. double[] ar = {1.2, 3.0, 0.8}; int sum = 0; for (int i = 0; i < ar.length; i++) { sum += ar[i]; }

www.hndit.com int[ ] a={1,2,3,4,5,6,7,8,9,0};   for(int i : a){     System.out.println(i);   }

Using Command-Line Arguments www.hndit.com Using Command-Line Arguments On many systems it is possible to pass arguments from the command line (these are known as command-line arguments) to an application by including a parameter of type String[] (i.e., an array of Strings) in the parameter list of main. When an application is executed using the java command, Java passes the command-line arguments that appear after the class name in the java command to the application's main method as Strings in the array args.

www.hndit.com public class InitArray { public static void main( String args[] ) if ( args.length != 3 ) System.out.println( "Error: Please re-enter the entire command, including\n" + 12 "an array size, initial value and increment." ); else { int arrayLength = Integer.parseInt( args[ 0 ] ); int array[] = new int [ arrayLength ]; int initialValue = Integer.parseInt( args[ 1 ] ); int increment = Integer.parseInt( args[ 2 ] ) for ( int counter = 0; counter < array.length; counter++ ) array[ counter ] = initialValue + increment * counter; System.out.println( "Index" “\t Value" ); System.out.println( counter+”\t”+ array[ counter ] ); }