Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Objectives This lab was designed to reinforce programming concepts from Chapter 5. In this lab, you will practice: Design Classes Create objects Invoke methods

Lab Objectives

This lab was designed to reinforce programming concepts from Chapter 5. In this lab, you will practice:

Design Classes

Create objects

Invoke methods on objects

Encapsulation

Static members

g a semicolon after the for statement header is not a compilation error because Java treats the

Exercise: n-sided Regular Polygon

In n-sided regular polygon, all sides have the same length and all angles have the same degree. Design and write a Java class named RegularPolygon.java that contains:

A data field named n that defines the number of sides inthe polygon, with default value 3. n should be greater than 2.

A data field named side that stores the length of the side, with default value 1.0. side should be greater than 0.0.

A data field named x that defines the x-coordinate of thepolygons center with default value 0. x should be a positive number or 0.

A data field named y that defines the y-coordinate of thepolygons center with default value 0. y should be a positive number or 0.

A data field named nbOfPolygons that stores the number of created polygon objects.

A no-arg constructor that creates a regular polygon with default values.

A constructor that creates a regular polygon with the specified number of sides and length of side, centered at (0,0).

A constructor that creates a regular polygon with the specified number of sides, length of side, and x- and y-coordinates.

The get and set methodsfor all private data fields.

The method getPerimeter() that returns the perimeter of the polygon.

The method getArea() that returns the area of the polygon. The formula for computing the area of a regular polygon is:image text in transcribed

The method toString() that returns the number of side, the length of side and the center of the polygons similar to the sample output below.

The method getNbOfPolygons() that returns the value of the nbOfPolygons data field.

Note: You should validate the data fields appropriately in the constructors and set() methods. If the values are not valid you have to display an appropriate message which informs the user about the valid range for the data field. If the validation in the constructors is not correct you should add The record is not created. to your message.

Write a test program, TestRegularPolygon.java which includes the following steps:

- Output the current number of created RegularPolygon objects by using getNumberOfPolygons().

- Create the first RegularPolygon object (polygon1) which has default values for all members.

- Output the current number of created RegularPolygon objects by using getNumberOfPolygons ().

- Output its members using toString().

- Set the members n, side, x and y of the first object to 4, 5.6, 8.0 and 9.0 using appropriate set() methods.

- Display the perimeter and area of the first RegularPolygon using getPerimeter() and getArea().

- Create 2nd RegularPolygon object (polygon2) whose number of side is 6 and length of side is 4.0.

- Output the current number of created RegularPolygon objects.

- Output members of the 2nd RegularPolygon object.

- Display the perimeter and area of the 2nd RegularPolygon.

- Set the x-coordinate and y-coordinate of the center of the 2nd RegularPolygon to 6.5 and 5.7.

- Output members of the 2nd RegularPolygon object again.

- Create 3rd RegularPolygon object (polygon3) whose number of side is 10, length of side is 4.0, x-coordinate and y-coordinate of the center are 5.6 and 7.8.

- Output the current number of created RegularPolygon objects.

- Output members of the 3rd RegularPolygon object.

- Display the perimeter and area of the 3rd RegularPolygon.

Sample output

Current number of polygons: 1

Polygon :: Number of side: 3 Length of side: 1.0 Center: (0.0,0.0)

Perimeter: 22.4 Area: 31.36

----------------

Current number of polygons: 2

Polygon :: Number of side: 6 Length of side: 4.0 Center: (0.0,0.0)

Perimeter: 24.0 Area: 41.569219381653056

----------------

Polygon :: Number of side: 6 Length of side: 4.0 Center: (6.5,5.7)

----------------

Current number of polygons: 3

Polygon :: Number of side: 10 Length of side: 4.0 Center: (5.6,7.8)

Perimeter: 40.0 Area: 123.10734148701015

----------------

Area =4tan(n)ns2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What makes he construction project unique

Answered: 1 week ago

Question

=+development and make the product, should you go ahead and do so?

Answered: 1 week ago