Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Intro Java Computer Science and Programming. I need help with debugging my code. The Assignment: Complete the copyTo method in the Picture class [2 points]

Intro Java Computer Science and Programming.

I need help with debugging my code.

The Assignment:

Complete the copyTo method in the Picture class [2 points] public void copyTo(Picture target, int startX, int startY) This method copies the calling object Picture onto the parameter Picture target, placing the upper-left corner of the calling object image (i.e. coordinate (0, 0)) at coordinate (startX, startY) in the parameter Picture target. It does NOT modify the calling object. This method is similar to the copyPictureTo method defined in your book, with two important exceptions: First, instead of copying from the parameter to the calling object, it copies the picture from the calling object to the Picture passed as a parameter (target). It copies the whole calling object picture to the target, placing its upper left corner at position startX, startY in the parameter Picture. That is, your method must not modify the calling object, but should modify the Picture referenced by the parameter. Also unlike the method defined in the book, it must handle the case where the target Picture is not large enough to hold the calling object picture in in the location specified. If the calling object image will go out of bounds, this method simply copies that part that will fit in the specified position, and ignore the rest. You may assume that startX and startY will be within the bounds of the calling object.

My code (the for loops are nested):

public void copyTo(Picture target, int startX, int startY)

{

Pixel sourcePixel = null;

Pixel targetPixel = null;

int limitx = target.getWidth();

int limity = target.getHeight();

for (int sx = 0, tx = startX; sx < limitx && tx < this.getWidth(); sx++, tx++)

{

for (int sy = 0, ty = startY; sy < limity; sy++, ty++)

{

sourcePixel = target.getPixel(sx, sy);

targetPixel = this.getPixel(tx, ty);

targetPixel.setColor(sourcePixel.getColor());

}

}

}

------------------ TEST 1 -----------------------

Testing copyTo() Method : Source Image : yellowFlowers.jpg Target Image : caterpillar.jpg startX : 0 startY : 0 Test Passed!! Good Job!!

------------------ TEST 2 -----------------------

Testing copyTo() Method : Source Image : caterpillar.jpg Target Image : yellowFlowers.jpg startX : 100 startY : 100 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318) at java.awt.image.BufferedImage.getRGB(BufferedImage.java:917) at SimplePicture.getBasicPixel(SimplePicture.java:300) at Pixel.getAlpha(Pixel.java:72) at Pixel.setColor(Pixel.java:212) at Picture.copyTo(Picture.java:201) at ImageTransformationTester.testcopyTo(ImageTransformationTester.java:39) at ImageTransformationTester.main(ImageTransformationTester.java:291)

Update:

https://gist.github.com/Camicam311/e82597de79670139a25f05ef3dd4d0b8

This link shows the code formatted properly.

We are only supposed to write the method. We cannot access or alter the testers.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 28th British National Conference On Databases Bncod 28 Manchester Uk July 2011 Revised Selected Papers Lncs 7051

Authors: Alvaro A.A. Fernandes ,Alasdair J.G. Gray ,Khalid Belhajjame

2011th Edition

3642245765, 978-3642245763

More Books

Students also viewed these Databases questions

Question

Evaluate the integral. rs In R dR R?

Answered: 1 week ago

Question

6. How do histories influence the process of identity formation?

Answered: 1 week ago