i have to construct a rectangle, print the location, then translate/print it 3 more times such that if they were drawn, they would form one large rectangle.

[][] <- like so
[][]

here's the code & the error.

C:\java>javac FourRectanglePrinter.java
FourRectanglePrinter.java:11: incompatible types
found : void
required: java.awt.Rectangle
Rectangle box1 = box.translate(0,30);
- ^
FourRectanglePrinter.java:14: incompatible types
found : void
required: java.awt.Rectangle
Rectangle box2 = box.translate(40,0);
- ^
FourRectanglePrinter.java:17: incompatible types
found : void
required: java.awt.Rectangle
Rectangle box3 = box.translate(40,30);
- ^

import java.awt.Rectangle;



public class FourRectanglePrinter

{

	public static void main(String[] args)

	{

	System.out.println(" ");

	Rectangle box = new Rectangle(10, 20, 30, 40);

	System.out.println(box);

	System.out.println(" ");

	Rectangle box1 = box.translate(0,30);

	System.out.println(box1);

	System.out.println(" ");

	Rectangle box2 = box.translate(40,0);

	System.out.println(box2);

	System.out.println(" ");

	Rectangle box3 = box.translate(40,30);

	System.out.println(box3);

	}

}

Recommended Answers

All 7 Replies

I did read the api!

translate(int dx, int dy): Translates this Rectangle the indicated distance, to the right along the X coordinate axis, and downward along the Y coordinate axis.

This SEEMS like its what i'm trying to do.. i just need to figure out how to print these translated rectangles!!!!

I'm going to visit the computer science help centre in my school.. see what they say.

You didn't read carefully! If you had, you would have seen that that method, is declared to be void. Meaning that it doesn't return anything.
So when you do this: Rectangle box3 = box.translate(40,30); You get an error because translate has return type "void". It doesn't return anything. It translates the instance that you call it: "box". Just call it. It is is void. Like you call any other void method. And then print the box

how do i call "box"? and then print the translated rectangle??! uggggh hahah

Think what you are trying to do.

Rectangle box = box.translate(40,30);

the variable box is of type Rectangle. As javaAddict pointed out, the translate method returns void. This line is basically like saying:

int i = ;

Therefore, void methods are called independently. Meaning, you do not need to store the 'result' in a variable, since there is no result.
So, once you create a Rectangle:

Rectangle box = new Rectangle(10, 20, 30, 40);

You "call" the translate method like:

Rectangle box = new Rectangle(10, 20, 30, 40);
 box.translate(40,30);

I also see you are trying to 'print' a Rectangle object.

System.out.println(box);

What do you see this code doing? Or what are you expecting it to do? You cannot 'print' a rectangle. You need to draw a rectangle.

This was my question: Write a program caleed FourRectanglePrinter that constructs a
Rectangle object, prints its location by calling System.out.println(box), and then
translates and prints it three more times, so that, if the rectangles were drawn, they would form one large rectangle.

Thanks guys, here is what i submited & i hope its right!:

import java.awt.Rectangle;



public class FourRectanglePrinter

{

	public static void main(String[] args)

	{

	System.out.println(" ");

	Rectangle box = new Rectangle(10, 20, 30, 40);

	System.out.println(box);

	System.out.println(" ");

	box.translate(0,30);

	System.out.println(box);

	box.translate(40,0);

	System.out.println(box);

	box.translate(40,30);

	System.out.println(box);

	}

}

:p

Ah, I thought you were trying to draw the rectangles, I did think it strange that you would not have been told to use a paint method. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.