Need help with my JAVA homework, please write the code exactly the question ask.
ID: 3852398 • Letter: N
Question
Need help with my JAVA homework, please write the code exactly the question ask. Thank you.
Exercise 2
Define the class Rectangle that contains double instance variables named width and height, and a static variable named printChar. Include the following:
A no-arg constructor that creates a default rectangle with 1.0 for both width and height.
A constructor that creates a rectangle with the specified width, and height.
Get and set methods for all the instance variables.
A method getArea() that returns the area of the rectangle.
A static method setPrintChar() that modifies the static variable
A toString() method that returns a String that represents an outline drawing of the rectangle using the character given by printChar. For example, if printChar were ‘c’ and one Rectangle had a width of 5 and a height of 4, then printing the toString() method to output would give
ccccc
c c
ccccc
Write a test program that first sets the printChar for the Rectangle class as ‘*’, then creates two Rectangle Objects of reasonable sizes. Print out each rectangle separately to output using the toString() method, followed by the value of that rectangle’s area.
Change the printChar to a reasonable (non-white-space) character of your choosing, modify both instance variables for both Rectangle Objects, and then repeat the output process: print out each rectangle separately to output followed by the value of that rectangle’s area.
Explanation / Answer
class Rectangle{
public static void drawRectangle(int height, int width) {
// y axis
for( int y = 0; y <= width; y++ )
{
if( y == 0 || y == width )
{
for(int i=0;i<=width;i++){
System.out.print("c");
}
System.out.print( " " );
if( y == width )
{
// Leave loop
break;
}
}
// x axis
for( int x = 0; x <= height; x++ )
{
if( x == 0 || x == height )
{
System.out.print( "c" );
if( x == height )
{
System.out.println();
}
}
else
{
System.out.print( " " );
}
}
}
System.out.println("Area of the triangle:" +(height*width));
}
}
public class Draw{
public static void main(String args[]){
Rectangle.drawRectangle(4, 3);
}
}
**Please please provide the CF score by liking the answer. Thanks in advance.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.