Simple Rectangle Class to be built. Intro to Programming in Java problem. Please
ID: 3679569 • Letter: S
Question
Simple Rectangle Class to be built. Intro to Programming in Java problem. Please make sure your response compiles and works with the example below...see link below to copy/paste
http://cs.nyu.edu/~joannakl/cs101.06_s16/assignments/Assignment05.pdf
Problem 2 (40 points): Rectangle Class Following the example of the Circle class in your textbook, design a class named Rectangle to represent a rectangle. The class should contain: Two double data fields named width and height that specify the width and height of the rectangle. The data fields should be private. The default values are 0 for both width and height. » Two double data fields named width and height that specify the width and height of the rectangle. The data fields should be private. The ·A no-arg constructor that creates a default rectangle. ·A constructor that creates a rectangle with the specified width and height.Explanation / Answer
//Two program Rectangle.java and TestRectangle.java both must be executed separately
//and keeping in the same folder
//class Rectangle contains two variables width and length ,both are private
//Rectangle.java
public class Rectangle
{
private double length=0;
private double width=0;
public Rectangle() //default constructor or no arg constructor
{
}
public Rectangle(double l,double w) //constructor with specified width and height
{
length=l;
width=w;
}
public void setWidth(double w) //setter method to set width of the rectangle
{
width=w;
}
public void setLength(double l) //setter method to set length of the rectangle
{
length=l;
}
public double getWidth() //getter method to get the value of the width
{
return width;
}
public double getLength() //getter method to get the value of the length
{
return length;
}
public double getArea() //getArea() method to return area of the Rectangle
{
return width*length;
}
public double getPerimeter() //getPerimeter() method to return perimetre of the Rectangle
{
return width*length;
}
}
//TestRectangle.java
//Using Rectangle class by creating different objects
public class TestRectangle
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle(); //creating default constructor without setting values,taking default values
System.out.println("rectangle 1 :");
System.out.println("dimensions: "+rec1.getWidth() +","+rec1.getLength());
System.out.println("Area"+rec1.getArea());
System.out.println("Perimetre"+rec1.getPerimeter());
Rectangle rec2=new Rectangle(); //default Rectangle and then with setter methods
rec2.setWidth(3.3333);
rec2.setLength(5.1);
System.out.println("rectangle 2 :");
System.out.println("dimensions: "+rec2.getWidth() +","+rec2.getLength());
double area=rec2.getWidth()*rec2.getLength();
System.out.println("Area"+area);
double peri=2*(rec2.getWidth()+rec2.getLength());
System.out.println("Perimetre"+peri);
Rectangle rec3=new Rectangle(13.7,2.3); //creating two parameter constructor
System.out.println("rectangle 3 :");
System.out.println("dimensions: "+rec3.getWidth() +","+rec3.getLength());
System.out.println("Area"+rec3.getArea());
System.out.println("Perimetre"+rec3.getPerimeter());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.