TWiki
>
CS111 Web
>
CS111Fall2014
>
Exam2SolnF14
(2014-12-02, Main.troy)
(raw view)
E
dit
A
ttach
---+ CS 111 - Fall 2014 ---+++ Solutions to Multiple Choice Questions for Exam 2 1 b 1 c 1 b 1 d 1 c 1 a 1 c 1 c 1 b 1 a 1 c 1 b 1 a 1 d 1 b 1 d 1 d 1 d 1 c 1 a Q. 21 <pre> public static Picture makeBlackOrWhite (Picture p) { // access the size of the picture int width = p.getWidth(); int height = p.getHeight(); for ( int xIndex = 0 ; xIndex < width ; ++xIndex ) { // do whatever I need to column number xIndex // access each pixel in this column for ( int yIndex = 0 ; yIndex < height ; ++yIndex ) { // access the pixel at coordinate // (xIndex, yIndex) Pixel pix = p.getPixel (xIndex, yIndex); int r, g, b; // access the individual color values from the pixel r = pix.getRed(); g = pix.getGreen(); b = pix.getBlue(); // modify the individual color amounts int grayAmount = (int)(r * 0.299 + g * 0.587 + b * 0.114); // save the modified values back into the pixel if ( grayAmount < 64 ) { pix.setColor (Color.BLUE); } else if ( grayAmount < 128 ) { pix.setColor (Color.GREEN); } else if ( grayAmount < 192 ) { pix.setColor (Color.CYAN); } else { pix.setColor (Color.YELLOW); } } } return p; }</pre> Q. 22 <pre> public static Picture makeCollage (Picture p1, Picture p2) { int width1 = p1.getWidth(); int height1 = p1.getHeight(); int width2 = p2.getWidth(); int height2 = p2.getHeight(); int height; if (height1 > height2) height = height1; else height = height2; Picture result = new Picture (width1 + width2 , height); // copy the pixels fro, p1 to the result for ( int xIndex = 0 ; xIndex < width1 ; xIndex = xIndex + 1) { // access each pixel in this column for ( int yIndex = 0 ; yIndex < height1 ; yIndex = yIndex + 1 ) { // access the pixel at coordinate // (xIndex, yIndex) Pixel pix1 = p1.getPixel (xIndex, yIndex); Color c1 = pix1.getColor(); Pixel pixR; int xModified = xIndex; int yModified = yIndex; pixR = result.getPixel (xModified, yModified); pixR.setColor (c1); } // end of inner for loop } // end of outer for loop // copy the pixels from p2 to the result for ( int xIndex = 0 ; xIndex < width2 ; xIndex = xIndex + 1) { // access each pixel in this column for ( int yIndex = 0 ; yIndex < height2 ; yIndex = yIndex + 1 ) { // access the pixel at coordinate // (xIndex, yIndex) Pixel pix2 = p2.getPixel (xIndex, yIndex); Color c2 = pix2.getColor(); Pixel pixR; int xModified = xIndex + width1; int yModified = yIndex; pixR = result.getPixel (xModified, yModified); pixR.setColor (c2); } // end of inner for loop } // end of outer for loop return result; } // end of method</pre> -- Main.troy - 2014-12-02
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r1
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r1 - 2014-12-02 - 20:32:28 - Main.troy
CS111
Web Page Spr 18
Syllabus Spr 18
Lecture Notes Spr 18
Sample Code Spr 18
Assignments Spr 18
[edit this menu
]
Log In
CS 111 Main Page
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
ABOUT US
Our Department
Recent News
Contact Us
ACADEMICS
Prospective Students
Undergraduate
CS Minor
Graduate
Courses
RESEARCH
Overview
By Faculty
Labs
PEOPLE
Faculty
Adjuncts
Staff
Students
Alumni
Copyright 2016 The Board of Trustees
of the University of Illinois.
webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF