How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Todd Your Own Question
Todd
Todd, Computer Software Engineer
Category: Programming
Satisfied Customers: 422
Experience:  PHP, Ruby, Python, C#, C, C++, Java, CSS, HTML, JavaScript.
21607325
Type Your Programming Question Here...
Todd is online now
A new question is answered every 9 seconds

Write a class definition for a Rectangle class that contains Two

This answer was rated:

Write a class definition for a Rectangle class that contains:
Two int fields, length and width.
Mutator and accessor methods for the length and width fields.
A Boolean method named isSquare that returns true if the rectangle’s length and width are the same and false otherwise.
Write a class definition for a RectangleDriver class that contains a main method that acts as a driver for the Rectangle class of the previous problem. The main method should do this:
Construct a Rectangle object named rect.
Use the mutator methods to assign arbitrary values to rect’s length and width fields.
Use the Boolean method to determine if rect is a square.
If rect is a square, print “Square: ” and then the square’s dimensions. For example:
Square: 4x4
If rect is not a square, print “Rectangle: ” and then the rectangle’s dimensions. Ex:
Rectangle: 6x13
In printing the above messages, use the accessor methods to retrieve the rectangle’s dimensions. Include UML diagram, calculate area & perimeter
Which programming language do you need this in? Looks like Java from your syntax above but making sure.
Customer: replied 7 years ago.
You are correct it is Java. Thanks
OK. I'll submit the answer shortly.
Customer: replied 7 years ago.
When I save this is a separate file it compiles but the program doesn't run.



class Rectangle
{
private int width, length; //variables being used

public Rectangle() {
// empty constructor
}

public void setWidth(int width) {
this.width = width;
}

public void setLength(int length) {
this.length = length;
}

public int getWidth() {
return width;
}

public int getLength() {
return length;
}

public boolean isSquare() {
return width == length;
}

public int getArea() {
return width * length;
}

public int getPerimeter() {
return 2 * width + 2 * length;
}
} //end class Rectangle
Are you running the class containing the main method? For example, if you moved the main method out into a separate class you need to compile and run this class too because the program can only run when a "static void main" method is present.
Customer: replied 7 years ago.
I dont think so. I saved the following as Rectangle.java in textpad:

class Rectangle
{
private int width, length; //variables being used

public Rectangle() {
// empty constructor
}

public void setWidth(int width) {
this.width = width;
}

public void setLength(int length) {
this.length = length;
}

public int getWidth() {
return width;
}

public int getLength() {
return length;
}

public boolean isSquare() {
return width == length;
}

public int getArea() {
return width * length;
}

public int getPerimeter() {
return 2 * width + 2 * length;
}
} //end class Rectangle



Then I saved the second file as RectangleDriver.java with the following:

public class RectangleDriver
{
public static void main(String[] args)
{
java.util.Scanner in = new java.util.Scanner(System.in);

System.out.print("Width: ");
int width = in.nextInt();
System.out.print("Length: ");
int length = in.nextInt();

Rectangle rect = new Rectangle();
rect.setWidth(width);
rect.setLength(length);

if (rect.isSquare())
System.out.println("Square: " + rect.getWidth() + "x" + rect.getLength());

else

System.out.println("Rectangle: " + rect.getWidth() + "x" + rect.getLength());

System.out.println("Area: " + rect.getArea());
System.out.println("Perimeter: " + rect.getPerimeter());

} // end main
} // end class RectangleDriver
What you have is fine.

If you are compiling on the command line then do (without >):
> javac Rectangle.java RectangleDriver.java
This will generate the *.class files. To run the program do:
> java RectangleDriver

This will run the program.


Edited by Todd A. on 10/5/2009 at 7:37 PM EST
Customer: replied 7 years ago.
that didn't work either. Can you please resubmit the code with spaces so I know how it should look in the two different files...one being Rectangle.java and the other RectangleDriver.java
Todd and 2 other Programming Specialists are ready to help you

Related Programming Questions