Design a public class named Rectangle to represent a rectangle following the example of the Circle class we did in Lesson #5. The Rectangle class contains: • Data fields: o private double data field width with default value 2 o private double data field length with default value 3 • Two constructor methods: o A no-argument constructor that creates a default rectangle with the default values o A constructor that creates a rectangle with the specified width and length in the formal parameters • Instance methods: o A public method named getPerimeter() that returns the perimeter of the rectangle (double) o A public method named getArea() that returns the area of the rectangle (double) o Public getter methods to access the data fields, specifically: ▪ getWidth() ▪ getLength() o Public setter methods to set the data fields to values passed in ▪ setWidth (double newWidth) ▪ setLength(double newLength) INFO 1214 –Assignment 3 Page 2 Write a simple test program call TestRectangle like we did in class that creates three Rectangle objects: • rect1 with default values for width and length (call the non-argument constructor) • rect2 with width = 18.5 and length = 33.92 • rect3 with width default values for width and length (call the non-argument constructor) However, you will test the setter methods and set the width and length as follows: o Set the width as a random number between 5 and 10 (excluding 5 and 10) o Set the length as a random number between 10 and 20 (excluding 10 and 20) o Check out Slide #40 Lesson #3 to see how to generate random numbers between two numbers • For each rectangle, print out: o width, length, area, and perimeter o Print each element to 2 significant digits (they are all doubles)
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
Design a public class named Rectangle to represent a rectangle following the example of the Circle class we did in Lesson #5. The Rectangle class contains:
• Data fields:
o private double data field width with default value 2
o private double data field length with default value 3
• Two constructor methods:
o A no-argument constructor that creates a default rectangle with the default values
o A constructor that creates a rectangle with the specified width and length in the formal parameters
• Instance methods:
o A public method named getPerimeter() that returns the perimeter of the rectangle (double)
o A public method named getArea() that returns the area of the rectangle (double)
o Public getter methods to access the data fields, specifically:
▪ getWidth()
▪ getLength()
o Public setter methods to set the data fields to values passed in
▪ setWidth (double newWidth)
▪ setLength(double newLength)
INFO 1214 –Assignment 3
Page 2
Write a simple test program call TestRectangle like we did in class that creates three Rectangle objects:
• rect1 with default values for width and length (call the non-argument constructor)
• rect2 with width = 18.5 and length = 33.92
• rect3 with width default values for width and length (call the non-argument constructor) However, you will test the setter methods and set the width and length as follows:
o Set the width as a random number between 5 and 10 (excluding 5 and 10)
o Set the length as a random number between 10 and 20 (excluding 10 and 20)
o Check out Slide #40 Lesson #3 to see how to generate random numbers between two numbers
• For each rectangle, print out:
o width, length, area, and perimeter
o Print each element to 2 significant digits (they are all doubles)
Step by step
Solved in 4 steps with 3 images