Documentation Array and Searching. Documentation rules Easy rules: –Naming convention for variables, constants and methods Difficult rules: –Professional.

Slides:



Advertisements
Similar presentations
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
Java Unit 9: Arrays Declaring and Processing Arrays.
CSC – Java Programming II Lecture 9 January 30, 2002.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.
Lists in Python.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Grouping Related Items
Chapter 8 Arrays and Strings
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
 Pearson Education, Inc. All rights reserved Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
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.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
CIT 590 Intro to Programming First lecture on Java.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
A brief introduction to javadoc and doxygen. What’s in a program file? 1. Comments 2. Code.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Java Doc Guideline R.SANTHANA GOPALAN. Java Doc Guideline Audience Internal Developers PQA - who write test plans PPT – who write the documentation Customers.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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 Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ARRAYS Multidimensional realities Image courtesy of
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Computer Programming BCT 1113
Introduction to javadoc
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
Introduction to javadoc
Arrays.
Presentation transcript:

Documentation Array and Searching

Documentation rules Easy rules: –Naming convention for variables, constants and methods Difficult rules: –Professional comments that generate professional looking documentation using Javadoc tool

Naming convention Variables: –Class names, method names, and variable names must be descriptive. –NO one character names, short cryptic names/abbreviations are accepted Exception: counting variable in a for loop Constants: - 0 and 1 may be used through out the program - Otherwise, all constants should be defined and given descriptive names.

Examples 1. Which one is more descriptive (for a class to compute a special type of loan – discounted loan) DataProcessing or DiscountedLoan 2. Which one is better (for a variable name) l11or loanAmount 3. Is this acceptable: for (int i=0; i<MAX_AMOUNT; i++) { …… }

Examples Can I do this: int n = 12; Why/Why not?

Using Javadoc to generate documentation Examples: documentation for String class in Java documentation for JOptionPane class in Java How can we generate a similar document for our own class

Javadoc tool generating Application Program Interface (API) documentation in HTML format from doc comments in source code. Resource:

What we focus on in this course How to write simple doc comments for Javadoc How to run Javadoc

Writing Doc comments A doc comment must precede a class, field, constructor or method declaration Class description: First sentence: should be a summary sentence, containing a concise but complete description of a class. In other words, it should talk about the purpose of this class

Writing DOC comments Constructors/methods: Contains: PURPOSE, Data (parameters, what they are) and details description of data (parameters name, their data types) Example: public void writeToFile(String fileName, int array[]) /** Purpose: Write an array of integers to a file and throws exception if there is any errors happening during this process. Data: It takes two arguments: file name and array name. The filename argument must specify a valid file name. The array argument must specify the name of the array being written to a file. ………….. **/

Writing Doc comments Block (classes, interfaces, methods and constructors (methods is a synonym added in Javadoc (classes and interfaces only, (classes and interfaces only, required.

Required For example: fileName this represents the name of the file created for writing purpose. */ public void writeToFile(String fileName)

Require Example: IOException if any error happens during the process of writing to a file. */ public void writeToFile(String filename, int[] array) throws IOException

Required Note: Omit this for constructors and void methods Example: public void writeToFile(String fileName) ??? a boolean value (true/false) if there is no error/or any errors during reading process. */ public boolean readFromFile(String fileName)

Example /** What do I write here????? **/ public void readFromFile(String filename) throws IOException { ……………………………………………….. }

Fields /** field description */ public ; Note: The documentation is only generated for those methods/fields that are declared as public

How to run Javadoc Step 1: Make sure you have javadoc installed Step 2: Command line mode: change to the source directory type: javadoc d

How to run Javadoc in eclipse Step 1: Make sure you have javadoc installed (searching for javadoc.exe, usually in bin subdirectory of your JDK) Step 2: In eclipse: Choose Project -> Generate javadoc -> Configure to navigate to where your javadoc resides -> change the directory where javadoc documents will be generated if necessary. -> Finish

Example /** * Converts a given length in inches to equivalent centimeters. inches the length expressed in inches length expressed in centimeters */ public static double inchesToCentimeters( double inches ) { // this is a stub return 0; // please change it with your code }

Lab 2

Project 1 checklist Java files *.java Create your own book.txt Commenting your code

On the fly review Given a one dimensional array myArray, what is the correct way of getting the number of elements in this array. a. myArray.length b. myArray.length - 1 c. myArray.size d. myArray.size – 1

On the fly review Given a one dimensional array myArray, what is the correct way of getting the number of elements in this array. a. myArray.length b. myArray.length - 1 c. myArray.size d. myArray.size – 1

On the fly review Array is a collection of data values of the different data types a. True b. False

On the fly review Array is a collection of data values of the different data types a. True b. False

Arrays Array is a collection of data values of the same data type. Example: int[ ] number; // This is an array of integers double[] gpa; // This an array of double

Array Declaration Format: [] ; OR []; data type: double, int, float, String Example: double[] gpa; Or double gpa[];

Arrays The amount of memory allocated to store an array depends on the size and type of values in the array –Size: number of elements in the array –Type of values: data type of each element in the array –An individual value in an array is called array element Example: int[ ] number = new int[5]; data type: integer (4 bytes) size: 5 memory: 20 bytes Array is a reference data type. Array is NOT an object

Arrays Example: int[ ] number = new int[5]; number number[0] 6

Arrays Elements of an array are indexed from zero to size -1 Size: the number of elements in an array length: a public constant represents the size of an array. Example: sum =0; for (int i=0; i<number.length; i++) sum += number[i];

Fixed –size array declaration Size of an array is pre-determined. Example: int[] number= new int[5]; Problems: What if we have more than pre-determined size of an array? Underutilization of space.

Variable-size array declaration In Java, we can declare an array of different size every time we run a program Example: int size; int[] number; inputStr = JOptionPane.showInputDialog(null,"Please enter the number of elements "); size = Integer.parseInt(inputStr); number = new int[size];

Arrays for Objects // An array for the names of the distinct private Loan[] loanArray = new Loan[5]; Note: No Loan objects are created. Only a container for Loan. Each location is initialized to null. loanArray NULL

Arrays of Objects private Loan[] loanArray = new Loan[5]; for (i=0; i<5; i++) { loanArray[i] = new Loan(); } loanArray

Indexing an Array int[] number = new int [5]; number[0] =2; number[1]=3; number[2]=4; number[3]= 6; number[4]=-1;

Initializing Arrays... // Differentially number the assignments. private int[] number = {1, 2, 1, 5, 1,}; private final int totalNumber = number.length; No new required. Terminating semicolon. Optional final comma.

Further Initializer Examples String[] suitNames = { "Spades", "Hearts", "Diamonds", "Clubs" }; Point[] vertices = { new Point(0,0), new Point(0,1), new Point(1,1), new Point(1,0), }; YearlyRainfall y2k = new YearlyRainfall( new int[]{10,10,8,8,6,4,4,0,4,4,7,10,});

Iterating over an Array in Reverse int size = 5; minValue = number[size-1]; for (int i=size-2; i>=0; i--) { if (minValue > number[i]) minValue = number[i]; }

Passing Array to Methods When an array is passed to a method, only its reference is passed. A copy of the array is not created in the method. That means: we pass the identifier for that array which in fact is a reference to a start address of the array.

Passing Array to Methods Assuming changeArrayValue is one method of a class named ArrayClass public void changeArrayValue(int[] arrayChanged) { for (int i=0; i< arrayChanged.length-1; i++) arrayChanged[i] += 1; } We call this method as:

Passing Array to Methods ArrayClass anObj = new ArrayClass(); int number[] = new int[5]; for(int i=0; i<number.length; i++) number[0] = i; anObj.changeArrayValue(number);

Passing Array to Methods for(int i=0; i<number.length; i++) number[i] = i; anObj.changeArrayValue(number); number

On the fly review The amount of memory allocated to store an array depends on: a. the name of the array b. the size of the array c. the size and type of values in the array d. none of the above

On the fly review The amount of memory allocated to store an array depends on: a. the name of the array b. the size of the array c. the size and type of values in the array d. none of the above

On the fly review Arrays are: a. variable-length entities. b. fixed-length entities. c. data structures that contain up to 10 related data items. d. used to draw a sequence of lines, or “rays.”

On the fly review Arrays are: a. variable-length entities. b. fixed-length entities. c. data structures that contain up to 10 related data items. d. used to draw a sequence of lines, or “rays.”

Searching an Unsorted Array We often need to search an array for a particular item of data. The data is often unsorted. The item might or might not be present. –Care must be taken not to search beyond the end of the array. –We need to decide how to return a found item.

Search with Multiple Returns public int indexOf(int[] numbers,int value){ final int notPresent = -1; for(int index = 0; index < numbers.length; index++){ if(numbers[index] == value){ return index; } // We did not find it. return notPresent; }

The Arrays Class Defined in the java.util package. Contains static methods for manipulating arrays: –binarySearch : search for a value. –equals : compare the contents of two arrays. –fill : fill an array with a particular value. –sort : sort the contents of an array.

Multi-Dimensional Arrays Arrays of multiple dimensions are possible. –2D grid, for a board game such as chess. –3D cube structure, etc. Multi-dimensional arrays are regarded as being arrays of arrays. Non-rectangular structures are possible.

2D Array Construction final int numRows = 10, numCols = 5; double[][] matrix = new double[numRows][numCols]; char[][] hiddenWord = { { 'd', 'g', 'i', 'b' }, { 'e', 'i', 'u', 'm' }, { 't', 'a', 's', 'a' }, };

Review Arrays make it possible to group related items in a single object. An array's length is fixed on construction. Arrays may have multiple dimensions.

Multidimensional Arrays Multidimensional arrays –Tables with rows and columns Two-dimensional array m-by-n array

Two-dimensional array with three rows and four columns.

Multidimensional Arrays (Cont.) Arrays of one-dimensional array –Declaring two-dimensional array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; –1 and 2 initialize b[0][0] and b[0][1] –3 and 4 initialize b[1][0] and b[1][1] int b[][] = { { 1, 2 }, { 3, 4, 5 } }; –row 0 contains elements 1 and 2 –row 1 contains elements 3, 4 and 5

Multidimensional Arrays (Cont.) Two-dimensional arrays with rows of different lengths –Lengths of rows in array are not required to be the same E.g., int b[][] = { { 1, 2 }, { 3, 4, 5 } };

Multidimensional Arrays (Cont.) Creating two-dimensional arrays with array- creation expressions –Can be created dynamically 3 -by- 4 array int b[][]; b = new int[ 3 ][ 4 ]; Rows can have different number of columns int b[][]; b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 b[ 1 ] = new int[ 3 ]; // create 3 columns for row 1

Multidimensional Arrays (Cont.) Common multidimensional-array manipulations performed with for statements –Many common array manipulations use for statements E.g., for ( int column = 0; column < a[ 2 ].length; column++ ) a[ 2 ][ column ] = 0;

On the fly review In array items, which expression below retrieve the value at row 4 and column 5? a. items[ 3 ].[ 4 ] b. items[ 3[ 4 ] ] c. items[ 3 ][ 4 ] d. items[ 3, 4 ]

On the fly review In array items, which expression below retrieve the value at row 4 and column 5? a. items[ 3 ].[ 4 ] b. items[ 3[ 4 ] ] c. items[ 3 ][ 4 ] d. items[ 3, 4 ]

On the fly review An array with m rows and n columns is NOT: A. An m-by-n array.B. An n-by-m array. C. A two-dimensional array. 1. A and C. 2. A. 3. B. 4. C.

On the fly review An array with m rows and n columns is NOT: A. An m-by-n array.B. An n-by-m array. C. A two-dimensional array. 1. A and C. 2. A. 3. B. 4. C.