Computer Programming Methodology Files and Colors COS 130 Computer Programming Methodology Files and Colors
Files May be a device, like a printer Or a section of the disk (document) By default the input is the keyboard By default the output is the screen We can redirect this with > < To get things into the program we use read instructions To get things out of the program we use write or print instructions
Letters vs. Numbers “11” is not the same as 11 “11” is two letters that happen to be ‘1’ and ‘1’ 11 is a number that we can do math with In the computer “11” is 0011000100110001 11 is 1011
Text Files Text files have only characters (letters) Many programs know how to look at text files (Notepad, etc.) It is possible to use only text files However, For some things text files become very large and inefficient.
Binary Files May be much smaller Allows numbers to be saved in the format we need for computation, without translating back and forth to characters However, Programs must be written specifically for the format used.
Binary Numbers “Natural” for the computer Base 2 Roll to the next column every time we reach 2
Decimal 123 Ones 100 Tens 101 Hundreds 102 Binary 1010 Ones 20 Twos 21 Fours 22 Eights 23
Let’s Count Decimal Binary 0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111
Red, Green, Blue (RGB) Model http://en.wikipedia.org/wiki/RGB_color_model How to Represent a Color with 1’s and 0’s
RGB Model For each pixel (spot) use 3 numbers Each number is between 0 – 255 (inclusive) First number is the Red value Second number is the Green value Third number is Blue value The higher the number the greater the color intensity
Examples Red 255 0 0 0 255 0 Green 0 0 255 Blue
PPM Format A file header A sequence of pixels Tells what the rest of the file is like Metadata A sequence of pixels As many as we said in the header Each pixel is an RGB triple
PPM Header P6 White spaces Width using ASCII (letters) in decimal Height using ASCII (letters) in decimal Max color value using ASCII (letters) in decimal A SINGLE White space # to end of line are comments, they may occur anywhere in the header
PPM Header Example P6 800 600 255 P6 #Image by John Hunt #This is a rainbow 800 #Width 600 #Height 255 #This many shades
Writing to Files System.out.print(…); Prints characters Always takes a single argument Converts argument to a String Example System.out.print(“The “); System.out.print(“quick “); System.out.print(“brown fox”); Outputs The quick brown fox
Writing to Files System.out.println(…); Like System.out.print But, goes to the next line This varies depending on system Example System.out.println(“The “); System.out.println(“quick “); System.out.println(“brown fox”); Outputs The quick brown fox
Writing to Files Both print and println can use special control characters \n puts a new line character in. In most situations this causes the text to go to the next line. \t puts a tab character in. In most situations this causes multiple spaces to be displayed.
Writing to Files Example Outputs System.out.print(“The \n“); System.out.println(“quick \n“); System.out.println(“brown fox\n”); Outputs The quick brown fox
Writing to Files A Caution System.out.println(); uses two characters to move to a new line on MS Windows PPM allows only a single white space character at the end of a header To avoid this use System.out.print(“\n”); which will always use just a single new line character
Writing to Files System.out.write(…); Writes binary Will use the least number of bytes Example System.out.write(255); System.out.write(0); Example int red = 255; int green = 0; int blue = 0; System.out.write(red); System.out.write(green); System.out.write(blue);
Write the whole file When writing to disk, using write, your program puts data in memory, then moves it in big blocks to disk When your program ends the data in memory is thrown away without being written You must tell your program when to wrap it up Use System.out.flush();
Example public class Flush { public static void main(String args[]) { for(int i = 0; i < 800; i=i+1){ System.out.write(i); } System.out.flush();
What does this print out? A Last Trick int numTimes = 100; for(int i=0; i < numTimes; i=i+1) { System.out.println(i%4); } What does this print out?