Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Methodology Files and Colors

Similar presentations


Presentation on theme: "Computer Programming Methodology Files and Colors"— Presentation transcript:

1 Computer Programming Methodology Files and Colors
COS 130 Computer Programming Methodology Files and Colors

2 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

3 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 11 is 1011

4 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.

5 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.

6 Binary Numbers “Natural” for the computer Base 2
Roll to the next column every time we reach 2

7 Decimal 123 Ones 100 Tens 101 Hundreds 102 Binary 1010 Ones 20 Twos 21 Fours 22 Eights 23

8 Let’s Count Decimal Binary

9 Red, Green, Blue (RGB) Model
How to Represent a Color with 1’s and 0’s

10 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

11 Examples Red Green Blue

12 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

13 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

14 PPM Header Example P6 800 600 255 P6 #Image by John Hunt
#This is a rainbow 800 #Width 600 #Height 255 #This many shades

15 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

16 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

17 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.

18 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

19 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

20 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);

21 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();

22 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();

23 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?


Download ppt "Computer Programming Methodology Files and Colors"

Similar presentations


Ads by Google