We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,106 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Interface Problem

Interface:

import java.awt.Rectangle;

public interface Doubler
{
	public void makeDouble(Rectangle newRectangle);
}
import java.awt.Rectangle;

public class RectangleDoubler implements Doubler
{
	private double x, y, width, height;
	RectangleDoubler()
	{
		x = 0.0;
		y = 0.0;
		width = 0.0;
		height = 0.0;
	}//end of default constructor
	public void makeDouble(Rectangle newRectangle)
	{
		x = newRectangle.getX();
		y = newRectangle.getY();
		width = newRectangle.getWidth();
		height = newRectangle.getHeight();

		width*=2;//doubling the width
		height*=2;//doubling the height

		newRectangle.setFrame(x, y, width, height);//setting the Rectangle into new parameters.
	}//end of doubleMethod()
}//end of class

Tester Class:

import java.awt.Rectangle;

public class RectangleDoublerTester
{
	public static void main(String[] args)
	{
		Rectangle box = new Rectangle(5, 10, 20, 30);
		RectangleDoubler doubler = new RectangleDoubler();
		Rectangle box2 = doubler.makeDouble(box);

		System.out.println(box);
		System.out.println("Box Expected:  x=5, y=10, width=20, height=30");
		System.out.println(box2);
		System.out.println("Box2 Expected: x=5, y=10, width=40, height=60");
	}//end of main
}//end of Tester class

I'm stuck on a homework program. Which we have to double the width and height of the Rectangle. The Tester class is set by our teacher, it can't be change.

I keep getting the error:
incompatible types at line 9 in the Tester class.

Rectangle box2 = doubler.makeDouble(box);

Also, how can I return an object? Do I have to set my method into another type?

3
Contributors
3
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
Blackiey
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

have your makeDouble() method return a Rectangle. So something like

public interface Doubler {
     public Rectangle makeDouble(Rectangle newRectangle);
}
Traps
Newbie Poster
23 posts since Jan 2012
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

Thank You!

Blackiey
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Traps

Every function declared in an interface is inherently public, so you don't need a explicit visibility modifier, also, if you don't provide a constructor with a visibility modifier the constructor will by default assume the visibility of the class it is declared in, but you should explicitly declare the visibility anyways for clarity.

thanatos1
Light Poster
35 posts since Jul 2009
Reputation Points: 7
Solved Threads: 3
Skill Endorsements: 2

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0672 seconds using 2.78MB