1 / 66

Workshop for Programming And Systems Management Teachers

Workshop for Programming And Systems Management Teachers. Chapter 3 Introduction to Media Computation, Java, and DrJava. Learning Goals. Understand at a conceptual level What can computers do? What is DrJava? What are Java math and conditional operators?

fionan
Download Presentation

Workshop for Programming And Systems Management Teachers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Workshop for Programming And Systems Management Teachers Chapter 3 Introduction to Media Computation, Java, and DrJava Georgia Institute of Technology

  2. Learning Goals • Understand at a conceptual level • What can computers do? • What is DrJava? • What are Java math and conditional operators? • What can computers do better than humans? • What can humans do better than computers? • What is media computation? • How do digital pictures work? • How do digital sounds work? Georgia Institute of Technology

  3. What Can Computers Do? • Basically they can • Add, subtract, multiply, divide • Compare values • Move data • They are amazingly fast • Millions to billions of instructions per second • They can store a huge amount of data • Entire movies, photo albums, dictionaries, encyclopedias, complex games • They keep getting faster, cheaper, and smaller Georgia Institute of Technology

  4. Math Operators in Java (+ * / - %) • Addition 3 + 4 • Multiplication 3 * 4 • Division 3 / 4 • Subtraction 3 – 4 • Negation -4 • Modulo (Remainder) 10 % 2 and 11 % 2 Georgia Institute of Technology

  5. Math Operators Exercise • Open DrJava and do the following in the interactions pane • Subtract 7 from 9 • Add 7 to 3 • Divide 3 by 2 • Divide 4.6 by 2 • Multiply 5 by 10 • Find the remainder when you divide 10 by 3 Georgia Institute of Technology

  6. Why is the result of 3 / 4 zero? • Java is a strongly typed language • Each value has a type associated with it • Tells the computer how to interpret the number • It is an integer, floating point, letter, etc • The compiler determines the type if it isn’t specified (literals) • 3 is an integer • 3.0 is a floating point number (has a fractional part) • The result of an operation is in the same type as the operands • 3 and 4 are integers so the answer is an integer 0 Georgia Institute of Technology

  7. Casting • There are other ways to solve the problem of 3 / 4 has a result of 0 • You can make one of the values floating point by adding .0 • 3.0 / 4 • 3 / 4.0 • The result type will then be floating point • Or you can cast one of the values to the primitive types: float or double • (double) 3 / 4 • 3 / (float) 4 Georgia Institute of Technology

  8. Casting Exercise • Use casting to get the values right for a temperature conversion from Fahrenheit to Celsius • Celsius is 5/9 * (Fahrenheit – 32) • Try it first with a calculator • Try it in DrJava without casting • Try it in DrJava with casting Georgia Institute of Technology

  9. Java Primitive Types • Integers (numbers without fractional parts) are represented by • The types: int or short or long • 235, -2, 33992093, etc • Floating point numbers (numbers with fractional parts) are represented by • The types: double or float • 3.233038983 -423.9, etc • A single character is represented by • The type: char • ‘a’ ‘b’ ‘A’ etc • True and false values are represented by • The type: boolean • true or false Georgia Institute of Technology

  10. Why so Many Different Types? • They take up different amounts of space • They have different precisions • Usually use int, double, and boolean • byte uses 8 bits (1 byte) 2’s compliment • short uses 16 bits (2 bytes) 2’s compliment • int uses 32 bits (4 bytes) 2’s compliment • long uses 64 bits (8 bytes) 2’s compliment • float uses 32 bits (4 bytes) IEEE 754 • double uses 64 bits (8 bytes) IEEE 754 • char uses 16 bits (2 bytes) Unicode format Georgia Institute of Technology

  11. 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits Sizes of Primitive Types byte short int long float double char Georgia Institute of Technology

  12. Floating Point Numbers • Numbers with a fractional part • 6170.20389 • Stored as binary numbers in scientific notation -52.202 is -.52202 x 102 • The sign (1 bit) • The digits in the number (mantissa) • The exponent (8 bits) • Two types • float – 6-7 significant digits accuracy • double – 14-15 significant digits accuracy Georgia Institute of Technology

  13. Operator Order • The default evaluation order is • Negation - • Multiplication * • Division / • Modulo (remainder) % • Addition + • Subtraction - • The default order can be changed • By using parenthesis • (3 + 4) * 2 versus 3 + 4 * 2 Georgia Institute of Technology

  14. Math Operator Order Exercise • Try 2 + 3 * 4 + 5 • Add parentheses to make it clear what is happening first • How do you change it so that 2 + 3 happens first? • How do you change it so that it multiplies the result of 2 + 3 and the result of 4 + 5? Georgia Institute of Technology

  15. Printing Output to the Console • One of the things you often want to do in a program is output the value of something • In Java the way to print to the console is to use • System.out.println(thing to be printed); • To print the thing in the parentheses and a new line • System.out.print(thing to be printed); • To print just the thing in the parentheses Georgia Institute of Technology

  16. Console Output Exercise • Use System.out.println() to print the results of expression to the console • System.out.println(3 * 28) • System.out.println(14 – 7) • System.out.println(10 / 2) • System.out.println(128 + 234) • System.out.println(“Hi “ + “There”); • Try using System.out.print() instead • What is the difference? Georgia Institute of Technology

  17. Strings • Java has a type called: String • A string is an object that has a sequence of characters in Unicode • It can have no characters (the null string “”) • It can have many characters • “This is one long string with spaces in it.” • Java knows how to add strings • It returns a new string with the characters of the second string after the characters of the first Georgia Institute of Technology

  18. Methods • Classes in Java define methods • Recipes or functions f(x) = x2 • May take input • May produce an output • Two Types • Object method • Sent as a message to an object • Implicitly passed the current object • Class method • Sent as a message to a class Georgia Institute of Technology

  19. Method Exercise • In DrJava’s interaction pane try these • Object methods • "HI".toLowerCase() • "This is a string".indexOf("is") • " This is ".trim() • Class methods • Math.abs(13) • Math.abs(-13) • Math.min(3,4) • Character.getNumericValue('A') Georgia Institute of Technology

  20. Common Errors • Did you make any mistakes when you typed in the examples? • If you use the wrong case it won’t work > math.abs(-3) Error: Undefined class 'math‘ • If you misspell something it won’t work > Mat.abs(-3) Error: Undefined class 'Mat‘ > Math.ab(-3) Error: No 'ab' method in 'java.lang.Math' Georgia Institute of Technology

  21. “Hi” is a String Object • The compiler turns literal strings into string objects • Objects of the String class • Object methods are invoked by sending a message • with the same name as the method • the same type, number, and order of input parameters • to the object Georgia Institute of Technology

  22. API Exercise • The Classes defined as part of the Java language are documented in the API • http://java.sun.com/j2se/1.4.2/docs/api/ • Find the documentation for the following classes • String and Math • Find documentation for the methods used in the previous exercise • Try out some other methods for these classes Georgia Institute of Technology

  23. Java Packages • Java groups related classes into packages • Common Packages • java.lang • Contains basic classes for the language • System, Math, Object, … • java.io • Contains classes for input and output • java.awt • Contains basic user interface classes • javax.swing • Contains more advanced user interface classes Georgia Institute of Technology

  24. Class Methods versus Object Methods • In the API documentation how can you tell which are class methods and which are object methods? • Look for the keyword static on the method • If it has the keyword static then it is a class method • If there is no keyword static then it is an object method Georgia Institute of Technology

  25. What do Objects Look Like? • Objects are created with space for their data • Objects have a reference to the object that represents the class • Object of the class “Class” Fries: Food Waffles: Food Name = “Fries” Price = 1.99 Name =“Waffles” Price = 2.99 Food : Class Name = Food Fields = Name, Price Methods = getName, setName, getPrice, setPrice, getCalories Georgia Institute of Technology

  26. Java is Case Sensitive • Some programming languages are case sensitive • Meaning that double isn’t the same as Double • Or string isn’t the same as String • In Java primitive types are all lowercase • double, float, int, • Class names start with an uppercase letter • So String and System are the names of classes Georgia Institute of Technology

  27. Java Naming Conventions • In Java only Class names start with an uppercase letter • System, BufferedImage, Picture • All other names start with lowercase letters but uppercase the first letter of each additional word • picture, fileName, thisIsALongName Georgia Institute of Technology

  28. A Semicolon (;) ends a Statement • Java programs are made up of statements • Like sentences in English • Java statements end in a semicolon not a period • The period is used to send a message to an object • System.out.println() • Or access data in the object • System.out.println() • DrJava prints the result of statements without a semicolon • but not the result of statements with a semicolon • Use System.out.println(); to force output Georgia Institute of Technology

  29. Message Always Have Parenthesis • You can tell that out.println() is sending a message • Because of the () • Messages always have () • Even if there are no parameters (arguments) • If you are sending data along with a message it goes inside the parentheses • Separated by commas • Math.min(3,4); Georgia Institute of Technology

  30. Greater than > 4 > 3 is true 3 > 3 is false 3 > 4 is false Less than < 2 < 3 is true 3 < 2 is false Equal == 3 == 3 is true 3 == 4 is false Not equal != 3 != 4 is true 3 != 3 is false Greater than or equal >= 3 >= 4 is true 3 >= 3 is true 2 >= 4 is false Less than or equal <= 2 <= 3 is true 2 <= 2 is true 4 <= 2 is false Comparison (Relational) Operators Georgia Institute of Technology

  31. Comparison Operators Exercise • In DrJava • Try out the comparison operators in the interactions pane • with numbers • 3 < 4 • 4 <= 4 • 5 < 4 • 6 == 6.0 • with characters (single alphabet letter) • Put single quote around a character • ‘a’ < ‘b’ • ‘b’ < ‘a’ • ‘a’ == ‘a’ Georgia Institute of Technology

  32. Why Do Computers Keep Getting Cheaper? • The number of transistors (a major component of processors) at the same price doubles every 18 months • making computers faster, smaller, and cheaper over time • This notion is know as Moore’s Law • For Gordon Moore, a founder of Intel • This “Law” has held true for decades • And is predicted to hold true at least one more Georgia Institute of Technology

  33. What are Computers Good At? • Producing the same answer every time • Like calculating the sum of hundreds of numbers • Computer don’t tire of tedious tasks • Storing information • They don’t forget information • Looking up information quickly • Search through a phone book to find the customer name for a phone number Georgia Institute of Technology

  34. What are People Good At? • Being Creative • Computers can only do what they are programmed to do • Doing Visual Processing • Reading someone else’s handwriting • Recognizing a face very quickly • Understanding Natural Language • Understanding a variety of speakers • Even if I never heard them speak before Georgia Institute of Technology

  35. What is Media Computation? • Processing millions to billions of • picture elements • sound fragments • movie frames • The speed and storage capacity of modern computers makes this possible • Even for beginning students just learning to program Georgia Institute of Technology

  36. How Does Color Vision Work? • Our eyes and brain work together to make sense of what we see • The cones in our eyes are what allow us to see in color • The rods allow us to see black, white, and shades of gray • Our cones are sensitive to red, green, and blue light • All other colors are combinations of these Georgia Institute of Technology

  37. Red, Green and Blue Light • White light is a combination of red, green, and blue • Full intensity red, green, and blue combined • Black is the absence of all light • No red, green or blue light • All other colors are combinations • Of red, green, and blue • Of different intensities Georgia Institute of Technology

  38. Color Exercise • Start DrJava • In the interactions pane type • ColorChooser.pickAColor(); • Click on the RGB tab and move the sliders to change the intensity of red, green, and blue • Make white, black, red, blue, green, yellow, violet, and orange Georgia Institute of Technology

  39. How do Digital Cameras Work? • There are red, green, and blue filters that capture the amount of each color at a position • A part of a grid • There are many positions • picture element or pixel • 640 x 480 is low resolution • 1600 x 1200 is high resolution • The more pixels the better the picture • Can enlarge it without it looking grainy Georgia Institute of Technology

  40. How do Computer Displays Work? • A display has pixels (picture elements) • Each pixel has a red, green, and blue component • Combinations of red, green, and blue give the resulting color • Black is 0 red, 0 green and 0 blue • White is 255 red, 255 green, 255 blue Georgia Institute of Technology

  41. Pictures are made up of Pixels • Digital cameras record light at pixels • Monitors display pictures using pixels • Our limited vision acuity helps us to see the discrete pixels as a smooth picture • If we blow up the picture we can see the pixels Georgia Institute of Technology

  42. Digital Pictures • Capture the intensity of the red, green, and blue colors at each pixel • Stored as a bunch of numbers • 8 bits for red, 8 bits for green, 8 bits for blue • Need nearly 1 million bytes to store a 640 x 480 picture • Need 3 million bytes to store an image from a 1 megapixel (million pixel) camera • Displayed as red, green, and blue colors on the computer display • Lots of them close together • Our brain sees a smooth color image Georgia Institute of Technology

  43. Picking a File Name • Use the class method pickAFile() on our FileChooser class to pick a file > System.out.println(FileChooser.pickAFile()) • Look for a file that holds a digital picture • .jpg or .gif • Look for a file that holds a digital sound • .wav or .aif or .aiff or .au • The result might look like • C:\intro-prog-java\mediasources\cat.jpg Georgia Institute of Technology

  44. Parts of a File Name • Path separators ‘\’ • Path – where on the hard disk the file is • C:\intro-prog-java\mediasources\ • Base file name • cat.jpg • File extension • .jpg Georgia Institute of Technology

  45. Creating Objects • Remember that a class is a factory that creates objects of that class • We ask a class to create an object by using the keyword: newClassName • We also ask the class to initialize the object • And pass data to help initialize it Georgia Institute of Technology

  46. Creating a Picture Object • A picture needs data to use to create the picture • One way to create it is to give it the full path name of the file to read the data from • We know how to get the full path name • Using FileChooser.pickAFile() > System.out.println(new Picture(FileChooser.pickAFile())) Picture, filename C:\intro-prog-java\mediasources\partFlagSmall.jpg height 217 width 139 Georgia Institute of Technology

  47. Where is the Picture? • When you printed the result of • new Picture(FileChooser.pickAFile()) • You got the file name, width, and height • So it looks like a picture object was created • But, where is the picture? • To get it to display send the picture object the message “show()” • new Picture(FileChooser.pickAFile()).show() Georgia Institute of Technology

  48. How did that work? • The expression inside the parentheses is evaluated first • FileChooser.pickAFile() • This returns the full path name of a file as a String object • “C:\intro-prog-java\mediasources\cat.jpg” • Next the Picture class was asked to create and initialize a new object • using the full path name of the file as the place to read the picture data from • new Picture(“C:\intro-prog-java\mediasources\cat.jpg”) • Then we asked the new picture object to show itself • pictureObject.show() Georgia Institute of Technology

  49. Naming the Results • Another way to do the same thing is to name each of the results • Gives us a way to refer to the result again • Makes it easier to understand • In Java we must give a type when we create a new name • This is called declaring a variable • Type name; or • Type name = something; Georgia Institute of Technology

  50. Assignment Exercise • Try some integer calculations using the assignment operator (‘=‘) int x = 3; int y =4; int z; z = x + y; // assign z to be x + y System.out.println(z); System.out.println(x); System.out.println(y); Georgia Institute of Technology

More Related