1 More on 1-D Arrays Overview l Array as a return type to methods l Array of Objects.

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Algorithms and Problem Solving
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
1 Lecture 23 Searching Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
1 Repetition structures Overview while statement for statement do while statement.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch.
More on Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lectures 2 & 3 Wanda M. Kunkle.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
String Tokenization What is String Tokenization?
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
1 StringTokenizer and StringBuffer classes Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples l StringBuffer class.
Looping Yong Choi School of Business CSU, Bakersfield.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Search & Sorting Using Java API Searching and Sorting Using Standard Classes  Searching data other than primitive type.  Comparable interface.  Implementing.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
1 Selection Statements Overview l Relational and Logical Operations l Selection structures »if statement »if-else statement l Preview: More on Selection.
1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Introduction.
1 Memory Model of A Program, Methods Overview l Memory storage areas for an executing program l Introduction to methods and methods definitions l General.
1 Repetition structures [cont’d] Overview l Nested Loops l Sentinel-Controlled loop l Avoiding Number Format exception.
1 More on 1-D Array Overview l Array as a return type to methods l Array of Objects l Array Cloning l Preview: 2-D Arrays.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
1 One-Dimensional Arrays  What are and Why 1-D arrays?  1-D Array Declaration  Accessing elements of a 1-D Array  Initializer List  Passing Array.
Saravanan.G.
1 Course Lectures Available on line:
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
CS1101: Programming Methodology Recitation 3 – Control Structures.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
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.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
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.
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];
Methods.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
Math and String Examples
Operators Laboratory /11/16.
Yong Choi School of Business CSU, Bakersfield
Computing Adjusted Quiz Total Score
class PrintOnetoTen { public static void main(String args[]) {
Scope of variables class scopeofvars {
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Handout-16 Recursion Overview
Objects.
Presentation transcript:

1 More on 1-D Arrays Overview l Array as a return type to methods l Array of Objects

2 Array as return type to methods l We saw in the last lecture, that arrays can be passed as parameters to methods. l In addition, methods can return array as their result a reference to an array object. import java.io.*; class ArrayAndMethods { static BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int size; System.out.print("Enter array size: "); size = Integer.parseInt(stdin.readLine()); double[] firstArray = createArray(size); double[] secondArray = createArray(size); System.out.println("The dot product = "+dotProduct(firstArray, secondArray)); }

3 Array as return type to methods static double[] createArray(int size) throws IOException { double[] array = new double[size]; System.out.println("Enter "+size+" elements for this array: "); for (int i=0; i<size; i++) array[i] = Double.parseDouble( stdin.readLine()); return array; } static double dotProduct(double[] first, double[] second) { double sum = 0; for (int i=0; i<first.length; i++) sum += first[i]*second[i]; return sum; }

4 Array of Objects l In the examples we have seen so far, we have been creating arrays whose elements are primitive types. l We can equally create an array whose elements are objects. l Assuming we have a class named Student, then we can create an array, students, to hold 10 Student objects as follows: Student[] students = new Student[10];

5 Array of Objects (cont’d) l As you can see from the figure, there is a fundamental difference between an array of primitive type and an array of object. l Here, the array only holds references to the actual objects. l The statement: Student[] students = new Student[10]; only creates the references. To actually create the objects, we have to use the new operator, usually in a loop as follows: for (int i = 0; i < students.length ; i++) students[i] = new Student(id, grade); l The following is the complete Student example. class Student { int iDNumber; double grade; public Student(int iDNumber, double grade) { this.iDNumber = iDNumber; this.grade = grade; } public void print() { System.out.println(iDNumber+"\t"+grade); }

6 Array of Objects (cont’d) import java.io.*; public class ArrayOfObjects { static BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int size; System.out.print("Enter number of students: "); size = Integer.parseInt(stdin.readLine()); Student[] students = createArray(size); double average = average(students); System.out.println("The average is "+average); System.out.println("Students below average are"); for (int i=0; i<students.length; i++) if (students[i].grade < average) students[i].print(); }

7 Array of Objects (cont’d) static Student[] createArray(int size) throws IOException { Student[] array = new Student[size]; int id; double grade; System.out.println("Enter "+size+" students"); for (int i=0; i<size; i++) { System.out.print("ID Number : "); id = Integer.parseInt(stdin.readLine()); System.out.print("Grade : "); grade = Double.parseDouble( stdin.readLine()); array[i] = new Student(id, grade); } return array; } static double average(Student[] studentList) { double sum = 0; for (int i=0; i<studentList.length; i++) sum += studentList[i].grade; return sum/studentList.length; }