Arrays, File Access, and Plotting

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Introduction to C Programming
Introduction to C Programming
Chapter 8: Arrays.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
The Web Warrior Guide to Web Design Technologies
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 10.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Guide To UNIX Using Linux Third Edition
 Pearson Education, Inc. All rights reserved Arrays.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 2006 Pearson Education, Inc. All rights reserved Arrays.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 8: Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Chapter 2 part #1 C++ Program Structure
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Introduction to programming in java Lecture 21 Arrays – Part 1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
User-Written Functions
Chapter 7 Text Input/Output Objectives
Computer Programming BCT 1113
Array, Strings and Vectors
Chapter 7 Text Input/Output Objectives
Introduction to Python
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.
Chapter 2: Introduction to C++.
Presentation transcript:

Arrays, File Access, and Plotting Chapter 5 Arrays, File Access, and Plotting Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting An array is a special object containing: A group of contiguous memory locations that all have the same name and same type A separate instance variable containing the number of elements in the array Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Declaring Arrays An array must be declared or created before it can be used. This is a two-step process: First, declare a reference to an array. Then, create the array with the new operator. Example: double x[]; // Create reference x = new double[5]; // Create array object These steps can be combined on a single line: double x[] = new double[5]; // All together Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Using Arrays An array element may be used in any place where an ordinary variable of the same type may be used. An array element is addressed using the array name followed by a integer subscript in brackets: a[2] Arrays are typically used to perform the same calculation on many different values Example: for ( int i = 0; i < 100; i++ ) { a[i] = Math.sqrt(a[i]); // sqrt of 100 values } Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Initializing Arrays When an array object is created with the new operator, its elements are automatically initialized to zero. Arrays can be initialized to non-zero values using array initializers, which are comma-separated list enclosed in braces Array initializers only work in declaration statements Example: int a[] = {1, 2, 3, 4, 5}; // Create & initialize array Chapter 5 - Arrays, Strings, File Access, and Plotting

Out-of-Bounds Subscripts Each element of an array is addressed using the name of the array plus the subscripts 0, 1, …, n-1, where n is the number of elements in the array. Subscripts < 0 or  n are illegal, since they do not correspond to real memory locations in the array. These subscripts are said to be out-of-bounds. Reference to an out-of-bounds subscript produces an out-of-bounds exception, and the program will crash if the exception is not handled. Chapter 5 - Arrays, Strings, File Access, and Plotting

Out-of-Bounds Subscripts (2) Example: // Declare and initialize array int a[] = {1,2,3,4,5}; // Write array (with an error!) for ( int i = 0; i <= 5; i++ ) System.out.println("a[" + i + "] = " + a[i]); Result: C:\book\java\chap5>java TestBounds a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 a[4] = 5 java.lang.ArrayIndexOutOfBoundsException: 5 at TestBounds.main(TestBounds.java:15) Note failure when access to an out-of-bounds subscript (5) is attempted. Chapter 5 - Arrays, Strings, File Access, and Plotting

Reading and Writing Data to Files Disk files are a convenient way to store large amounts of data between uses. The simplest way to read or write disk files is with command-line redirection. The file to read data from is listed after a < on the command line. All input data comes from this file. The file to write data from is listed after a > on the command line. All output data goes to this file. Example: D:\>java Example < infile > outfile Chapter 5 - Arrays, Strings, File Access, and Plotting

Reading and Writing Data to Files Command line redirection is relatively inflexible, since all input must be in a single file and all output must go to a single file. It is better to use the Java I/O system to open and close files as needed when a program is running. The Java I/O system is very complex, and discussion is postponed to Chapter 14. We introduce 2 convenience classes FileIn and FileOut for easy Java I/O. Chapter 5 - Arrays, Strings, File Access, and Plotting

Reading Files with Class FileIn Class chapman.io.FileIn is designed to read numeric data from an input file. To open a file for reading, import package chapman.io, and create a new FileIn object with the name of the file as a calling parameter. FileIn in = new FileIn("inputFile"); Then read input data using the readDouble(), readFloat(), readInt(), or readLong() methods. Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Using Class FileIn Import package Open file by creating object Check for valid file open Read data one value at a time Close file when done Report errors if they exist Chapter 5 - Arrays, Strings, File Access, and Plotting

Writing Files with Class FileOut Class chapman.io.FileOut is designed to write formatted data from an output file. To open a file for writing, import package chapman.io, and create a new FileOut object with the name of the file as a calling parameter. FileOut out = new FileOut("outFile"); Then write output data using the printf() method, and close file using the close() method. Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Using Class FileOut Import package Open file by creating object Check for valid file open Write data one value at a time Close file when done Chapter 5 - Arrays, Strings, File Access, and Plotting

Introduction to Plotting Support for graphics is built into the Java API. Support is in the form of low-level graphics classes and methods These methods are discussed in Chapter 11 Meanwhile, we will use the convenience class chapman.graphics.JPlot2D to create 2D plots. Chapter 5 - Arrays, Strings, File Access, and Plotting

Introduction to Class JPlot2D Class JPlot2D supports many types of plots: Linear plots Semilog x plots Semilog y plots Logarithmic plots Polar Plots Bar Plots Examples are shown on the following slides Chapter 5 - Arrays, Strings, File Access, and Plotting

Example JPlot2D Outputs (1) Linear Plot Semilog x Plot Chapter 5 - Arrays, Strings, File Access, and Plotting

Example JPlot2D Outputs (2) Semilog y Plot Log-log Plot Chapter 5 - Arrays, Strings, File Access, and Plotting

Example JPlot2D Outputs (3) Polar Plot Bar Plot Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Creating Plots To create plots, use the template shown on the following two pages Create arrays x and y containing the data to plot, and insert it into the template The components of this program will be explained in Chapters 11 and 12; meanwhile we can use it to plot any desired data See Table 5.5 for a list of plotting options Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Using Class JPlot2D (1) Import packages Create data to plot here Create JPlot2D obj Set data to plot Set the desired plotting styles Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Using Class JPlot2D (2) Create frame to hold plot Create listener (see Chap 11) Set size of plot Add plot to frame Window listener (see Chap 11) Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting A String is an object containing one or more characters, treated as a unit. Strings are types of objects. Once a string is created, its contents never change. The simplest form of string is a string literal, which is a series of characters between quotation marks. Example: "This is a string literal." Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Creating Strings To create a String: First, declare a reference to a String. Then, create the String with a string literal or the new operator. Examples: String s1, s2; // Create references s1 = "This is a test."; // Create array object s2 = new String(); // Create array object These steps can be combined on a single line: String s3[] = "String 3."; // All together Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Substrings A substring is a portion of a string. The String method substring creates a new String object containing a portion of another String. The forms of this method are: s.substring(int st); // From "st" s.substring(int st, int en); // "st" to "en" This method returns a String object containing the characters from st to en (or the end of the string). Chapter 5 - Arrays, Strings, File Access, and Plotting

Chapter 5 - Arrays, Strings, File Access, and Plotting Substrings (2) Examples: String s = "abcdefgh"; String s1 = s.substring(3); String s2 = s.substring(3,6); Substring s1 contains "defgh", and substring s2 contains "def". Note that the indices start at 0, and that the substring contains the values from st to en-1. "abcdefgh" s s.substring(3) s.substring(3,6) "defgh" s1 "def" s2 Chapter 5 - Arrays, Strings, File Access, and Plotting

Concatenating Strings The String method concat creates a new String object containing the contents of two other strings. The form of this method is: s1.concat(String s2); // Combine s1 and s2 This method returns a String object containing the contents of s1 followed by the contents of s2. Chapter 5 - Arrays, Strings, File Access, and Plotting

Concatenating Strings (2) Example: New object created. Chapter 5 - Arrays, Strings, File Access, and Plotting

Selected Additional String Methods Chapter 5 - Arrays, Strings, File Access, and Plotting