.getWidth () ,. getHeight () in Java

So, I just started learning Java yesterday, coming from a different language, and I read my tutorial and find it pretty good. However, I did an exercise that basically required me to create a new Rectangle object to use and find the area. Below is the working code that I came up with.

Now, coming out of other programming languages, I just played with it and did it int area,width,height;, and then it gave me an error saying that I had to use double to use .getWidth();, .getHeight(). I could not find anything in my book, telling me why I had to double this, and I started searching online and found this link.

Now I have found some documentation on the Internet where He also told me to use double, but I'm not quite sure why I need to set them as paired. Is it because the people who created Java knew that precision is necessary when we work with coordinates and do math with width, heights and coordinates? My book says that creating a variable doubleinstead intrequires more memory (I get a lot of javascript and PHP, so reading what I did floatand did doublesomething good for me).

those. Why do I need my variable to area,height,widthdouble to use.getWidth,.getHeight

package keepo;
import java.awt.Rectangle;
public class tuna{
    public static void main(String [] args){
        Rectangle rect = new Rectangle(10,20,50,40);
        double area,width,height;
        width = rect.getWidth();
        height = rect.getHeight();
        area = width * height;
        System.out.println("Width is : " + width + "Height is : " + height);
        System.out.println("Area is : " + area);
    }
}
+4
source share
4 answers

, java api. , getWidth(), getHeight() double.

+1

. AWT Rectangle int s. int, , , getter:

int area, width, height;
width = rect.width; // not getWidth()
height = rect.height; // not getHeight()
area = width * height;

getWidth() getHeight() , , , ( int double , a double - , ).

, ( getX() getY()) ? Java 1.2 API . , Rectangle . Java Rectangle int double, , . , , Rectangle2D.Float Rectangle2D.Double, float double .

, , ? Rectangle2D . ( , , ), . , ( , Rectangle2D ). getWidth() getHeight(), double s, .

, , , RectangularShape : Rectangle2D, RoundRectangle2D, Ellipse2D Arc2D. , getWidth() getHeight(), RectangularShape:

// What this shape? A rectangle? An ellipse? Does it use ints? floats? doubles?
RectangularShape something = ......;
// We don't care!
System.out.println("The shape (whatever it is) occupies an area of:");
System.out.println(something.getWidth() + " × " + something.getHeight());

, getter ( " " ), , , , / , , .

P.S. Point, int, double getX() double getY() - Point2D.Float Point2D.Double, Point2D.

P.P.S. double ( long) , int s. 32- int, . , , , :

Rectangle big = new Rectangle(0, 0, 1000000, 1000000);
int area = big.width * big.height;
long bigArea = (long)big.width * big.height;
System.out.println(area); // -727379968 (uh oh!)
System.out.println(bigArea); // 1000000000000
+1

.

java- getHeight() getWidth() .

java.​awt.​Rectangle

  • public double getHeight()


: .

/ int double, height width:

  • getSize(), , (width height)
    • int height = rect.getSize().height;
    • int height = (int) rect.getHeight();
    • int height = (int) rect.getSize().getHeight();
0

Rectangle.getWidth() Rectangle.getHeight() double, . , , Rectangle , int:

int width = (int)rect.getWidth() int height = (int)rect.getHeight()

0
source

Source: https://habr.com/ru/post/1648174/


All Articles