Sample Programming Problem Consider a Rectangle class, which stores a rectangle
ID: 3690573 • Letter: S
Question
Sample Programming Problem Consider a Rectangle class, which stores a rectangle using two floating point values: width and height. It has 2 constructors (one default, one with 2 arguments), and accessors and mutators for the width and height. It also has a lessThan function (a rectangle is less than another if its area is less than the other's area). The default rectangle has width and length equal to 1. The other constructor takes the initial width and height. The rectangle also has a function area() that calculates and returns the area of the rectangle. (a) Write the class declaration (b) Implement all the class functions (do not inline the definitions)Explanation / Answer
/*Rectangle.java*/
public class Rectangle {
private double length, width;
/**
* constructor
* pre: none
* post: A Rectangle class is created.
*/
public Rectangle() {
length = 1; //default length
width = 1; //default width
}
/**
* constructor
* pre: none
* post: A Rectangle object is created with length and width.
*/
public Rectangle (double l, double w) {
length = l;
width = w;
}
/**
* Changes the length of the Rectangle
* pre: none
* post: Length has been changed.
*/
public void setLength (double newLength) {
length = newLength;
}
/**
* Changes the width of the Rectangle.
* pre: none
* post: Width has been changed.
*/
public void setWidth (double newWidth) {
width = newWidth;
}
/**
* Returns the length of the Rectangle.
* pre: none
* post: The length of the Rectangle has been returned.
*/
public double getLength() {
return(length);
}
/**
* Returns the width of the Rectangle.
* pre: none
* post: The width of the Rectangle has been returned.
*/
public double getWidth() {
return(width);
}
/**
* Returns the area of Rectangle
* pre: none
* post: The area of the Rectangle is returned
*/
public double getArea() {
double triArea;
triArea = length * width;
return(triArea);
}
/**
* Determines if the object is equal to another
* Circle object.
* pre: c is a Circle object.
* post: true has been returned if the objects have
* the same radii, false otherwise.
*/
public static boolean lessThan(Rectangle r1 , Rectangle r2) {
if (r1.getArea() < r2.getArea() ){
return(true);
} else {
return(false);
}
}
/**
* Returns a String that represents the Circle object.
* pre: none
* post: A string representing the Circle object has
* been returned.
*/
public String toString(){
String RectangleString;
RectangleString = "Rectangle has the Area " + length*width;
return(RectangleString);
}
/**
*
* @param args
*/
public static void main(String [] args){
Rectangle spot = new Rectangle();
Rectangle spot2 = new Rectangle(5, 9);
System.out.println("Area is Rectangle with default constructor: " + spot.getArea());
System.out.println("Area is Rectangle with a constructor: " + spot2.getArea());
if(lessThan(spot,spot2) == true) System.out.println("Area is Rectangle 1 is less Than area of Rectangle 2");
}
}
/** class IntList **/
struct IntNode
{
int data;
IntNode *next;
IntNode(int data) : data(data), next(0) {}
};
// Member function to find maximum in list.
void IntList::max()
{
int max = head->data;
// loop compares the current index-value to the max
for(IntNode *i = head; i != 0; i = i->next)
{
if(i->data > max)
{
max = i->data;
}
}
return max;
}
// Member function adds a new node to the front of the list.
void IntList::push_front(int value)
{
// If head doesn't point to address 0, then we make sure we point head to
// a new node, but to preserve the address that head was pointing to, we
// save that address in temp. New list: head -> temp, temp.next-> head[prev]
if(head != 0)
{
IntNode *temp = new IntNode(value);
temp->next = head;
head = temp;
}
else
{
head = new IntNode(value); // Creates new node is head was 0
tail = head;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.