What does this code do? Explain in plain English. The following is a method in t
ID: 3864067 • Letter: W
Question
What does this code do? Explain in plain English.
The following is a method in the Picture class:
1: public Picture sillyStuff(Picture p, Picture q)
2: {
3: int w = Math.min(Math.min(p.getWidth(), q.getWidth()), this.getWidth());
4: int h = Math.min(Math.min(p.getHeight(), q.getHeight()), this.getHeight());
// why the two calls to Math.min?
5: Picture x = new Picture(w,h);
6:
7: for (int a = 0; a < x.getWidth(); a++)
8: {
9: for (int b = 0; b <x.getHeight(); b++)
10: {
11: x.getPixel(a,b).setRed(p.getPixel(a,b).getRed());
12: x.getPixel(a,b).setGreen(q.getPixel(a,b).getGreen());
13: x.getPixel(a,b).setBlue(this.getPixel(a,b).getBlue());
14: }
15: }
16: return x;
17: }
1. In the code above, what does line 3 do?
2. In the code above, what does line 5 do?
3. Does the code above make changes to the Picture based on
A) The pixel locations
B) The pixel color values
C) Both pixel location and pixel color value
D) Ordering of parameter values
4. Suppose I want to change the above code to make a picture that is as LARGE is the largest picture. What problems will I
have with my loop as written? Explain your concerns in English.
5. Suppose I have some wacky Picture method that creates a new Picture from the calling object Picture in a kind of wacky
way. Complete the code with an appropriate if statement to make sure that I have no index out of bounds errors.
Please help me #1 - #5 questions!
Thanks in advance! :-)
#java programming
Explanation / Answer
1. Since Picture sillyStuff is a method in class picture.
So, there must be some object which must have invoked this method.
this.getWidth() is used to get width of that object which invoked this method.
Math.min(p.getWidth(), q.getWidth()) will return minimum of the two widths of p and q objects passed as arguments.
And further one more Math.min will compare result of the above with width of the object which invoked this method.
And finally minimum of these will be the output of line 3.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.