import apcslib.*; // formattingimport chn.util.*; // Input Console
public class Polygon {
public static void main(String args[]) { /*ConsoleIO input = new ConsoleIO(); System.out.print(\"Number of sides: \"); int sides = input.readInt(); System.out.print(\"Length of each side: \"); double length = input.readDouble(); RegularPolygon polly = new RegularPolygon(sides, length);*/ RegularPolygon polly = new RegularPolygon(4, 10); System.out.println("Radius of circumscribed circle = " + polly.getr()); System.out.println("Radius of inscribed circle = " + polly.getR()); System.out.println("Vertex angle = " + polly.vertexAngle()); System.out.println("Perimeter = " + polly.Perimeter()); System.out.println("Area = " + polly.Area()); }}
class RegularPolygon{
private int myNumSides; // # of sides private double mySideLength; // length of side private double myR; // radius of circumscribed circle private double myr; // radius of inscribed circle
// constructors public RegularPolygon() { myNumSides = 4; mySideLength = 2; }
public RegularPolygon(int numSides, double sideLength) { myNumSides = numSides; mySideLength = sideLength; }
// private methods private void calcr() { myr = 1.0/2*mySideLength*(cot(Math.toRadians(180.0)/myNumSides)); }
private void calcR() { myR = 1.0/2*mySideLength*(csc(Math.toRadians(180.0)/myNumSides)); }
// public methods public double vertexAngle() { return ((myNumSides -2.0)/myNumSides)*180.0; }
public double Perimeter() { return myNumSides*mySideLength; }
public double Area() { return 1.0/2.0*myNumSides*(myR*myR)*(double)Math.sin(2.0*Math.toRadians(180.0)/(double)myNumSides); }
public double getNumside() { return myNumSides; }
public double getSideLength() { return mySideLength; }
public double getR() { return myR; }
public double getr() { return myr; }
public double csc(double x) { return 1.0/Math.sin(x); }
public double cot(double x) { return 1.0/Math.tan(x); }}
The part I'm on is this.
#Write a testing class with a main() that constructs a RegularPolygon and calls each method. A sample run of the program for a polygon with 4 sides of length 10 would give:number of sides = 4length of side = 10.00radius of circumscribed circle = 7.07radius of inscribed circle = 5.00vertex angle = 90.0perimeter = 40.00area = 100.00
Write a testing class with a main() that constructs a RegularPolygon and calls each method. A sample run of the program for a polygon with 4 sides of length 10 would give:
number of sides = 4length of side = 10.00radius of circumscribed circle = 7.07radius of inscribed circle = 5.00vertex angle = 90.0perimeter = 40.00area = 100.00
Also you need to probably choose better variable names for exampleCode:private double myR; // radius of circumscribed circle private double myr; // radius of inscribed circleJava supports long variable names for this reason.. you should probably try something more descriptive.Code:private double radiusCircumscribed;private double radiusInscribed;I didn't look at what you are doing with the vars so there may be a better name, I just used what I could make from the description of it.Also it's not good to have the var the same but just different capitolization.. that would lead to confusion. It should be easy for someone else to understand your code and not have to be confused with such things as same name of variables with different capitol letters.(Edited by Suke at 2:50 pm on Oct. 25, 2004)
Java supports long variable names for this reason.. you should probably try something more descriptive.
I didn't look at what you are doing with the vars so there may be a better name, I just used what I could make from the description of it.
Also it's not good to have the var the same but just different capitolization.. that would lead to confusion. It should be easy for someone else to understand your code and not have to be confused with such things as same name of variables with different capitol letters.
(Edited by Suke at 2:50 pm on Oct. 25, 2004)
I agree with you here, this is me being lazy typing code into livewire.
I usually use Hungarian notation for my code. For those of you who don't know, Hungarian notation precedes each variable name with an abbreviation for the type.
ie.nWidth - Integer typelpPoint - Long pointer typesz - string type, 0 terminatedhDC - win32 HANDLEetc, etc, etc.
Long names provides better readability + more typing ;] Though.. I sometimes write cryptic code ;]
Quote: from sakurag at 11:31 pm on Sep. 23, 2004 Note:If you are going to use a constant like 1.0, if you'd like to make it explicitly a double or a float, you can use F or D(I think).such as,Code:float x = 1.0F + 1.2F; This is garbage imo.. even if it does work it's unnecessary as a decimal is assumed as a double.for example 10/3 = 3 (integer division)However 10/3.0 = 3.3333 (treated as a double)(Edited by Suke at 2:52 pm on Oct. 25, 2004)
Note:If you are going to use a constant like 1.0, if you'd like to make it explicitly a double or a float, you can use F or D(I think).such as,Code:float x = 1.0F + 1.2F;
This is garbage imo.. even if it does work it's unnecessary as a decimal is assumed as a double.
for example 10/3 = 3 (integer division)However 10/3.0 = 3.3333 (treated as a double)
(Edited by Suke at 2:52 pm on Oct. 25, 2004)
I disagree. For instance, what if the data type you are using is a float. Unless you explicitly say it is, you will have to type cast it later in order to get rid of warnings.
...ptMyPoint.x = 10/3.0; // downcast, may cause unexpected results
ptMyPoint.x = 10.0F / 3.0F; // No casting necessary
for(i = 0; i < 500000000000; i++){ ptMyPoint.x = (float)(10/3.0);}// Explicit cast 500000000000 times, depending on compiler, EXTREMELY inefficient.
In my line of practice(though usually C), I try to be as precise as possible with my code. I also try to avoid any uneccessary processor power, ie downcasting. (I think upcasting is usually fine, but may eat cpu time).
Of course, we all program differently.
The Code:this. method seems unnecessary he doesn’t need that constructor to set anything because the radius will always be reset for each time the prog is run and there is no reason to change the initial value after the user gives input (or the program generates a test case) . Fyi for those that don’t know Code:this. Allows variables to evaluate them self’s, in other words (this var = the value of the argument var sent in)
Fyi for those that don’t know
The keyword
For example, you have a member variable
You have a function which sets the file name.What do you name the param. you pass in?
The this keyword allows you to name them the same name. Of course, I've never tried to just say x = x; instead of this.x = x, so I don't know if it can tell, but I doubt it.
I suppose it's up to you what you do. I do this in c++ as well.
Thanks for the heads up. I sorta understand it, but I'm sure I'll grasp it better once we get more into it.
import apcslib.*; // formatting import chn.util.*; // Input Console
public static void main(String args[]) { /*ConsoleIO input = new ConsoleIO(); System.out.print(\"Number of sides: \"; int sides = input.readInt(); System.out.print(\"Length of each side: \"; double length = input.readDouble(); RegularPolygon polly = new RegularPolygon(sides, length);*/ RegularPolygon polly = new RegularPolygon(4, 10.0D); System.out.println("Radius of circumscribed circle = " + polly.getr()); System.out.println("Radius of inscribed circle = " + polly.getR()); System.out.println("Vertex angle = " + polly.vertexAngle()); System.out.println("Perimeter = " + polly.Perimeter()); System.out.println("Area = " + polly.Area()); } }
class RegularPolygon {
public const int DEF_NUM_SIDES = 4; public const double DEF_SIDE_LENGTH = 2.0D;
private int numSides; // # of sides private double sideLength; // length of side private double rCircum; // radius of circumscribed circle private double rInscrib; // radius of inscribed circle
// constructors public RegularPolygon( ) { numSides = DEF_NUM_SIDES; sideLength = DEF_SIDE_LENGTH; }
public RegularPolygon(int numSides, double sideLength) { // gs Note: you might want to put checks, ie /* if(numSides > 0) { this.numSides = numSides; } else { this.numSides = DEF_NUM_SIDES; } */ this.numSides = numSides; this.sideLength = sideLength; }
// private methods private void calcr() { this.rInscrib = 1.0D / 2.0D * this.mySideLength * ( this.cot( Math.toRadians(180.0D) / (double)myNumSides)); }
private void calcR() { this.rCircum = 1.0D / 2.0D * this.mySideLength * ( this.csc( Math.toRadians(180.0D) / (double)myNumSides)); }
// public methods public double vertexAngle() { return ( (this.numSides - 2.0D) / (double) myNumSides) * 180.0; }
public double Perimeter() { return (double)this.numSides * this.sideLength; }
public double Area() { return 1.0D / 2.0D * (double)this.numSides * ( this.rCircum * this.rInscrib ) * (double)Math.sin(2.0D * Math.toRadians(180.0D) / (double)myNumSides); }
public int getNumside( ) { return this.numSides; }
public double getSideLength() { return this.mySideLength; }
public double getR() { return this.rCircum; }
public double getr() { return this.rInscrib; }
public double csc(double x) { return 1.0D / Math.sin(x); }
public double cot(double x) { return 1.0D / Math.tan(x); }
I would like you note that some of the things you do are a little weird to me. That asside, I have rewritten some of your code, I don't know if it compiles or runs correctly, since I do not have a java compiler on my computer right now.
Note:If you are going to use a constant like 1.0, if you'd like to make it explicitly a double or a float, you can use F or D(I think).such as,
Also, if you are using a class, like you are go ahead and name your data members whatever you want.
I hope this helps you're problem.