Professional Documents
Culture Documents
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 * math.pow(side,2)) / (4 * math.tan(math.pi/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;
this.side = 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;
this.side = 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 this.side;
}
public void setSide(double side){
this.side = 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 * this.side;
}
//Method to calculate the Area...
public double getArea(){
return ((this.n * Math.pow(this.side,2)) / (4 * Math.tan(Math.PI/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 && this.side > 0){
Polygon = "The Regular Polygon: n= " + this.n + ", side= "+ this.side +
", Area= " + this.getArea() + " and Perimeter= " + this.getPerimeter();
}else if(this.n >= 3 && this.side <= 0){
Polygon = "The side length must be greater than zero.";
}else if(this.n < 3 && this.side > 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...
System.out.println(reg0);
System.out.println(reg1);
System.out.println(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)