“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
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.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Java Syntax Primitive data types Operators Control statements.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
The switch statement: an N-way selection statement.
UNIT II Decision Making And Branching Decision Making And Looping
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
DAT602 Database Application Development Lecture 5 JAVA Review.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
“Introduction to Programming With Java”
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
BIT115: Introduction to Programming
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 CSC103: Introduction to Computer and Programming Lecture No 17.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Object Oriented Programming Lecture 2: BallWorld.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 2: Data Types, Variables, Operators, and Expressions
Introduction to programming in java
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Introduction To Programming Information Technology , 1’st Semester
Arrays .
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Lecture Notes – Week 2 Lecture-2
Single-Dimensional Arrays chapter6
Arrays in Java.
C Programming Lecture-8 Pointers and Memory Management
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL

‘switch’ construct Introduction to Arrays Contents for Today’s Lecture

class print_num { public static void main(String args[]) { int a_number; a_number = 0; if(a_number = = 1) System.out.println("One"); else { if(a_number = = 2) System.out.println("Two"); else { if(a_number = = 3) System.out.println("Three"); else System.out.println("No. is out of the range"); } } } } ‘if-else’ Program

class print_num_switch { public static void main(String args[]) { int a_number; a_number = 3; switch(a_number){ case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; case 3: System.out.println("Three"); break; default: System.out.println("No. is out of the range"); } } } ‘switch’ Program

switch(expression){ case value1: statements; break; case value2: statements; break;.. case valueN: statements; break; default: statements; } Syntax

The case values can be compared only for equality with the switch expression The expression must be of type int, char, short, byte Each case value must be a constant, not a variable Duplicate case values are not allowed The default part is optional Note

Definition: An array is a group/collection of variables of the same type that are referred to by a common name and an index Examples: Collection of numbers Collection of names Collection of suffixes Arrays

Array of numbers: Array of names: Array of suffixes: Examples ment tion ness ves Sholay Shaan Shakti

Array is like a pen box with fixed no. of slots of same size. Analogy

Declaration of array variable: data-type variable-name[]; eg. int marks[]; This will declare an array named ‘marks’ of type ‘int’. But no memory is allocated to the array. Allocation of memory: variable-name = new data-type[size]; eg. marks = new int[5]; This will allocate memory of 5 integers to the array ‘marks’ and it can store upto 5 integers in it. ‘new’ is a special operator that allocates memory. Syntax

Accessing elements in the array: Specific element in the array is accessed by specifying name of the array followed the index of the element. All array indexes in Java start at zero. variable-name[index] = value; eg. marks[0] = 10; This will assign the value 10 to the 1 st element in the array. ; And marks[2] = 863; This will assign the value 863 to the 3 rd element in the array. Syntax…

STEP 1 : (Declaration) int marks[]; marks  null STEP 2: (Memory Allocation) marks = new int[5]; marks  marks[0] marks[1] marks[2] marks[3] marks[4] STEP 3: (Accessing Elements) marks[0] = 10; marks  marks[0] marks[1] marks[2] marks[3] marks[4] Example

class try_array { public static void main(String args[]) { int marks[]; marks = new int[3]; marks[0] = 10; marks[1] = 35; marks[2] = 84; System.out.println(“Marks obtained by 2 nd student=” + marks[1]); } Program

Arrays can store elements of the same data type. Hence an int array CAN NOT store an element which is not an int. Though an element of a compatible type can be converted to int and stored into the int array. eg. marks[2] = (int) 22.5; This will convert ‘22.5’ into the int part ‘22’ and store it into the 3 rd place in the int array ‘marks’. Array indexes start from zero. Hence ‘marks[index]’ refers to the (index+1) th element in the array and ‘marks[size-1] refers to last element in the array. eg. marks[0] refers to 1 st element, marks[1] refers to 2 nd element… etc. etc. Note

Combined declaration & memory allocation: data-type variable-name[] = new data-type[size]; eg. int marks[] = new int[5]; This will declare an int array ‘marks’ and will also allocate memory of 5 integers to it. Combined declaration, allocation & assignment: data-type variable-name[] = {comma-separated values}; eg. int marks[] = {10, 35, 84, 23, 5}; This will declare an int array ‘marks’, will allocate memory of 5 integers to it and will also assign the values as- marks  marks[0] marks[1] marks[2] marks[3] marks[4] Alternative Syntax

1.Write a program that uses two arrays of size 5. One string array for storing names of students and one int array for storing marks obtained by the students. Assign values to the elements in the arrays. Print names and marks of students who have scored more than 35 marks. 2.Write a program same as the above, but don’t use numbers (like 0, 1, 2…etc) to access the elements in the array. Declare an int variable ‘i’ and use it as an index into the arrays. Assignment

Thank you… End