很顯然,根據課堂作業,這是家庭作業..所以幫助我盡你所能/希望,不要讓我張貼HW好嗎?爪哇矩形和鑄造
我一直在死亡,閱讀我的書,通過在線閱讀過去兩個小時,無法找到神的愛爲什麼這個愚蠢的矩形說「找不到方法setX()」。還有一種方法可以使雙打成爲整數,而不會將它們作爲整型?它看起來非常混亂,我們不應該添加任何內容,除非在封閉的方法中......我想我必須投射?
感謝您的幫助,您可以給..
import java.awt.Rectangle;
import java.util.ArrayList;
public class Homework18A
{
public static void main(String[] args)
{
ArrayList<Rectangle> test = new ArrayList<Rectangle>();
test.add(new Rectangle(10, 20, 30, 40));
test.add(new Rectangle(20, 10, 30, 40));
test.add(new Rectangle(10, 20, 40, 50));
test.add(new Rectangle(20, 10, 50, 30));
Rectangle enc = enclosing(test);
System.out.println(enc);
System.out.println("Expected: java.awt.Rectangle[x=10,y=10,width=60,height=60]");
}
/**
Computes the smallest rectangle enclosing all rectangles in
an array list.
@param rects an array list of rectangles of size >= 1
@return the smallest rectangle enclosing all rectangles in rect
*/
public static Rectangle enclosing(ArrayList<Rectangle> rects)
{
Rectangle containRec = new Rectangle(0, 0, 0, 0);
for(int i = 0; i < rects.size(); i++) {
if(rects.get(i).getX() < containRec.getX())
containRec.setX((int)rects.get(i).getX());
if(rects.get(i).getY() < containRec.getY())
containRec.setY((int)rects.get(i).getY());
if(rects.get(i).getX() + rects.get(i).getWidth() > containRec.getWidth())
containRec.setWidth(((int)rects.get(i).getX() + rects.get(i).getWidth()));
if(rects.get(i).getY() + rects.get(i).getHeight() > containRec.getHeight())
containRec.setHeight(((int)rects.get(i).getY() + rects.get(i).getHeight()));
}
return containRec;
}
}
大聲笑沒關係,我想我讓這個問題比它真正的更復雜:(我已經設置了一些東西,但現在得到警告......太多的鑄件〜_〜..... freaking矩形問題是絕對有用...... – Matt