3 Replies - 313 Views - Last Post: 28 March 2011 - 07:44 PM Rate Topic: -----

#1 ell2337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-August 10

calling methods and classes

Posted 28 March 2011 - 06:36 PM

I am new to this website and not sure if you want me to copy and paste like I did here or attach it in a word DOC

I will be up front with you guys this WAS a homework problem but was do last week. I will not recieve anymore cedit for this.I just wantto have a better understanding of what is going on.

The code below is supposed to take in a value form the user and use that value to determin the circumference and are of a circle.the code will compile but the results for the area and circumference is always zero. I think it has somthing to do with the value that is read in from the user never gets carrie dover to the other class where the math is performed.I also wonder if I am calling the methods right.any help will be greatly appreciated.Also any tips on how I should format my questions in the futhur please let me know



package Devry3;
public class circle 
{

	public static double radius;
	public static double circumference;
	public static double Area;
			
public circle()
{
	radius = 0;
	circumference = 0;
	Area = 0;
	
}

public  double CalculateCircumference(double circumference)
{
	circumference = radius * 2 * Math.PI;
	return circumference;
}

public static double CalculateArea(double Area)
{
	Area = Math.pow(radius,2) * Math.PI;
	return Area;
}


}




the second class is below

import javax.swing.JOptionPane;

import Devry3.circle;


public class CircleTestClass {

static String str1;
static double  r;
static double Area;
static double circumference;


public static void main(String[] args) {
	circle methods = new circle();
str1 = JOptionPane.showInputDialog("please enter the radius: ");
double r = Double.parseDouble(str1);

//methods.CalculateCircumference(r);
//methods.CalculateArea(r);



JOptionPane.showMessageDialog(null,"the circumference is: " + methods.CalculateCircumference(circumference));

JOptionPane.showMessageDialog(null,"the Are is: " + methods.CalculateArea(Area));


}
}






Is This A Good Question/Topic? 0
  • +

Replies To: calling methods and classes

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: calling methods and classes

Posted 28 March 2011 - 06:44 PM

Your problem is in passing parameter, the two methods should look like this:
public  double CalculateCircumference(double radius)//radius instead of circumference
{
	circumference = radius * 2 * Math.PI;
	return circumference;
}

public static double CalculateArea(double radius)//radius instead of area
{
	Area = Math.pow(radius,2) * Math.PI;
	return Area;
}


About formatting you have done as required!

This post has been edited by smohd: 29 March 2011 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: calling methods and classes

Posted 28 March 2011 - 07:07 PM

public  double CalculateCircumference(double circumference)
{
    circumference = radius * 2 * Math.PI;
    return circumference;
}


should be
public  double CalculateCircumference(double radius)
{
    circumference = radius * 2 * Math.PI;
    return circumference;
}



because you use the radius value from the static variable which is 0.0

Not a good idea to have static variable

You should create a Cercle object passing the radius to it and them have the other method to access the area and the circumference


public class circle 
{
	private double radius;

	public circle(double radius)
	{
		this.radius = radius;
	}

	public double CalculateCircumference()
	{
		return  radius * 2 * Math.PI;
	}

	public double CalculateArea()
	{
		return Math.pow(radius,2) * Math.PI;
	}

    public static void main(String[] args) {
    	circle c = new circle(10.0);
    	System.out.println("Area: " + c.CalculateArea());
    	System.out.println("Circum: " + c.CalculateCircumference());
    }
}



Was This Post Helpful? 1
  • +
  • -

#4 ell2337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-August 10

Re: calling methods and classes

Posted 28 March 2011 - 07:44 PM

View Postsmohd, on 28 March 2011 - 06:44 PM, said:

Your problem is in passing parameter, the two methods should look like this:
public double CalculateCircumference(double radius)//radius instead of circumference
{
	circumference = radius * 2 * Math.PI;
	return circumference;
}

public static double CalculateArea(double radius)//radius instead of area
{
	Area = Math.pow(radius,2) * Math.PI;
	return Area;
}


About formatting you have done as required!

Thank both you guys for the help. I changed it to radius and ran the program and it still did not work. Then I starte dlooking at it and playing and it turned out that I also had to change the "circumference" to "r" when I called the method in the second class. The r was the double value from when I parsed the input from the user. pbl I have not tried your alternative approach,and by alternative I mean the right way, yet and is past my bed time but will play tommorrow or this weekend and probrably have questions for you. thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1