Explore BrainMass
Share

How to write Java code for a Rectangle

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Having problems with part of question beginning:
"These classes come with default "constructor methods". Write the Java code for .." through to and including last question from the question below.

The Class for a Rectangle is shown below. PlaneRectangle is a specialisation of Rectangle with fields that define the location of the centre of the rectangle. Write Java Classes that are implementations of the Rectangle Class and of the PlaneRectangle Class.
(Figure 1 in the attachment)
Now add methods to the Java PlaneRectangle Class that:
a. enable the location of an instance of PlaneRectangle to be moved by a specified distance (expressed as a change in the x-coordinate, and a change in the y-coordinate).
b. Enable a check to be made that a given point lies inside or outside the rectangle.
c. Enable a check to be made that another instance of PlaneRectangle lies completely within the current instance.

These classes come with default "constructor methods". Write the Java code for new constructors for the Rectangle Class that enable:
a. An instance of the Rectangle Class to be created with a default length of 1.0. and a default width of 1.0.
b. An instance of the Rectangle Class can be created with user specified values for the length and width.
Now add constructors to the PlaneRectangle Class to enable:
a. An instance of the PlaneRectangle Class to be created with the above default length and width, and located at the origin by default.
b. An instance of the PlaneRectangle Class to be created with user specified values for the length, width and location.

Write a Java application that enables the user to specify the length, width and location of two instances of a PlaneRectangle and check whether the first lies completely within the second.

© BrainMass Inc. brainmass.com October 24, 2018, 5:25 pm ad1c9bdddf
https://brainmass.com/computer-science/java/how-to-write-java-code-for-a-rectangle-9756

Attachments

Solution Preview

"The Class for a Rectangle is shown below"
<br>
<br>As the class is NOT shown in the question, it has to be assumed from the question. The following suggestion is made from a generic assumption based on a rectangle class.
<br>
<br>"These classes come with default "constructor methods"."
<br>
<br>Every class has at least one constructor. If you do not write a constructor, the Java programming language provides one for you. This constructor takes no arguments and has an empty body.
<br>The default constructor doesn't do anything. So, if you want to perform some initialization, you will have to write some constructors for your class.
<br>
<br>"Write the Java code for new constructors for the Rectangle Class that enable:
<br>a. An instance of the Rectangle Class to be created with a default length of 1.0. and a ...

Solution Summary

Constructors and explanation for Rectangle Class

$2.19
See Also This Related BrainMass Solution

Writing JAVA Programming Scripts

2. How does a programmer typically get access to a Graphics object when coding an applet?

One must be created with the Graphics constructor.
It is an instance variable of the class JApplet.
It is a parameter of the paint method.

6. What can be stated about the line drawn by the following code? g.drawLine( 100, 200, 300, 200 )

the line is vertical.
The line is horizontal.
The line is a diagonal.
None the above.

8. What do the arguments 100, 200 represent in the following statement? g.drawRect( 10, 20, 100, 200)
The (x,y) coordinate of the upper-left corner of the rectangle we are drawing
The width and height of the rectangle we are drawing.
The height and width of the rectangle we are drawing.
The (x,y) coordinate of the lower-right corner of the rectangle we are drawing.

14. What is the (x,y) coordinate of the upper-right corner of the rectangle being drawn? g.fillRect( 10, 20, 250, 350 );

18. This code draws a filled rectangle with a width of 100 pixels and a height of 300 pixels, starting at the coordinate (50,30).
//assume you have a Graphics object called g
//your code goes here

20. This code draws a circle of radius 100 with its center located at (200,200).
//assume you have a Graphics object called g
//your code goes here

View Full Posting Details