Slide 1A Foundation for ProgrammingData TypesObjectsConstructors and MethodsSlide 6Color Data TypeColor Data TypeAlbers SquaresUsing Colors in JavaAlbers SquaresMonochrome LuminanceColor CompatibilityGrayscalePicture Data TypeImage Processing: Grayscale FilterDefining Classes in JavaSlide 18Slide 19Complex Number Data TypeApplications of Complex NumbersComplex Number Data Type: A Simple ClientComplex Number Data Type: ImplementationMandelbrot SetMandelbrot SetPlotting the Mandelbrot SetComplex Number Data Type: Another ClientComplex Number Data Type: Another ClientMandelbrot SetMandelbrot SetSlide 31Mandelbrot SetMandelbrot Set Music Video3.1 Using Data TypesIntroduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 1/14/19 03:15:28 PM2A Foundation for Programmingobjectsfunctions and modulesgraphics, sound, and image I/Oarraysany program you might want to writeconditionals and loopsMath text I/Oassignment statementsprimitive data typescreate your owndata types3Data TypesData type. Set of values and operations on those values.Primitive types. Values directly map to machine representation;ops directly map to machine instructions.We want to write programs that process other types of data.Colors, pictures, strings, input streams, …Complex numbers, vectors, matrices, polynomials, … Points, polygons, charged particles, celestial bodies, …OperationsSet of ValuesData Typenot, and, or, xortrue, falsebooleandoubleintadd, subtract, multiplyany of 264 possible realsadd, subtract, multiply-231 to 231 - 14ObjectsObject. Holds a data type value; variable name refers to object.Object-oriented programming.Create your own data types (set of values and ops on them).Use them in your programs (manipulate objects that hold values).An object = a set of data + a set of methods operating on the datalength, substring, comparesequence of charsStringOperationsSet of ValuesData Typeget red component, brightenThree intsColorPicture get/set color of pixel (i, j)2D array of ColorsInstance of a class. A class is a set of related behaviors (methods).5Constructors and MethodsTo construct a new object:Use keyword new (to invoke constructor).Use name of data type (to specify which type of object). To apply an operation:Use name of object (to specify which object).Use the dot operator (to invoke method).Use the name of the method (to specify which operation).Java does this automatically when you write: s = “Hello, World”Image Processing7Color Data TypeColor. A sensation in the eye from electromagnetic radiation.Set of values. [RGB representation] 2563 possible values, which quantify the amount of red, green, and blue, each on a scale of 0 to 255.2552552552550255000255001052550G1050255R ColorB001058Color Data TypeColor. A sensation in the eye from electromagnetic radiation.Set of values. [RGB representation] 2563 possible values, which quantify the amount of red, green, and blue, each on a scale of 0 to 255.API. Application Programming Interface. (predefined classes) (Next lecture: we will see how to create our own classes)http://download.oracle.com/javase/6/docs/api/java/awt/Color.html9Albers SquaresJosef Albers. Revolutionized the way people think about color.Homage to the Square by Josef Albers (1949-1975)10Using Colors in Javaimport 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); int r2 = Integer.parseInt(args[3]); int g2 = Integer.parseInt(args[4]); int b2 = Integer.parseInt(args[5]); Color c2 = new Color(r2, g2, b2); StdDraw.setPenColor(c1); StdDraw.filledSquare(.25, .5, .2); StdDraw.setPenColor(c2); StdDraw.filledSquare(.25, .5, .1); StdDraw.setPenColor(c2); StdDraw.filledSquare(.75, .5, .2); StdDraw.setPenColor(c1); StdDraw.filledSquare(.75, .5, .1); }} first colorsecond colorfirst squaresecond square11% java AlbersSquares 9 90 166 100 100 100Albers SquaresJosef Albers. Revolutionized the way people think about color.bluegray12Monochrome LuminanceMonochrome luminance. Effective brightness of a color.NTSC formula. Y = 0.299r + 0.587g + 0.114b.import java.awt.Color;public 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; }}13Color CompatibilityQ. Which font colors will be most readable with which background colors on computer and cell phone screens? A. Rule of thumb: difference in luminance should be 128.public static boolean compatible(Color a, Color b) { return Math.abs(lum(a) - lum(b)) >= 128.0;}208255 28 14105 4714GrayscaleGrayscale. When all three R, G, and B values are the same,resulting color is on grayscale from 0 (black) to 255 (white).Convert to grayscale. Use luminance to determine value.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 long15Picture Data TypeRaster graphics. Basis for image processing.Set of values. 2D array of Color objects (pixels).API.16Image Processing: Grayscale FilterGoal. Convert color image to grayscale according to luminance formula.import java.awt.Color;public class Grayscale { public static void main(String[] args) { Picture pic = new Picture(args[0]); for (int x = 0; x < pic.width(); x++) { for (int y = 0; y < pic.height(); y++) { Color color = pic.get(x, y); Color gray = Luminance.toGray(color); pic.set(x, y, gray); } } pic.show(); } }from before17Defining Classes in JavaTo define a class, specify:Set of values.Operations defined on those values.Java class. Defines a class by specifying:Instance variables. (set of values)Methods. (operations defined on those values)Constructors. (create and initialize new objects)Bank Account ClassComplex Numbers20Complex Number Data TypeGoal. Create a data type to manipulate complex numbers.Set of values. Two real numbers: real and imaginary parts.API.a = 3 + 4i, b = -2 + 3ia + b = 1 + 7ia b = -18 + i| a | = 521Applications of Complex NumbersRelevance. A quintessential mathematical
View Full Document