0% found this document useful (0 votes)
690 views8 pages

Solution For Problem 9.9 PG 362 Java

The document describes the pseudocode, UML diagram, source code, and test code for a RegularPolygon class. The class defines regular polygons with fields for the number of sides, side length, and coordinates. It includes constructors to set the fields with default or custom values. Methods calculate the perimeter and area, and a toString method returns details based on valid property values. The test code creates RegularPolygon objects with different parameters and displays the results.

Uploaded by

josue868
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
690 views8 pages

Solution For Problem 9.9 PG 362 Java

The document describes the pseudocode, UML diagram, source code, and test code for a RegularPolygon class. The class defines regular polygons with fields for the number of sides, side length, and coordinates. It includes constructors to set the fields with default or custom values. Methods calculate the perimeter and area, and a toString method returns details based on valid property values. The test code creates RegularPolygon objects with different parameters and displays the results.

Uploaded by

josue868
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Josue Rodriguez 0407037

COSC 1337-02
September 5, 2014



Pseudo Code

//Create the constants for our program, such as the int n with 3 as default value, double
side with 1 as default value, and double x and y both 0 as default value.
//Create the no-arg constructor that creates a regular polygon with default values.
Josue Rodriguez 0407037
COSC 1337-02
September 5, 2014

//Create the constructor that creates a regular polygon with specified number of sides, length
of side, and centered at (0, 0).
//Create the constructor that creates a regular polygon with specified number of sides, length
of side, and x- and y- coordinates.
//Create the accesors and mutators methods for all data (n, side, x, and y)
//Create the method to calculate the perimeter: (n * side)
//Create the method to calculate the area: (n * [Link](side,2)) / (4 * [Link]([Link]/n)));

//Now, the method of regular polygon object.
//String Polygon:
//Make conditions for every case, as if and else if

// if (n major or equal than 3) and (side major than 0)
Then, polygon = n, side, area, perimeter

//else if (n major or equal than 3) and (side less or equal 0)
// Then, polygon = the side length must be greater than zero.

//else if (n less than 3) and (side major than 0)
// Then, polygon = the number of edges must be greater than three.

//else, and if none above is true, display = the side length must be greater than
zero and the number of edges must be greater than three.

Return polygon.


Josue Rodriguez 0407037
COSC 1337-02
September 5, 2014

TEST PROGRAM,

//Main method to test our Regular Polygon.
//Create our Regular Polygon objects

//Invoke our regular polygons, and save them in a new reg.
RegularPolygon reg0 = new RegularPolygon() Default values
RegularPolygon reg1 = new RegularPolygon(6,4) New values 1
RegularPolygon reg2 = new RegularPolygon(10,4,5.6,7.8) New values 2

// Show RegularPolygon object values.
//Display reg0, reg1, and reg2.













Flowchart
Josue Rodriguez 0407037
COSC 1337-02
September 5, 2014


Josue Rodriguez 0407037
COSC 1337-02
September 5, 2014

Source Code
/*
* Josue Rodriguez 0407037
* COSC 1337 Online
* Chapter 9 Assignment 9.9
* Sep 5, 2014
*/

public class n_sidedRegularPolygon {

//Constants for our program...

private int n = 3;
private double side = 1;
private double x = 0;
private double y = 0;

//....

public n_sidedRegularPolygon(){}


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

public n_sidedRegularPolygon(int n, double side){
this.n = n;
[Link] = side;
this.x = 0;
this.y = 0;
}


//Constructor that creates a regular polygon with specified number of sides,
//length of side, and x- and y- coordinates...
public n_sidedRegularPolygon(int n, double side, double x, double y){
this.n = n;
[Link] = side;
this.x = x;
this.y = y;
}

//Accesor and mutator methods for all data fields above...


Josue Rodriguez 0407037
COSC 1337-02
September 5, 2014

public int getN(){
return this.n;
}

public void setN(int n){
this.n = n;
}

public double getSide(){
return [Link];
}

public void setSide(double side){
[Link] = side;
}

public double getX(){
return this.x;
}

public void setX(double x){
this.x = x;
}

public double getY(){
return this.y;
}

public void setY(double y){
this.y = y;
}

//Method to calculate the Perimeter...

public double getPerimeter(){
return this.n * [Link];
}

//Method to calculate the Area...

public double getArea(){
return ((this.n * [Link]([Link],2)) / (4 * [Link]([Link]/this.n)));
}


//Method of Regular Polygon object...
Josue Rodriguez 0407037
COSC 1337-02
September 5, 2014

public String toString(){
String Polygon;
if(this.n >= 3 && [Link] > 0){
Polygon = "The Regular Polygon: n= " + this.n + ", side= "+ [Link] +
", Area= " + [Link]() + " and Perimeter= " + [Link]();

}else if(this.n >= 3 && [Link] <= 0){
Polygon = "The side length must be greater than zero.";

}else if(this.n < 3 && [Link] > 0){
Polygon = "The number of edges must be greater than three.";

}else{
Polygon = "The side length must be greater than zero and the number of"
+ "edges must be greater than three.";
}
return Polygon;
}
}
















Josue Rodriguez 0407037
COSC 1337-02
September 5, 2014

Source Code for Test Polygon


/*
* Josue Rodriguez 0407037
* COSC 1337 Online
* Chapter 9 Assignment 9.9
* Sep 5, 2014
*/

public class TestPolygon {

//Main method to test our Regular Polygon...

public static void main(String [] args){

//Create three Regular Polygon objects...

n_sidedRegularPolygon reg0 = new n_sidedRegularPolygon();
n_sidedRegularPolygon reg1 = new n_sidedRegularPolygon(6,4);
n_sidedRegularPolygon reg2 = new n_sidedRegularPolygon(10,4,5.6,7.8);

// Display Regular Polygon object values...

[Link](reg0);
[Link](reg1);
[Link](reg2);
}
}


Output

run:
The Regular Polygon: n= 3, side= 1.0, Area= 0.43301270189221946 and Perimeter= 3.0
The Regular Polygon: n= 6, side= 4.0, Area= 41.569219381653056 and Perimeter= 24.0
The Regular Polygon: n= 10, side= 4.0, Area= 123.10734148701015 and Perimeter= 40.0
BUILD SUCCESSFUL (total time: 0 seconds)

You might also like