Computer Science 1026 Topic 11.docx

2 Pages
Unlock Document

Computer Science
Computer Science 1026A/B
Brian Langen

Topic 11: Returning Values from Methods Returning Values  Previous return value methods: o getWidth(): Returns an integer o getPixel(): Returns the reference to a pixel object o getPixels(): Returns the reference to an array of pixels  The return statement sends the specified value back to where the method was invoked.  The return type must be specified in the method header (replaced by ‘void’)  The return value must be of the correct type, and can be stored in a variable or used directly. public int countWhitePixels() { int counter = 0; for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { Pixel pixelObj = this.getPixel(x,y); if (pixelObj.getRed()==255 && pixelObj.getGreen()==255 && pixelObj.getBlue() == 255) { counter++; } } } return counter; } Returning Boolean  A return value can also be a Boolean value (true or false).  Equal Size Method: o Picture class method that checks whether the two pictures are of the same size o One picture invokes the method, and another is used as a parameter for the method. o If the Picture objects are of the same size, return true. Otherwise, return false. Public Boolean equalSize (Picture otherPic) { Return (this.getWidth() == otherPic.getWidth() && this.getHeight() == otherPic.getHeight()); } Returning Objects  Previously, Picture class methods were invoked on a target Picture object.  It is possible to create a new target Picture object inside a method, and return it as the result of the method  Decrease Red Method: o Picture class method that is invoked on a source picture, and returns a target picture with the same dimensions. o (
More Less

Related notes for Computer Science 1026A/B

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.