LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 557 users online 158098 members 174 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Video | Dictionary | News | FAQ
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
2 online / 70 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

Java
returns 0!
Replies: 10Last Post Oct. 27, 2004 3:39am by squirellplayingtag
Single page for this topic Email Print Favorite
( squirellplayingtag )


Dairy Product Addict

Patron
Reply
I worked on this all day today and can not figure out why it returns 0 for some of them.
Code:

/**
 @Author: Dave O\'Brien
    Date: Sep 23, 2004
 Teacher:
     Lab:
    Misc:
*/

import apcslib.*; // formatting
import 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);
 }
}


I assume a math function returns something like 0.32 as an int so when multiplied I get 0. I've tried putting (double) everywhere and nothing works. I figured the people who know java know more about it then me and my classmates.

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 = 4
length of side = 10.00
radius of circumscribed circle = 7.07
radius of inscribed circle = 5.00
vertex angle = 90.0
perimeter = 40.00
area = 100.00





-------
"The most abundant element in the universe is
hydrogen, then second is stupidity."-Harlan Ellison
"What good fortune for our governments that the
people do not think."-Hitler

12:27 pm on Sep. 23, 2004 | Joined Jan. 2003 | 1256 Days Active
Join to learn more about squirellplayingtag Maryland, United States | Straight Male | 6469 Posts | 27059 Points
sakurag


Dairy Product Addict

Ad Free
Reply
Code:

/**
@Author: Dave O\'Brien
   Date: Sep 23, 2004
Teacher:
    Lab:
   Misc:
*/

import apcslib.*; // formatting
import 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.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,
Code:

float x = 1.0F + 1.2F;

Also, if you are using a class, like you are go ahead and name your data members whatever you want.
Code:

private double radius;

Then if you need to set the radius, do this.
Code:

public void setRadius(double radius)
{
  this.radius = radius;
}

This gets you away from, 'myradius' or whatever.

I hope this helps you're problem.

-------
I've got spurs that jingle jangle jingle.
As I go riding merrily along.
And they sing, "Oh, ain't you glad you're single?"
And that song ain't so very far from wrong.


9:31 pm on Sep. 23, 2004 | Joined Oct. 2002 | 460 Days Active
Join to learn more about sakurag Washington, United States | Straight Male | 1892 Posts | 8074 Points
( squirellplayingtag )


Dairy Product Addict

Patron
Reply
Yea I figured it out. Turns out I forgot to call two methods which didn't set 2 variables. Opps. I thought that I could call them from inside another method, while returning the numbers which didn't work. Or something like that, I got it straightened out now.

Thanks for the heads up. I sorta understand it, but I'm sure I'll grasp it better once we get more into it.


-------
"The most abundant element in the universe is
hydrogen, then second is stupidity."-Harlan Ellison
"What good fortune for our governments that the
people do not think."-Hitler


12:13 pm on Sep. 24, 2004 | Joined Jan. 2003 | 1256 Days Active
Join to learn more about squirellplayingtag Maryland, United States | Straight Male | 6469 Posts | 27059 Points
sakurag


Dairy Product Addict

Ad Free
Reply
Another thing to consider is to calculate the areas and perimeters when you set the values of the sides and what not.  This way, you will only ever need to calculate them once, if you decide to call the gat 100 times

-------
I've got spurs that jingle jangle jingle.
As I go riding merrily along.
And they sing, "Oh, ain't you glad you're single?"
And that song ain't so very far from wrong.

1:58 pm on Sep. 24, 2004 | Joined Oct. 2002 | 460 Days Active
Join to learn more about sakurag Washington, United States | Straight Male | 1892 Posts | 8074 Points
the real anti christ


Ron Paul is rockin!

Patron
Reply
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)

-------
"Yes humor must be banned to prevent people getting offended!!" -osmoticdespair


8:27 pm on Oct. 22, 2004 | Joined Dec. 2002 | 1073 Days Active
Join to learn more about the real anti christ Congo | 14028 Posts | 25927 Points
sakurag


Dairy Product Addict

Ad Free
Reply
Quote: from the real anti christ at 8:27 pm on Oct. 22, 2004

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)


The keyword
Code:
this
can be completely omitted from any Java code.  The thing is, it allows for a better way to name your member variables.

For example, you have a member variable
Code:
szFilename

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.

-------
I've got spurs that jingle jangle jingle.
As I go riding merrily along.
And they sing, "Oh, ain't you glad you're single?"
And that song ain't so very far from wrong.


9:16 am on Oct. 25, 2004 | Joined Oct. 2002 | 460 Days Active
Join to learn more about sakurag Washington, United States | Straight Male | 1892 Posts | 8074 Points
Suke


Technician
Reply
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)


12:41 pm on Oct. 25, 2004 | Joined Oct. 2004 | 23 Days Active
Join to learn more about Suke Ohio, United States | Straight Male | 79 Posts | 309 Points
Suke


Technician
Reply
Also you need to probably choose better variable names for example
Code:

private double myR;            // radius of circumscribed circle
private double myr;            // radius of inscribed circle

Java 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)


12:47 pm on Oct. 25, 2004 | Joined Oct. 2004 | 23 Days Active
Join to learn more about Suke Ohio, United States | Straight Male | 79 Posts | 309 Points
sakurag


Dairy Product Addict

Ad Free
Reply
Quote: from Suke at 12:41 pm on Oct. 25, 2004

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)


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.

Code:

struct Point
{
 public float x;
 public float y;
 public float z;
}


...
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.  

-------
I've got spurs that jingle jangle jingle.
As I go riding merrily along.
And they sing, "Oh, ain't you glad you're single?"
And that song ain't so very far from wrong.


7:57 pm on Oct. 25, 2004 | Joined Oct. 2002 | 460 Days Active
Join to learn more about sakurag Washington, United States | Straight Male | 1892 Posts | 8074 Points
sakurag


Dairy Product Addict

Ad Free
Reply
Quote: from Suke at 12:47 pm on Oct. 25, 2004

Also you need to probably choose better variable names for example
Code:

private double myR;            // radius of circumscribed circle
private double myr;            // radius of inscribed circle

Java 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)


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 type
lpPoint - Long pointer type
sz       - string type, 0 terminated
hDC    - win32 HANDLE
etc, etc, etc.

Long names provides better readability + more typing ;]  Though.. I sometimes write cryptic code ;]
Code:

(*Rn) = 3.0F * ( (*(RPtn + (*(Ptn + 1)))) - (*(RPtn + (*(Ptn + (N - 1))))) );


-------
I've got spurs that jingle jangle jingle.
As I go riding merrily along.
And they sing, "Oh, ain't you glad you're single?"
And that song ain't so very far from wrong.


8:03 pm on Oct. 25, 2004 | Joined Oct. 2002 | 460 Days Active
Join to learn more about sakurag Washington, United States | Straight Male | 1892 Posts | 8074 Points
( squirellplayingtag )


Dairy Product Addict

Patron
Reply
I do use long variable names when making my own, but the labs give me the variable names as well as a basic outline so I just keep it.

-------
"The most abundant element in the universe is
hydrogen, then second is stupidity."-Harlan Ellison
"What good fortune for our governments that the
people do not think."-Hitler

3:39 am on Oct. 27, 2004 | Joined Jan. 2003 | 1256 Days Active
Join to learn more about squirellplayingtag Maryland, United States | Straight Male | 6469 Posts | 27059 Points
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic