Question

Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

Within NetBeans, write a Java program for EACH of the following problems.

(The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains:

· Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.

· A no-arg constructor that creates a default rectangle.

· A constructor that creates a rectangle with the specified width and height. o width should be the first parameter in the constructor o height should be the second parameter

· A method named getArea() that returns the area of this rectangle.

· A method named getPerimeter() that returns the perimeter.

Implement the class, and then write a test program that creates two Rectangle objects— one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order

0 0
Add a comment Improve this question Transcribed image text
Answer #1

To run this code in your netbeans please create a new class named "Rectangle" and then copy the below code.

public class Rectangle{
double width,height;
Rectangle(){
//Default constructor with default sizes
width=1;
height=1;
}
Rectangle(double width, double height){
//Parameterised constructor
this.width=width;
this.height=height;
}
  
double getArea(){
return width*height;
}
  
double getPerimeter(){
return (2*(width+height));
}
  
public static void main(String []args){
//Testing by creating objects
Rectangle obj1=new Rectangle(4,40);
System.out.println("Width: "+obj1.width);
System.out.println("Height: "+obj1.height);
System.out.println("Area: "+obj1.getArea());
System.out.println("Perimeter: "+obj1.getPerimeter());
System.out.println("-------------------------------------");
Rectangle obj2=new Rectangle(3.5,35.9);
System.out.println("Width: "+obj2.width);
System.out.println("Height: "+obj2.height);
System.out.println("Area: "+obj2.getArea());
System.out.println("Perimeter: "+obj2.getPerimeter());

}
}

Code and output:

Feel free to reach out to me in comments in case of any doubts or clarifications. I will be more than happy to reply.

Add a comment
Know the answer?
Add Answer to:
Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

  • using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1)...

    using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1) Two double data members named width and height which specifies the width and height of the rectangle . (2) A no-arg constructor that creates a rectangle with width 1 and height 1. (3) A constructor that creates a rectangle with the specified width and height . (4) A function named getArea() that returns the area of this rectangle . (5) A function named getPerimeter()...

  • Code a rectangle class extending the geometric obi.

    rectangle class contains tge following; -three data fields name recname,  height and width with default values"Ricky" for recName, 5 for the height and 15 for the width.-default constructor with no arguments -constructor that accept width  and height to create  a rectangle  -the accessor of all data feilds-methods that returns the area of the rectangle called getArea()-methods that returns the perimeter of thr rectangle called getPerimeter()-methods that returns the spring description of the rectangle called toString()    Note: the toString() method is...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to...

    PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Python...

  • write a completed program (included the main) for the Q: add an equals method to each...

    write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT