3.1 Data Types

3y ago
57 Views
2 Downloads
1.96 MB
11 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Shaun Edmunds
Transcription

3.1 Data Types1A Foundation for ProgrammingData TypesData type. Set of values and operations on those values.Primitive types. values directly map to machine representations operations directly translate to machine instructions.any program you might want to writeobjectscreate your owndata typesfunctions and modulesgraphics, sound, and image I/OData TypeSet of ValuesOperationsbooleantrue, falsenot, and, or, xorint-231 to 231 - 1add, subtract, multiplydoubleany of 264possible realsadd, subtract, multiplyarraysconditionals and loopsMathprimitive data typesWe want to write programs that process other types of data. Colors, pictures, strings, input streams, text I/O Complex numbers, vectors, matrices, polynomials, Points, polygons, charged particles, celestial bodies, assignment statements34

ObjectsConstructors and MethodsObject. Holds a data type value; variable name refers to object.To construct a new object: Use keyword new and name of data type.Impact. Enables us to create our own data types; define operations onthem; and integrate into our programs.To apply an operation: Use name of object, the dot operator, andthe name of the method.Data TypeSet of ValuesOperationsColor24 bitsget red component, brightenPicture2D array of colorsget/set color of pixel (i, j)Stringsequence of characterslength, substring, compareColor Data TypeImage ProcessingColor. A sensation in the eye from electromagnetic radiation.Set of values. [RGB representation] 2563 possible values, which quantify theamount of red, green, and blue, each on a scale of 0 to Color8

Color Data TypeAlbers SquaresColor. A sensation in the eye from electromagnetic radiation.Josef Albers. Revolutionized the way people think about color.Set of values. [RGB representation] 2563 possible values, which quantify theamount of red, green, and blue, each on a scale of 0 to 255.API (Application Programming Interface) specifies set of operations.Homage to the Square by Josef Albers /java/awt/Color.html910Albers SquaresUsing Colors in JavaJosef Albers. Revolutionized the way people think about color.blueimport java.awt.Color;public class AlbersSquares{public static void main(String[] args){int r1 Integer.parseInt(args[0]);int g1 Integer.parseInt(args[1]);int b1 Integer.parseInt(args[2]);Color c1 new Color(r1, g1, b1);gray% java AlbersSquares 9 90 166 100 100 100int r2 int g2 int b2 Color 4]);Integer.parseInt(args[5]); new Color(r2, g2, 25, .5, 25, .5, 75, .5, 75, .5, .1);to access Color libraryfirst colorsecond colorfirst squaresecond square}11}12

Monochrome LuminanceColor CompatibilityMonochrome luminance. Effective brightness of a color.Q. Which font colors will be most readable with which background colors oncomputer monitors and cell phone screens?NTSC formula. Y 0.299r 0.587g 0.114b.A. Rule of thumb: difference in luminance should be ! 128.import java.awt.Color;256208105472814public class Luminance{public static double lum(Color c){int r c.getRed();int g c.getGreen();int b c.getBlue();return .299*r .587*g .114*b;}public static boolean compatible(Color a, Color b){return Math.abs(lum(a) - lum(b)) 128.0;}}1314GrayscaleOOP Context for ColorGrayscale. When all three R, G, and B values are the same,resulting color is on grayscale from 0 (black) to 255 (white).Possible memory representation (in TOY).Convert to grayscale. Use luminance to determine value.public static Color toGray(Color c){int y (int) Math.round(lum(c));Color gray new Color(y, y, y);return 6graymagentamemory address("pointer")round doubleto nearest intObject reference is analogous to variable name. We can manipulate the value that it holds. We can pass it to (or return it from) a method.Bottom line. We are writing programs that manipulate color.1516

ReferencesThis is Not a PipeRené Magritte. "This is not a pipe."Neither is this.Java. This is not a color.% java RandomSeq 10000 java AverageColor sienna new Color(160, 82,Color c sienna.darker();45);OOP. Natural vehicle for studying abstract models of the real world.Dan Piraro, http://www.uexpress.com1718Picture Data TypeImage Processing: Grayscale FilterRaster graphics. Basis for image processing.Goal. Convert color image to grayscale according to luminance formula.Set of values. 2D array of Color objects (pixels).import java.awt.Color;API.public class Grayscale{public static void main(String[] args){Picture pic new Picture(args[0]);for (int i 0; i pic.width(); i )for (int j 0; j pic.height(); j ){Color color pic.get(i, j);Color gray Luminance.toGray(color);pic.set(i, j, gray);}set eachpixel tograypic.show();}}1920

Image Processing: Grayscale FilterTEQ on Image Processing 1Goal. Convert color image to grayscale according to luminance formula.What does the following code do? (Easy question!)Picture pic new Picture(args[0]);for (int i 0; i pic.width(); i )for (int j 0; j pic.height(); j )pic.set(i, j, pic.get(i, j)); pic.show();mandrill.jpg% java Grayscale mandrill.jpg21TEQ on Image Processing 222TEQ on Image Processing 3What does the following code do? (Hard question.)What does the following code do?Picture source new Picture(args[0]);int width source.width();int height source.height();Picture target new Picture(width, height);for (int i 0; i width; i )for (int j 0; j height; j )target.set(i, height-j-1, source.get(i, j));target.show();Picture pic new Picture(args[0]);for (int i 0; i pic.width(); i )for (int j 0; j pic.height(); j )pic.set(i, pic.height()-j-1, pic.get(i, j));pic.show();2324

Image Processing: Scaling FilterImage Processing: Scaling FilterGoal. Shrink or enlarge an image to desired size.Goal. Shrink or enlarge an image to desired size.Downscaling. To shrink in half, delete half the rows and columns.Upscaling. To enlarge to double, replace each pixel by 4 copies.Uniform strategy. To convert from ws-by-hs to wt -by-ht : Scale row index by ws / wt . Scale column index by hs / ht . Set color of pixel (i, j) in target image to color of pixel(i " ws / wt , j " hs / ht ) in source image.j ! hs / htj?i ! ws / wtsource image(ws-by-hs)itarget image(wt-by-ht)25Image Processing: Scaling Filter26Image Processing: Scaling FilterScaling filter. Creates two Picture objects and two windows.import java.awt.Color;public class Scale{public static void main(String args[]){String filename args[0];int w Integer.parseInt(args[1]);int h Integer.parseInt(args[2]);Picture source new Picture(filename);Picture target new Picture(w, h);for (int ti 0; ti w; ti )for (int tj 0; tj h; tj ){int si ti * source.width() / w;int sj tj * source.height() / h;Color color source.get(si, sj);target.set(ti, tj, 27% java Scale 400 200 mandrill.jpg28

More Image Processing EffectsString ProcessingRGB color separationswirl filterwave filterglass filterSobel edge detection29String Data TypeTypical String Processing CodeString data type. Basis for text processing.Set of values. Sequence of Unicode characters.API. ring.html3132

Gene FindingGene Finding: AlgorithmPre-genomics era. Sequence a human genome.Algorithm. Scan left-to-right through genome. If start codon, then set beg to index i. If stop codon and substring is a multiple of 3Post-genomics era. Analyze the data and understand structure.Genomics. Represent genome as a string over { A, C, T, G } alphabet.– outputgeneto -1– reset begGene. A substring of genome that represents a functional unit.[start codon] Preceded by ATG.Multipleof3nucleotides.[codons other than start/stop] SucceededbyTAG,TAA,orTGA.[stop codons] multiple of genestopstopgene3334Gene Finding: Implementationpublic class GeneFind{public static void{String start String stop String genome }}OOP Context for StringsPossible memory representation of a string (using TOY addresses).main(String[] args) genome All();int beg -1;for (int i 0; i genome.length() - 2; i ){String codon genome.substring(i, i 3);if (codon.equals(start)) beg i;if (codon.equals(stop) && beg ! -1){String gene genome.substring(beg 3, i);if (gene.length() % 3 0){StdOut.println(gene);beg -1;}% more 15 s genome.substring(1, 5); t genome.substring(9, 13);smemoryaddresstB0B1B2B3D14D94lengths and t are different strings that share the same value "acaa" (s t) is false, but (s.equals(t)) is true.compares pointerscompares character sequences% java GeneFind ATG TAG genomeTiny.txtCATAGCGCATGC3536

Bird's Eye View (Revisited)In and Out38Non-Standard InputScreen Scrapingor use OS to redirect from one fileStandard input. Read from terminal window.Goal. Read from several different input streams.Goal. Find current stock price of Google.Step 1. Find web source.In data type. Read text from stdin, a file, a web site, or network.Ex: Are two text files identical?public class Diff{public static void main(String[] args){In in0 new In(args[0]);In in1 new In(args[1]);String s in0.readAll();String t /finance.yahoo.com/q?s goog39NYSE symbol40

Screen ScrapingScreen ScrapingGoal. Find current stock price of Google.Goal. Find current stock price of Google.Step 2. Find string representation (HTML code) of web source.Step 3. Write code to extract stock price from HTML code. tr td class "yfnc tablehead1" width "48%" Last Trade: /td td class "yfnc tabledata1" big b 459.52 /b /big /td /tr tr td class "yfnc tablehead1" width "48%" Trade Time: /td td class "yfnc tabledata1" 11:45AM ET /td /tr price is stringbetween b and /b after “Last Trade”public class StockQuote{public static void main(String[] args){String name "http://finance.yahoo.com/q?s ";In in new In(name args[0]);String input in.readAll();int start input.indexOf("Last Trade:", 0);int from input.indexOf(" b ", start);int to input.indexOf(" /b ", from);String price input.substring(from 3, to);StdOut.println(price);}}price is stringbetween b and /b after “Last Trade”% java StockQuote goog459.52 s.indexOf(t, i): index of first occurrence of t in s, starting at offset i. Read raw html from http://finance.yahoo.com/q?s goog. Find first string delimited by b and /b after Last Trade.4142Day TraderOOP SummaryAdd bells and whistles. Plot price in real-time.Object. Holds a data type value; variable name refers to object. Notify user if price dips below a certain price. Embed logic to determine when to buy and sell. Automatically send buy and sell orders to trading firm.In Java, programs manipulate references to objects. Exception: primitive types, e.g., boolean, int, double. Reference types: String, Picture, Color, arrays, everything else. OOP purist:language should not have separate primitive types.Warning. Use at your own financial risk.Bottom line.You learned to write programs that manipulatecolors, pictures, strings, and I/O streams.Next time.You will learn to define your own abstractionsand to write programs that manipulate them.The New Yorker, September 6, 19994344

Bottom line. We are writing programs that manipulate color. public static Color toGray(Color c) {int y (int) Math.round(lum(c)); Color gray new Color(y, y, y); return gray;} round double to nearest int 16 OOP Context for Color Possible memory representation (in TOY). Object reference is analogous to variable name.

Related Documents:

Guide 47: Advanced Fortran 90/95 Programming 3 3. Derived Data Types and Pointers 3.1 Derived data types It is possible to create new data types in Fortran 90/95, alongside the intrinsic data types. These are called derived data types and are build from any number of components. The components can be intrinsic data types and any other

Title: ER/Studio Data Architect 8.5.3 Evaluation Guide, 2nd Edition Author: Embarcadero Technologies, Inc. Keywords: CA ERwin data model software Data Modeler data modeler tools data modelers data modeling data modeling software data modeling tool data modeling tools data modeling with erwin data modelings data modeller data modelling software data modelling tool data modelling

neric Data Modeling and Data Model Patterns in order to build data models for crime data which allows complete and consistent integration of crime data in Data Warehouses. Keywords-Relational Data Modeling; Data Warehouse; Generic Data Modeling; Police Data, Data Model Pattern existing data sets as well as new kinds of data I. INTRODUCTION The research about Business Intelligence and Data

Bolero, Jazz Waltz, Add Style). A total of 16 types of accompaniment. 12 types of pop accompaniment. 5 types of Power Chord 3 types of octave play 4 types of guitar chords (Maj, Min, 7, mb5). Styles for Gypsy Jazz have other chord types (Maj6

SPATIAL DATA TYPES AND POST-RELATIONAL DATABASES Post-relational DBMS Support user defined abstract data types Spatial data types (e.g. polygon) can be added Choice of post-relational DBMS Object oriented (OO) DBMS Object relational (OR) DBMS A spatial database is a collection of spatial data types, operators, indices,

Data quality attributes 6. Data Stewardship (accepting responsibility for the data)for the data) 7. Metadata Management (managing the data about the data)about the data) 8. Data Usage (putting the data to work) 9. Data Currency (getting the data at the right time) 10. Education (teaching everyone about their role in data quality) 24

Wires transfer data from one node to another in a BD. Based on the data type of a data source, the color and thickness of its connecting wires change. Wires for the basic data types used in LabVIEW are shown in Figure 2-11. Other than the data types shown in this figure, there are some other specific data types.

2.2 Types of Data There are several types of data that an investigation could require. The data can be of volatile or non-volatile nature: Volatile data – data that may disappear when the system is switched off, i.e., the data in memory (processes, network connections, etc.), or data that may be deleted for one