Sunday, December 27, 2009

Java rectangle applet simple coding question?

why will my code not compile?! it says .class error or something like that! please help :(








import java.awt.Graphics;


import java.awt.Graphics2D;


import javax.swing.JApplet;


import java.awt.Rectangle;





public class RectangleCalculations extends JApplet


{


public void paint(Graphics g)


{


// Prepare for extended graphics


Graphics2D g2 = (Graphics2D) g;


// Construct a rectangle and draw it


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


g2.draw(box);


}





public double getWidth()


{


return width;


}





public double getHeight()


{


return height;


}





double width = box.getWidth;


double length = box.getHeight;


double area = width * height;


double perimeter = ((2 * width) + (2 * height));


g2.drawString(';Area = '; + area);








}


}Java rectangle applet simple coding question?
//You've overridden a parent's methods getWidth and getHeight and used a different return type (double rather than int). The compiler won't allow you to do this.





You should change the name of the getWidth and getHeight methods to fit your concrete class.





Like this: (Changed to getBoxWidth() and getBoxHeight())











import java.awt.Graphics;


import java.awt.Graphics2D;


import java.awt.Rectangle;





import javax.swing.JApplet;





public class RectangleCalculations


extends JApplet {





private double boxWidth;


private double boxHeight;





public void paint(Graphics g) {


// Prepare for extended graphics


Graphics2D g2 = (Graphics2D) g;


// Construct a rectangle and draw it


Rectangle box = new


Rectangle(5, 10, 20, 30);


g2.draw(box);





boxWidth = box.getWidth();


boxHeight = box.getHeight();





double area = boxWidth * boxHeight;


double perimeter = ((2 *


boxWidth) + (2 * boxHeight));





g2.drawString(';Area = '; + area, 10, 10);


}





public double getBoxWidth() {


return boxWidth;


}





public double getBoxHeight() {


return boxHeight;


}


}





//Hope that helps.

No comments:

Post a Comment