简体   繁体   中英

Create a program that constructs a Rectangle object (java.awt.Rectangle)

I'm using Dr. Java and the language is java ...i'm a beginner * just a basic rectangle

Problem:

Create a program that constructs a Rectangle object (java.awt.Rectangle). The object should have a getWidth(5) and a getHeight(8) compute and System.out.println() the area of the Rectangle object. Then compute the perimeter and print it as well.

what i have so far

//finding the area
int Width = 5;
int Height = 8;
Rectangle bob = new Rectangle(0,0,5,8);
double area = bob.getWidth()*bob.getHeight();
System.out.println("area = " + area);

 // Find the perimeter  
double perimeter = 2*(bob.getHeight()) + 2*(bob.getWidth()); 
System.out.println("get the perimeter = " + perimeter);` 

Have you tried this tutorial ? It covers the basics of Java 2D and can help you with your problem.

You are on the right track, but you need to read up a little on how System.out.println(...) works.

Rectangle bob = new Rectangle(10,20,5,8); 
double area = bob.getWidth()*bob.getHeight(); 
System.out.println("area = + area");

While you do have a variable named area , you do not reference it in your println statement. What you have in your println statement is one string that happens to have two continuous series of characters that spell "area", but neither one is a reference to the variable name.

System.out.println("area = " + area);

Is quite different. There is one string, "area = " and a concatenation operator which joins the string to the variable area (which will be automatically converted to a String type. It is a fine point of "moving" the terminating quotation mark of the string, but the meaning is quite different.

"area = + area"

one string, with a few funny characters

"area = " + area

one string, a concatenation operator, and a second name which will be "converted" to a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM