Java.lang.exception while creating an object -
Java.lang.exception while creating an object -
i trying create assignment have check if rectangels overlap or not , few other methods. have completed cant create new objects class. giving me error:(121, 32) java: unreported exception java.lang.exception; must caught or declared thrown here code
/** * created sarang on 18-10-2014. */ import java.awt.*; import java.util.arraylist; import java.util.list; import java.util.random; import java.util.scanner; import javax.swing.jframe; import javax.swing.jpanel; import java.io.*; public class rectangle { private int upper_x, upper_y, lower_x, lower_y; public rectangle(int upper1, int upper2,int lower1,int lower2) throws exception { this.upper_x=upper1; this.upper_y=upper2; this.lower_x=lower1; this.lower_y=lower2; seek { if (upper_x > lower_x || upper_y > lower_y) { throw new illegalargumentexception(); //checking if rectangle valid } if(lower_x >500) {lower_x=500;} if(lower_y>500) {lower_y=500;} //setting lower bounds 500 if(upper_x<50) {upper_x=50;} if(upper_y<50) {upper_y=50;} } grab (illegalargumentexception iae) { system.out.println("invalid rectangle"); } } rectangle( rectangle other) { upper_x=other.upper_x; upper_y=other.upper_y; lower_x=other.lower_x; lower_y=other.lower_y; } public boolean overlap(rectangle other) { if(upper_x<other.upper_x && upper_y<other.upper_y && lower_x>other.lower_x && lower_y>other.lower_y) { homecoming false;} if (!( lower_y < other.upper_y || upper_y > other.lower_y || lower_x < other.upper_x || upper_x > other.lower_x )) { homecoming true;} homecoming false; } public boolean containedin(rectangle other) { if(upper_x<other.upper_x && upper_y<other.upper_y && lower_x>other.lower_x && lower_y>other.lower_y) { homecoming true;} if(other.upper_x<upper_x && other.upper_y<upper_y && other.lower_x>lower_x && other.lower_y>lower_y) { homecoming true; } homecoming false; } public boolean drag(int x, int y) { int temp_1x=upper_x; int temp_1y=upper_y; int temp_2x=lower_x; int temp_2y=lower_y; int length=lower_x-upper_x; int height=lower_y-upper_y; upper_x=x-length/2; upper_y=y-height/2; lower_x=x+length/2; lower_x=y+height/2; if(upper_x<50||upper_y<50||lower_x>500||lower_y>500) { upper_x=temp_1x; upper_y=temp_1y; lower_x=temp_2x; lower_y=temp_2y; homecoming false; } homecoming true; } public boolean resize(int x, int y) { int temp_x,temp_y; temp_x=lower_x; temp_y=lower_y; lower_x=x; lower_y=y; if (upper_x > lower_x || upper_y > lower_y) { lower_x=temp_x; lower_y=temp_y; homecoming false; } if(lower_x >500||lower_y>500) { lower_x=temp_x; lower_y=temp_y; homecoming false; } homecoming true; } public int getupperx(){return upper_x;} public int getuppery(){return upper_y;} public int getlowerx(){return lower_x;} public int getlowery(){return lower_y;} public static void main(string[] args) throws ioexception { rectangle rectangle1 = new rectangle(60,70,400,400); rectangle rectangle2 = new rectangle(100,100,450,450); system.out.println("rectangle1.overlap(rectangle2):" + rectangle1.overlap(rectangle2)); system.out.println("rectangle1.containedin(rectangle2):" +rectangle1.containedin(rectangle2)); } /* * * code below comment visual display of 2d array of rectangles. * not supposed create changes or add together code below comment. * */ class showrecs extends jframe { public showrecs(rectangle[] rectarr) { // super("display arrays"); // rectangle[] r1 = rt.rarr; setdefaultcloseoperation(jframe.exit_on_close); displayrecs recs = new displayrecs(); getcontentpane().add(recs); (int = 0; < rectarr.length; i++) { int w = rectarr[i].getlowerx() - rectarr[i].getupperx(); int h = rectarr[i].getlowery() - rectarr[i].getuppery(); recs.addarrays(rectarr[i].getupperx(), rectarr[i].getuppery(), w, h); } pack(); setlocationrelativeto(null); setvisible(true); } } class displayrecs extends jpanel { private static final int frame_width = 500; private static final int frame_height = frame_width; private list<java.awt.rectangle> rects = new arraylist<java.awt.rectangle>(); public void addarrays(int x, int y, int width, int height) { java.awt.rectangle rect = new java.awt.rectangle(x, y, width, height); rects.add(rect); } @override public dimension getpreferredsize() { homecoming new dimension(frame_width, frame_height); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; (java.awt.rectangle rect : rects) { random rand = new random(); int r = rand.nextint(255); int g = rand.nextint(255); int b = rand.nextint(255); color color = new color(r, g, b); g.setcolor(color); g.drawstring("(" + rect.x + "," + rect.y + ")", rect.x - 45, rect.y); int m = rect.x + rect.width; int l = rect.y + rect.height; int idx = rects.indexof(rect); g.drawstring("(" + m + "," + l + ")", m + 1, l + 1); int locx = rect.x + rect.width / 2; int locy = rect.y + rect.height / 2; g.drawstring((string.valueof(idx)), locx, locy); g2.draw(rect); } } } }
what have done wrong in code. have yet apply other methods not running @ stage
you have:
public rectangle(int upper1, int upper2,int lower1,int lower2) throws exception { ^^^^^^^^^^^^^^^^^
that saying rectangle
constructor can throw any kind of exception. means code calls constructor has deal kind of exception (either catching or declaring thrown).
it looks constructor throwing illegalargumentexception
, unchecked (doesn't need declared in throws
clause). if take off throws
, should work.
public rectangle(int upper1, int upper2,int lower1,int lower2) { ...
java
Comments
Post a Comment